Repository navigation
chore(guard): Refactor functions into enum / handle no args - #551
Merged
dannyvassallo merged 6 commits intoAug 22, 2024
Merged
Conversation
joshfried-aws
left a comment
Contributor
There was a problem hiding this comment.
What do you think about adding an impl for the Function enum like this
pub fn get_expected_number_of_args(&self) -> usize {
match self {
Function::Join => 2,
Function::Substring | Function::RegexReplace => 3,
Function::Count
| Function::JsonParse
| Function::ToUpper
| Function::ToLower
| Function::URLDecode
| Function::ParseString
| Function::ParseBoolean
| Function::ParseFloat
| Function::ParseInt
| Function::ParseChar => 1,
}
}
This would allow us to move up the check for the num of params to the time of parsing like so
fn function_expr(input: Span) -> IResult<Span, FunctionExpr> {
let location = FileLocation {
file_name: input.extra,
line: input.location_line(),
column: input.get_column() as u32,
};
let (input, (name, parameters)) = call_expr(input)?;
let name = Function::try_from(name.as_str()).map_err(|e| {
nom::Err::Error(ParserError {
context: e.to_string(),
span: input,
kind: ErrorKind::AlphaNumeric,
})
})?;
if parameters.len() != name.get_expected_number_of_args() {
return Err(nom::Err::Error(ParserError {
context: format!(
"function: {name} requires: {} parameters to be passed, but received: {}",
name.get_expected_number_of_args(),
parameters.len()
),
span: input,
kind: ErrorKind::AlphaNumeric,
}));
}
Ok((
input,
FunctionExpr {
location,
name,
parameters,
},
))
}
joshfried-aws
approved these changes
Aug 21, 2024
joshfried-aws
approved these changes
Aug 22, 2024
pamkuang
approved these changes
Aug 22, 2024
joshfried-aws
pushed a commit
to joshfried-aws/cloudformation-guard
that referenced
this pull request
Nov 22, 2024
…dformation#551) * chore(guard): Refactor functions into enum / handle optional & no args * chore(guard): Update parser_tests.rs * chore(guard): Address feedback and update tests * chore(guard): Use function name in tests and address feedback * chore(guard): implement callable * chore(guard): Refactor functions into callable structs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #, if available: None
Description of changes:
Uses an enum instead of hardcoded strings for function names and allows for function invocation without arguments.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license