|
| 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::{AcceptedArgKind, Function, FunctionCategory}; |
| 7 | +use rust_i18n::t; |
| 8 | +use serde_json::{Map, Value}; |
| 9 | +use tracing::debug; |
| 10 | + |
| 11 | +#[derive(Debug, Default)] |
| 12 | +pub struct CreateObject {} |
| 13 | + |
| 14 | +impl Function for CreateObject { |
| 15 | + fn description(&self) -> String { |
| 16 | + t!("functions.createObject.description").to_string() |
| 17 | + } |
| 18 | + |
| 19 | + fn category(&self) -> FunctionCategory { |
| 20 | + FunctionCategory::Object |
| 21 | + } |
| 22 | + |
| 23 | + fn min_args(&self) -> usize { |
| 24 | + 0 |
| 25 | + } |
| 26 | + |
| 27 | + fn max_args(&self) -> usize { |
| 28 | + usize::MAX |
| 29 | + } |
| 30 | + |
| 31 | + fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { |
| 32 | + vec![ |
| 33 | + AcceptedArgKind::Array, |
| 34 | + AcceptedArgKind::Boolean, |
| 35 | + AcceptedArgKind::Number, |
| 36 | + AcceptedArgKind::Object, |
| 37 | + AcceptedArgKind::String, |
| 38 | + ] |
| 39 | + } |
| 40 | + |
| 41 | + fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 42 | + debug!("{}", t!("functions.createObject.invoked")); |
| 43 | + |
| 44 | + if args.len() % 2 != 0 { |
| 45 | + return Err(DscError::Parser(t!("functions.createObject.argsMustBePairs").to_string())); |
| 46 | + } |
| 47 | + |
| 48 | + let mut object_result = Map::<String, Value>::new(); |
| 49 | + |
| 50 | + for chunk in args.chunks(2) { |
| 51 | + let key = &chunk[0]; |
| 52 | + let value = &chunk[1]; |
| 53 | + |
| 54 | + if !key.is_string() { |
| 55 | + return Err(DscError::Parser(t!("functions.createObject.keyMustBeString").to_string())); |
| 56 | + } |
| 57 | + |
| 58 | + let key_str = key.as_str().unwrap().to_string(); |
| 59 | + object_result.insert(key_str, value.clone()); |
| 60 | + } |
| 61 | + |
| 62 | + Ok(Value::Object(object_result)) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(test)] |
| 67 | +mod tests { |
| 68 | + use crate::configure::context::Context; |
| 69 | + use crate::parser::Statement; |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn simple_object() { |
| 73 | + let mut parser = Statement::new().unwrap(); |
| 74 | + let result = parser.parse_and_execute("[createObject('name', 'test')]", &Context::new()).unwrap(); |
| 75 | + assert_eq!(result.to_string(), r#"{"name":"test"}"#); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn multiple_properties() { |
| 80 | + let mut parser = Statement::new().unwrap(); |
| 81 | + let result = parser.parse_and_execute("[createObject('name', 'test', 'value', 42)]", &Context::new()).unwrap(); |
| 82 | + assert_eq!(result.to_string(), r#"{"name":"test","value":42}"#); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn mixed_value_types() { |
| 87 | + let mut parser = Statement::new().unwrap(); |
| 88 | + let result = parser.parse_and_execute("[createObject('string', 'hello', 'number', 123, 'boolean', true)]", &Context::new()).unwrap(); |
| 89 | + |
| 90 | + let json: serde_json::Value = serde_json::from_str(&result.to_string()).unwrap(); |
| 91 | + assert_eq!(json["string"], "hello"); |
| 92 | + assert_eq!(json["number"], 123); |
| 93 | + assert_eq!(json["boolean"], true); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn nested_objects() { |
| 98 | + let mut parser = Statement::new().unwrap(); |
| 99 | + let result = parser.parse_and_execute("[createObject('outer', createObject('inner', 'value'))]", &Context::new()).unwrap(); |
| 100 | + assert_eq!(result.to_string(), r#"{"outer":{"inner":"value"}}"#); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn with_arrays() { |
| 105 | + let mut parser = Statement::new().unwrap(); |
| 106 | + let result = parser.parse_and_execute("[createObject('items', createArray('a', 'b', 'c'))]", &Context::new()).unwrap(); |
| 107 | + assert_eq!(result.to_string(), r#"{"items":["a","b","c"]}"#); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn odd_number_of_args() { |
| 112 | + let mut parser = Statement::new().unwrap(); |
| 113 | + let result = parser.parse_and_execute("[createObject('name')]", &Context::new()); |
| 114 | + assert!(result.is_err()); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn non_string_key() { |
| 119 | + let mut parser = Statement::new().unwrap(); |
| 120 | + let result = parser.parse_and_execute("[createObject(123, 'value')]", &Context::new()); |
| 121 | + assert!(result.is_err()); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn empty() { |
| 126 | + let mut parser = Statement::new().unwrap(); |
| 127 | + let result = parser.parse_and_execute("[createObject()]", &Context::new()).unwrap(); |
| 128 | + assert_eq!(result.to_string(), "{}"); |
| 129 | + } |
| 130 | +} |
0 commit comments