-
Notifications
You must be signed in to change notification settings - Fork 56
Add lambda expression and map() function with ARM syntax
#1238
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
Open
Gijsreyn
wants to merge
12
commits into
PowerShell:main
Choose a base branch
from
Gijsreyn:gh-57/main/add-map-lambda-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dd9d3d3
Initial setup
Gijsreyn fed30d4
Fix comments
Gijsreyn 9b05728
Re-add localization
Gijsreyn 97cf256
Merge branch 'main' of https://github.com/Gijsreyn/operation-methods …
Gijsreyn acc8181
Remove dead code and add instructions
Gijsreyn 3e019fe
Initial setup
Gijsreyn a81882c
Fix comments
Gijsreyn b180986
Re-add localization
Gijsreyn fc04a3b
Merge branch 'gh-57/main/add-map-lambda-function' of https://github.c…
Gijsreyn 00f9701
Merge branch 'main' into gh-57/main/add-map-lambda-function
Gijsreyn 3176d5b
Remove conflict
Gijsreyn a93f9e2
Fix test to look at output
Gijsreyn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| Describe 'map() function with lambda tests' { | ||
| It 'map with simple lambda multiplies each element by 2' { | ||
| $config_yaml = @' | ||
| $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
| parameters: | ||
| numbers: | ||
| type: array | ||
| defaultValue: [1, 2, 3] | ||
| resources: | ||
| - name: Echo | ||
| type: Microsoft.DSC.Debug/Echo | ||
| properties: | ||
| output: "[map(parameters('numbers'), lambda('x', mul(lambdaVariables('x'), 2)))]" | ||
| '@ | ||
| $out = $config_yaml | dsc config get -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.results[0].result.actualState.output | Should -Be @(2,4,6) | ||
| } | ||
|
|
||
| It 'map with lambda using index parameter' { | ||
| $config_yaml = @' | ||
| $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
| parameters: | ||
| items: | ||
| type: array | ||
| defaultValue: [10, 20, 30] | ||
| resources: | ||
| - name: Echo | ||
| type: Microsoft.DSC.Debug/Echo | ||
| properties: | ||
| output: "[map(parameters('items'), lambda('val', 'i', add(lambdaVariables('val'), lambdaVariables('i'))))]" | ||
| '@ | ||
| $out = $config_yaml | dsc config get -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.results[0].result.actualState.output | Should -Be @(10,21,32) | ||
| } | ||
|
|
||
| It 'map with range generates array' { | ||
| $config_yaml = @' | ||
| $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
| resources: | ||
| - name: Echo | ||
| type: Microsoft.DSC.Debug/Echo | ||
| properties: | ||
| output: "[map(range(0, 3), lambda('x', mul(lambdaVariables('x'), 3)))]" | ||
| '@ | ||
| $out = $config_yaml | dsc config get -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.results[0].result.actualState.output | Should -Be @(0,3,6) | ||
| } | ||
|
|
||
| It 'map returns empty array for empty input' { | ||
| $config_yaml = @' | ||
| $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json | ||
| resources: | ||
| - name: Echo | ||
| type: Microsoft.DSC.Debug/Echo | ||
| properties: | ||
| output: "[map(createArray(), lambda('x', mul(lambdaVariables('x'), 2)))]" | ||
| '@ | ||
| $out = $config_yaml | dsc config get -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.results[0].result.actualState.output.Count | Should -Be 0 | ||
| } | ||
| } |
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,38 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::DscError; | ||
| use crate::configure::context::Context; | ||
| use crate::functions::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata}; | ||
| use rust_i18n::t; | ||
| use serde_json::Value; | ||
|
|
||
|
|
||
| /// The lambda() function is special - it's not meant to be invoked directly | ||
| /// through the normal function dispatcher path. Instead, it's caught in the | ||
| /// Function::invoke method and handled specially via invoke_lambda(). | ||
| /// | ||
| /// This struct exists for metadata purposes and to signal errors if someone | ||
| /// tries to invoke lambda() as a regular function (which shouldn't happen). | ||
| #[derive(Debug, Default)] | ||
| pub struct LambdaFn {} | ||
|
|
||
| impl Function for LambdaFn { | ||
| fn get_metadata(&self) -> FunctionMetadata { | ||
| FunctionMetadata { | ||
| name: "lambda".to_string(), | ||
| description: t!("functions.lambda.description").to_string(), | ||
| category: vec![FunctionCategory::Lambda], | ||
| min_args: 2, | ||
| max_args: 10, // Up to 9 parameters + 1 body | ||
| accepted_arg_ordered_types: vec![], | ||
| remaining_arg_accepted_types: None, | ||
| return_types: vec![FunctionArgKind::Object], // Lambda is represented as a special object | ||
| } | ||
| } | ||
|
|
||
| fn invoke(&self, _args: &[Value], _context: &Context) -> Result<Value, DscError> { | ||
| // This should never be called - lambda() is handled specially in Function::invoke | ||
| Err(DscError::Parser(t!("functions.lambda.cannotInvokeDirectly").to_string())) | ||
| } | ||
| } |
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,77 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::DscError; | ||
| use crate::configure::context::Context; | ||
| use crate::functions::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata}; | ||
| use rust_i18n::t; | ||
| use serde_json::Value; | ||
| use tracing::debug; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct LambdaVariables {} | ||
|
|
||
| impl Function for LambdaVariables { | ||
| fn get_metadata(&self) -> FunctionMetadata { | ||
| FunctionMetadata { | ||
| name: "lambdaVariables".to_string(), | ||
| description: t!("functions.lambdaVariables.description").to_string(), | ||
| category: vec![FunctionCategory::Lambda], | ||
| min_args: 1, | ||
| max_args: 1, | ||
| accepted_arg_ordered_types: vec![vec![FunctionArgKind::String]], | ||
| remaining_arg_accepted_types: None, | ||
| return_types: vec![ | ||
| FunctionArgKind::String, | ||
| FunctionArgKind::Number, | ||
| FunctionArgKind::Boolean, | ||
| FunctionArgKind::Array, | ||
| FunctionArgKind::Object, | ||
| FunctionArgKind::Null, | ||
| ], | ||
| } | ||
| } | ||
|
|
||
| fn invoke(&self, args: &[Value], context: &Context) -> Result<Value, DscError> { | ||
| debug!("{}", t!("functions.lambdaVariables.invoked")); | ||
|
|
||
| if args.len() != 1 { | ||
| return Err(DscError::Parser(t!("functions.invalidArgCount", name = "lambdaVariables", count = 1).to_string())); | ||
| } | ||
|
|
||
| let Some(var_name) = args[0].as_str() else { | ||
| return Err(DscError::Parser(t!("functions.lambdaVariables.paramNameMustBeString").to_string())); | ||
| }; | ||
|
|
||
| // Look up the variable in the lambda context | ||
| if let Some(value) = context.lambda_variables.get(var_name) { | ||
| Ok(value.clone()) | ||
| } else { | ||
| Err(DscError::Parser(t!("functions.lambdaVariables.notFound", name = var_name).to_string())) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use serde_json::json; | ||
|
|
||
| #[test] | ||
| fn lookup_existing_variable() { | ||
| let mut context = Context::new(); | ||
| context.lambda_variables.insert("x".to_string(), json!(42)); | ||
|
|
||
| let func = LambdaVariables {}; | ||
| let result = func.invoke(&[Value::String("x".to_string())], &context).unwrap(); | ||
| assert_eq!(result, json!(42)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn lookup_nonexistent_variable() { | ||
| let context = Context::new(); | ||
| let func = LambdaVariables {}; | ||
| let result = func.invoke(&[Value::String("x".to_string())], &context); | ||
| assert!(result.is_err()); | ||
| } | ||
| } |
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,99 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::DscError; | ||
| use crate::configure::context::Context; | ||
| use crate::functions::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata, FunctionDispatcher}; | ||
| use rust_i18n::t; | ||
| use serde_json::Value; | ||
| use tracing::debug; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct Map {} | ||
|
|
||
| impl Function for Map { | ||
| fn get_metadata(&self) -> FunctionMetadata { | ||
| FunctionMetadata { | ||
| name: "map".to_string(), | ||
| description: t!("functions.map.description").to_string(), | ||
| category: vec![FunctionCategory::Array, FunctionCategory::Lambda], | ||
| min_args: 2, | ||
| max_args: 2, | ||
| accepted_arg_ordered_types: vec![ | ||
| vec![FunctionArgKind::Array], | ||
| vec![FunctionArgKind::String], // Lambda ID as string | ||
| ], | ||
| remaining_arg_accepted_types: None, | ||
| return_types: vec![FunctionArgKind::Array], | ||
| } | ||
| } | ||
|
|
||
| fn invoke(&self, args: &[Value], context: &Context) -> Result<Value, DscError> { | ||
| debug!("{}", t!("functions.map.invoked")); | ||
|
|
||
| if args.len() != 2 { | ||
| return Err(DscError::Parser(t!("functions.invalidArgCount", name = "map", count = 2).to_string())); | ||
| } | ||
|
|
||
| let Some(array) = args[0].as_array() else { | ||
| return Err(DscError::Parser(t!("functions.map.firstArgMustBeArray").to_string())); | ||
| }; | ||
|
|
||
| let Some(lambda_id) = args[1].as_str() else { | ||
| return Err(DscError::Parser(t!("functions.map.secondArgMustBeLambda").to_string())); | ||
| }; | ||
|
|
||
| // Retrieve the lambda from context | ||
| let lambdas = context.lambdas.borrow(); | ||
| let Some(lambda) = lambdas.get(lambda_id) else { | ||
| return Err(DscError::Parser(t!("functions.map.lambdaNotFound", id = lambda_id).to_string())); | ||
| }; | ||
|
|
||
| // Validate parameter count (1 or 2 parameters) | ||
| if lambda.parameters.is_empty() || lambda.parameters.len() > 2 { | ||
| return Err(DscError::Parser(t!("functions.map.lambdaMustHave1Or2Params").to_string())); | ||
| } | ||
|
|
||
| // Create function dispatcher for evaluating lambda body | ||
| let dispatcher = FunctionDispatcher::new(); | ||
| let mut result_array = Vec::new(); | ||
|
|
||
| // Iterate through array and evaluate lambda for each element | ||
| for (index, element) in array.iter().enumerate() { | ||
| // Create a new context with lambda variables bound | ||
| let mut lambda_context = context.clone(); | ||
Gijsreyn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Bind first parameter to array element | ||
| lambda_context.lambda_variables.insert( | ||
| lambda.parameters[0].clone(), | ||
| element.clone() | ||
| ); | ||
|
|
||
| // Bind second parameter to index if provided | ||
| if lambda.parameters.len() == 2 { | ||
| lambda_context.lambda_variables.insert( | ||
| lambda.parameters[1].clone(), | ||
| Value::Number(serde_json::Number::from(index)) | ||
| ); | ||
| } | ||
|
|
||
| // Evaluate lambda body with bound variables | ||
| let result = lambda.body.invoke(&dispatcher, &lambda_context)?; | ||
| result_array.push(result); | ||
| } | ||
|
|
||
| Ok(Value::Array(result_array)) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn requires_two_args() { | ||
| let func = Map {}; | ||
| let result = func.invoke(&[], &Context::new()); | ||
| assert!(result.is_err()); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.