-
Notifications
You must be signed in to change notification settings - Fork 52
add int function #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add int function #376
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f6b8dee
add int function
tgauth b39e862
fix merge conflict
tgauth 648ae38
update tests
tgauth de2addb
update error msg
tgauth 5f8ceab
Merge branch 'main' into add-int-function
SteveL-MSFT 8c34426
fix typo
tgauth f0a3a12
Merge branch 'add-int-function' of https://github.com/tgauth/dsc into…
tgauth d73f73e
add more specific error
tgauth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::DscError; | ||
| use crate::configure::context::Context; | ||
| use crate::functions::AcceptedArgKind; | ||
| use num_traits::cast::NumCast; | ||
| use serde_json::Value; | ||
| use super::Function; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct Int {} | ||
|
|
||
| impl Function for Int { | ||
| fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { | ||
| vec![AcceptedArgKind::String, AcceptedArgKind::Number] | ||
| } | ||
|
|
||
| fn min_args(&self) -> usize { | ||
| 1 | ||
| } | ||
|
|
||
| fn max_args(&self) -> usize { | ||
| 1 | ||
| } | ||
|
|
||
| fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { | ||
| let arg = &args[0]; | ||
| let value: i64; | ||
| if arg.is_string() { | ||
| let input = arg.as_str().ok_or(DscError::FunctionArg("int".to_string(), "invalid input string".to_string()))?; | ||
| let result = input.parse::<f64>().map_err(|_| DscError::FunctionArg("int".to_string(), "unable to parse string to int".to_string()))?; | ||
| value = NumCast::from(result).ok_or(DscError::FunctionArg("int".to_string(), "unable to cast to int".to_string()))?; | ||
| } else if arg.is_number() { | ||
| value = arg.as_i64().ok_or(DscError::FunctionArg("int".to_string(), "unable to parse number to int".to_string()))?; | ||
| } else { | ||
| return Err(DscError::FunctionArg("int".to_string(), "Invalid argument type".to_string())); | ||
| } | ||
| Ok(Value::Number(value.into())) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::configure::context::Context; | ||
| use crate::parser::Statement; | ||
| use crate::DscError; | ||
|
|
||
| #[test] | ||
| fn string() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[int('4')]", &Context::new()).unwrap(); | ||
| assert_eq!(result, 4); | ||
| } | ||
|
|
||
| #[test] | ||
| fn string_with_decimal() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[int('4.0')]", &Context::new()).unwrap(); | ||
| assert_eq!(result, 4); | ||
| } | ||
|
|
||
| #[test] | ||
| fn number() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[int(123)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, 123); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nested() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[int(int('-1'))]", &Context::new()).unwrap(); | ||
| assert_eq!(result, -1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn error() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let err = parser.parse_and_execute("[int('foo')]", &Context::new()).unwrap_err(); | ||
| assert!(matches!(err, DscError::FunctionArg(_, _))); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.