|
| 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}; |
| 7 | +use rust_i18n::t; |
| 8 | +use serde_json::Value; |
| 9 | +use tracing::debug; |
| 10 | + |
| 11 | +#[derive(Debug, Default)] |
| 12 | +pub struct Join {} |
| 13 | + |
| 14 | +fn stringify_value(v: &Value) -> Result<String, DscError> { |
| 15 | + match v { |
| 16 | + Value::String(s) => Ok(s.clone()), |
| 17 | + Value::Number(n) => Ok(n.to_string()), |
| 18 | + Value::Bool(b) => Ok(b.to_string()), |
| 19 | + Value::Null => Err(DscError::Parser(t!("functions.join.invalidNullElement").to_string())), |
| 20 | + Value::Array(_) => Err(DscError::Parser(t!("functions.join.invalidArrayElement").to_string())), |
| 21 | + Value::Object(_) => Err(DscError::Parser(t!("functions.join.invalidObjectElement").to_string())), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl Function for Join { |
| 26 | + fn get_metadata(&self) -> FunctionMetadata { |
| 27 | + FunctionMetadata { |
| 28 | + name: "join".to_string(), |
| 29 | + description: t!("functions.join.description").to_string(), |
| 30 | + category: FunctionCategory::String, |
| 31 | + min_args: 2, |
| 32 | + max_args: 2, |
| 33 | + accepted_arg_ordered_types: vec![ |
| 34 | + vec![FunctionArgKind::Array], |
| 35 | + vec![FunctionArgKind::String], |
| 36 | + ], |
| 37 | + remaining_arg_accepted_types: None, |
| 38 | + return_types: vec![FunctionArgKind::String], |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 43 | + debug!("{}", t!("functions.join.invoked")); |
| 44 | + |
| 45 | + let delimiter = args[1].as_str().unwrap(); |
| 46 | + |
| 47 | + if let Some(array) = args[0].as_array() { |
| 48 | + let items: Result<Vec<String>, DscError> = array.iter().map(stringify_value).collect(); |
| 49 | + let items = items?; |
| 50 | + return Ok(Value::String(items.join(delimiter))); |
| 51 | + } |
| 52 | + |
| 53 | + Err(DscError::Parser(t!("functions.join.invalidArrayArg").to_string())) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +mod tests { |
| 59 | + use crate::configure::context::Context; |
| 60 | + use crate::parser::Statement; |
| 61 | + use super::Join; |
| 62 | + use crate::functions::Function; |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn join_array_of_strings() { |
| 66 | + let mut parser = Statement::new().unwrap(); |
| 67 | + let result = parser.parse_and_execute("[join(createArray('a','b','c'), '-')]", &Context::new()).unwrap(); |
| 68 | + assert_eq!(result, "a-b-c"); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn join_empty_array_returns_empty() { |
| 73 | + let mut parser = Statement::new().unwrap(); |
| 74 | + let result = parser.parse_and_execute("[join(createArray(), '-')]", &Context::new()).unwrap(); |
| 75 | + assert_eq!(result, ""); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn join_array_of_integers() { |
| 80 | + let mut parser = Statement::new().unwrap(); |
| 81 | + let result = parser.parse_and_execute("[join(createArray(1,2,3), ',')]", &Context::new()).unwrap(); |
| 82 | + assert_eq!(result, "1,2,3"); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn join_array_with_null_fails() { |
| 87 | + let mut parser = Statement::new().unwrap(); |
| 88 | + let result = parser.parse_and_execute("[join(createArray('a', null()), ',')]", &Context::new()); |
| 89 | + assert!(result.is_err()); |
| 90 | + // The error comes from argument validation, not our function |
| 91 | + assert!(result.unwrap_err().to_string().contains("does not accept null arguments")); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn join_array_with_array_fails() { |
| 96 | + let mut parser = Statement::new().unwrap(); |
| 97 | + let result = parser.parse_and_execute("[join(createArray('a', createArray('b')), ',')]", &Context::new()); |
| 98 | + assert!(result.is_err()); |
| 99 | + let error_msg = result.unwrap_err().to_string(); |
| 100 | + assert!(error_msg.contains("Arguments must all be arrays") || error_msg.contains("mixed types")); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn join_array_with_object_fails() { |
| 105 | + let mut parser = Statement::new().unwrap(); |
| 106 | + let result = parser.parse_and_execute("[join(createArray('a', createObject('key', 'value')), ',')]", &Context::new()); |
| 107 | + assert!(result.is_err()); |
| 108 | + let error_msg = result.unwrap_err().to_string(); |
| 109 | + assert!(error_msg.contains("Arguments must all be") || error_msg.contains("mixed types")); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn join_direct_test_with_mixed_array() { |
| 114 | + use serde_json::json; |
| 115 | + use crate::configure::context::Context; |
| 116 | + |
| 117 | + let join_fn = Join::default(); |
| 118 | + let args = vec![ |
| 119 | + json!(["hello", {"key": "value"}]), // Array with string and object |
| 120 | + json!(",") |
| 121 | + ]; |
| 122 | + let result = join_fn.invoke(&args, &Context::new()); |
| 123 | + |
| 124 | + assert!(result.is_err()); |
| 125 | + let error_msg = result.unwrap_err().to_string(); |
| 126 | + assert!(error_msg.contains("Array elements cannot be objects")); |
| 127 | + } |
| 128 | +} |
0 commit comments