MCPcopy Create free account
hub / github.com/PowerShell/DSC / invoke_user_function

Function invoke_user_function

lib/dsc-lib/src/functions/user_function.rs:24–46  ·  view source on GitHub ↗

Invoke a user-defined function by name with the provided arguments and context. # Arguments `name` - The name of the user-defined function to invoke. `args` - The arguments to pass to the user-defined function. `context` - The current execution context. # Returns `Result ` - The result of the function invocation or an error. # Errors `DscError::Parser` - If the function is not f

(name: &str, args: &[Value], context: &Context)

Source from the content-addressed store, hash-verified

22/// * `DscError::Parser` - If the function is not found, parameters are invalid, or output type is incorrect.
23///
24pub fn invoke_user_function(name: &str, args: &[Value], context: &Context) -> Result<Value, DscError> {
25 if let Some(function_definition) = context.user_functions.get(name) {
26 validate_parameters(name, function_definition, args)?;
27 let mut user_context = context.clone();
28 user_context.process_mode = ProcessMode::UserFunction;
29 // can only use its own parameters and not the global ones
30 user_context.parameters.clear();
31 // cannot call other user functions
32 user_context.user_functions.clear();
33 for (i, arg) in args.iter().enumerate() {
34 let Some(params) = &function_definition.parameters else {
35 return Err(DscError::Parser(t!("functions.userFunction.expectedNoParameters", name = name).to_string()));
36 };
37 user_context.parameters.insert(params[i].name.clone(), (arg.clone(), params[i].r#type.clone()));
38 }
39 let mut parser = Statement::new()?;
40 let result = parser.parse_and_execute(&function_definition.output.value, &user_context)?;
41 validate_output_type(name, function_definition, &result)?;
42 Ok(result)
43 } else {
44 Err(DscError::Parser(t!("functions.userFunction.unknownUserFunction", name = name).to_string()))
45 }
46}
47
48fn validate_parameters(name: &str, function_definition: &UserFunctionDefinition, args: &[Value]) -> Result<(), DscError> {
49 if let Some(expected_params) = &function_definition.parameters {

Callers 1

invokeMethod · 0.85

Calls 5

validate_parametersFunction · 0.85
validate_output_typeFunction · 0.85
parse_and_executeMethod · 0.80
newFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected