diff --git a/rad/src/operators/array.rs b/rad/src/operators/array.rs index b4aac9674..c86f7778e 100644 --- a/rad/src/operators/array.rs +++ b/rad/src/operators/array.rs @@ -109,6 +109,40 @@ fn get_numeric_string(input: &RadonArray, args: &[Value]) -> Result Result { + // Join not applicable if the input array is not homogeneous + if !input.is_homogeneous() { + return Err(RadError::UnsupportedOpNonHomogeneous { + operator: "ArrayJoin".to_string(), + }); + } + let separator = if args.len() > 0 { + from_value::(args[0].to_owned()).unwrap_or_default() + } else { + String::from("") + }; + match input.value().first() { + Some(RadonTypes::String(_)) => { + let string_list: Vec = input.value().into_iter().map(|item| + RadonString::try_from(item).unwrap_or_default().value() + ).collect(); + Ok(RadonTypes::from(RadonString::from(string_list.join(separator.as_str())))) + Some(first_item) => { + return Err(RadError::UnsupportedOperator { + input_type: first_item.radon_type_name().to_string(), + operator: "ArrayJoin".to_string(), + args: Some(args.to_vec()) + }); + } + _ => { + return Err(RadError::EmptyArray) + } + } +} + pub fn map( input: &RadonArray, args: &[Value], diff --git a/rad/src/operators/mod.rs b/rad/src/operators/mod.rs index 32d6961c1..b1889c241 100644 --- a/rad/src/operators/mod.rs +++ b/rad/src/operators/mod.rs @@ -29,7 +29,7 @@ pub enum RadonOpCodes { // Array operator codes (start at 0x10) ArrayCount = 0x10, ArrayFilter = 0x11, - // ArrayFlatten = 0x12, + ArrayJoin = 0x12, ArrayGetArray = 0x13, ArrayGetBoolean = 0x14, ArrayGetBytes = 0x15, diff --git a/rad/src/types/array.rs b/rad/src/types/array.rs index 1c769e308..ce888f7f0 100644 --- a/rad/src/types/array.rs +++ b/rad/src/types/array.rs @@ -166,6 +166,7 @@ impl Operable for RadonArray { array_operators::get::(self, args).map(RadonTypes::from) } (RadonOpCodes::ArrayFilter, Some(args)) => array_operators::filter(self, args, context), + (RadonOpCodes::ArrayJoin, Some(args)) => array_operators::join(self, args), (RadonOpCodes::ArrayMap, Some(args)) => array_operators::map(self, args, context), (RadonOpCodes::ArrayReduce, Some(args)) => array_operators::reduce(self, args, context), (RadonOpCodes::ArraySort, Some(args)) => array_operators::sort(self, args, context),