|
| 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) -> String { |
| 15 | + if let Some(s) = v.as_str() { return s.to_string(); } |
| 16 | + if let Some(n) = v.as_i64() { return n.to_string(); } |
| 17 | + if let Some(b) = v.as_bool() { return b.to_string(); } |
| 18 | + if v.is_null() { return "null".to_string(); } |
| 19 | + // Fallback to JSON for arrays/objects or other numbers |
| 20 | + serde_json::to_string(v).unwrap_or_default() |
| 21 | +} |
| 22 | + |
| 23 | +impl Function for Join { |
| 24 | + fn get_metadata(&self) -> FunctionMetadata { |
| 25 | + FunctionMetadata { |
| 26 | + name: "join".to_string(), |
| 27 | + description: t!("functions.join.description").to_string(), |
| 28 | + category: FunctionCategory::String, |
| 29 | + min_args: 2, |
| 30 | + max_args: 2, |
| 31 | + accepted_arg_ordered_types: vec![ |
| 32 | + vec![FunctionArgKind::Array, FunctionArgKind::String], |
| 33 | + // delimiter: accept any type (no validation), convert to string |
| 34 | + vec![ |
| 35 | + FunctionArgKind::Array, |
| 36 | + FunctionArgKind::Boolean, |
| 37 | + FunctionArgKind::Null, |
| 38 | + FunctionArgKind::Number, |
| 39 | + FunctionArgKind::Object, |
| 40 | + FunctionArgKind::String, |
| 41 | + ], |
| 42 | + ], |
| 43 | + remaining_arg_accepted_types: None, |
| 44 | + return_types: vec![FunctionArgKind::String], |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 49 | + debug!("{}", t!("functions.join.invoked")); |
| 50 | + |
| 51 | + let delimiter = stringify_value(&args[1]); |
| 52 | + |
| 53 | + if let Some(array) = args[0].as_array() { |
| 54 | + let items: Vec<String> = array.iter().map(stringify_value).collect(); |
| 55 | + return Ok(Value::String(items.join(&delimiter))); |
| 56 | + } |
| 57 | + |
| 58 | + if let Some(s) = args[0].as_str() { |
| 59 | + // Edge case: empty string => empty string |
| 60 | + if s.is_empty() { return Ok(Value::String(String::new())); } |
| 61 | + let items: Vec<String> = s.chars().map(|c| c.to_string()).collect(); |
| 62 | + return Ok(Value::String(items.join(&delimiter))); |
| 63 | + } |
| 64 | + |
| 65 | + Err(DscError::Parser(t!("functions.join.invalidInputType").to_string())) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(test)] |
| 70 | +mod tests { |
| 71 | + use crate::configure::context::Context; |
| 72 | + use crate::parser::Statement; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn join_array_of_strings() { |
| 76 | + let mut parser = Statement::new().unwrap(); |
| 77 | + let result = parser.parse_and_execute("[join(createArray('a','b','c'), '-')]", &Context::new()).unwrap(); |
| 78 | + assert_eq!(result, "a-b-c"); |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn join_string_chars() { |
| 83 | + let mut parser = Statement::new().unwrap(); |
| 84 | + let result = parser.parse_and_execute("[join('abc', '-')]", &Context::new()).unwrap(); |
| 85 | + assert_eq!(result, "a-b-c"); |
| 86 | + } |
| 87 | +} |
0 commit comments