Add lambda expression and map() + filter() functions with ARM syntax#1238
Add lambda expression and map() + filter() functions with ARM syntax#1238Gijsreyn wants to merge 23 commits intoPowerShell:mainfrom
map() + filter() functions with ARM syntax#1238Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements lambda expressions and the map() function for DSC configuration documents, using ARM-compatible syntax (lambda('param', body) and lambdaVariables('param')). The implementation stores lambdas in the context with unique UUID-based identifiers and evaluates them by binding lambda parameters to array elements.
Key changes:
- Added
Lambdastruct and lambda-specific handling in the function evaluation pipeline - Implemented
map(),lambda(), andlambdaVariables()functions for array transformations - Extended
Contextwith lambda storage (lambdas) and lambda parameter bindings (lambda_variables)
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/dsc-lib/src/parser/functions.rs | Added Lambda struct and FunctionArg::Lambda variant; special handling for lambda() function to bypass normal evaluation |
| lib/dsc-lib/src/functions/mod.rs | Implemented invoke_lambda() to create and store lambda objects with UUID-based IDs; registered new lambda-related functions |
| lib/dsc-lib/src/functions/map.rs | Implemented array transformation function that evaluates lambdas for each element with optional index parameter |
| lib/dsc-lib/src/functions/lambda_variables.rs | Implemented parameter lookup function for accessing lambda-bound variables during evaluation |
| lib/dsc-lib/src/functions/lambda.rs | Created lambda function stub that returns error if invoked directly (handled specially elsewhere) |
| lib/dsc-lib/src/configure/context.rs | Added lambda_variables HashMap and lambdas RefCell for lambda storage and parameter binding |
| lib/dsc-lib/locales/en-us.toml | Added localized error messages and descriptions for lambda-related functions |
| dsc/tests/dsc_lambda.tests.ps1 | Added integration tests for map() with various lambda scenarios (multiplication, index usage, range, empty array) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
97cf256 to
b180986
Compare
…//github.com/Gijsreyn/operation-methods into PowerShellgh-57/main/add-map-lambda-function
lib/dsc-lib/src/functions/map.rs
Outdated
| for (index, element) in array.iter().enumerate() { | ||
| // Create a new context with lambda variables bound | ||
| let mut lambda_context = context.clone(); | ||
|
|
||
| // 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); | ||
| } |
There was a problem hiding this comment.
I haven't thought this through, but instead of having each lambda function implement processing lambdas, it would seem like this could be in lambda() itself so the other lambda functions in https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-lambda would just handle how to apply the lambda result.
Perhaps it would help to also implement filter() in this PR to see how it would work?
There was a problem hiding this comment.
I added the filter() function to compare, where I had introduced a new file (lamba_helpers.rs). Mind checking again?
map() function with ARM syntaxmap() + filter() functions with ARM syntax
| FunctionArgKind::String, | ||
| FunctionArgKind::Number, | ||
| FunctionArgKind::Boolean, | ||
| FunctionArgKind::Array, | ||
| FunctionArgKind::Object, | ||
| FunctionArgKind::Null, |
There was a problem hiding this comment.
| FunctionArgKind::String, | |
| FunctionArgKind::Number, | |
| FunctionArgKind::Boolean, | |
| FunctionArgKind::Array, | |
| FunctionArgKind::Object, | |
| FunctionArgKind::Null, | |
| FunctionArgKind::Array, | |
| FunctionArgKind::Boolean, | |
| FunctionArgKind::Null, | |
| FunctionArgKind::Number, | |
| FunctionArgKind::Object, | |
| FunctionArgKind::String, |
|
@Gijsreyn can you resolve the merge conflict and accept my suggestion to order then alpha? |
PR Summary
This PR implements lambda expressions and the
map()function for DSC configuration documents, enabling array transformations. The implementation uses ARM compatible syntax e.g., (lambda('param', body)andlambdaVariables('param'))Example usage:
Seperated tests in a different file.
PR Context
Partially addresses #57.