|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use crate::DscError; |
| 5 | +use crate::configure::context::Context; |
| 6 | +use crate::functions::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata, FunctionDispatcher}; |
| 7 | +use rust_i18n::t; |
| 8 | +use serde_json::Value; |
| 9 | +use tracing::debug; |
| 10 | + |
| 11 | +#[derive(Debug, Default)] |
| 12 | +pub struct Map {} |
| 13 | + |
| 14 | +impl Function for Map { |
| 15 | + fn get_metadata(&self) -> FunctionMetadata { |
| 16 | + FunctionMetadata { |
| 17 | + name: "map".to_string(), |
| 18 | + description: t!("functions.map.description").to_string(), |
| 19 | + category: vec![FunctionCategory::Array, FunctionCategory::Lambda], |
| 20 | + min_args: 2, |
| 21 | + max_args: 2, |
| 22 | + accepted_arg_ordered_types: vec![ |
| 23 | + vec![FunctionArgKind::Array], |
| 24 | + vec![FunctionArgKind::String], // Lambda ID as string |
| 25 | + ], |
| 26 | + remaining_arg_accepted_types: None, |
| 27 | + return_types: vec![FunctionArgKind::Array], |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fn invoke(&self, args: &[Value], context: &Context) -> Result<Value, DscError> { |
| 32 | + debug!("{}", t!("functions.map.invoked")); |
| 33 | + |
| 34 | + if args.len() != 2 { |
| 35 | + return Err(DscError::Parser(t!("functions.invalidArgCount", name = "map", count = 2).to_string())); |
| 36 | + } |
| 37 | + |
| 38 | + let Some(array) = args[0].as_array() else { |
| 39 | + return Err(DscError::Parser(t!("functions.map.firstArgMustBeArray").to_string())); |
| 40 | + }; |
| 41 | + |
| 42 | + let Some(lambda_id) = args[1].as_str() else { |
| 43 | + return Err(DscError::Parser(t!("functions.map.secondArgMustBeLambda").to_string())); |
| 44 | + }; |
| 45 | + |
| 46 | + // Retrieve the lambda from context |
| 47 | + let lambdas = context.lambdas.borrow(); |
| 48 | + let Some(lambda) = lambdas.get(lambda_id) else { |
| 49 | + return Err(DscError::Parser(t!("functions.map.lambdaNotFound", id = lambda_id).to_string())); |
| 50 | + }; |
| 51 | + |
| 52 | + // Validate parameter count (1 or 2 parameters) |
| 53 | + if lambda.parameters.is_empty() || lambda.parameters.len() > 2 { |
| 54 | + return Err(DscError::Parser(t!("functions.map.lambdaMustHave1Or2Params").to_string())); |
| 55 | + } |
| 56 | + |
| 57 | + // Create function dispatcher for evaluating lambda body |
| 58 | + let dispatcher = FunctionDispatcher::new(); |
| 59 | + let mut result_array = Vec::new(); |
| 60 | + |
| 61 | + // Iterate through array and evaluate lambda for each element |
| 62 | + for (index, element) in array.iter().enumerate() { |
| 63 | + // Create a new context with lambda variables bound |
| 64 | + let mut lambda_context = context.clone(); |
| 65 | + |
| 66 | + // Bind first parameter to array element |
| 67 | + lambda_context.lambda_variables.insert( |
| 68 | + lambda.parameters[0].clone(), |
| 69 | + element.clone() |
| 70 | + ); |
| 71 | + |
| 72 | + // Bind second parameter to index if provided |
| 73 | + if lambda.parameters.len() == 2 { |
| 74 | + lambda_context.lambda_variables.insert( |
| 75 | + lambda.parameters[1].clone(), |
| 76 | + Value::Number(serde_json::Number::from(index)) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + // Evaluate lambda body with bound variables |
| 81 | + let result = lambda.body.invoke(&dispatcher, &lambda_context)?; |
| 82 | + result_array.push(result); |
| 83 | + } |
| 84 | + |
| 85 | + Ok(Value::Array(result_array)) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + use super::*; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn requires_two_args() { |
| 95 | + let func = Map {}; |
| 96 | + let result = func.invoke(&[], &Context::new()); |
| 97 | + assert!(result.is_err()); |
| 98 | + } |
| 99 | +} |
0 commit comments