Skip to content

Commit

Permalink
feat(rad): first approach to ArrayJoin
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Nov 14, 2023
1 parent b3322b1 commit 88c8674
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
34 changes: 34 additions & 0 deletions rad/src/operators/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,40 @@ fn get_numeric_string(input: &RadonArray, args: &[Value]) -> Result<RadonString,
)))
}

pub fn join(
input: &RadonArray,
args: &[Value]
) -> Result<RadonTypes, RadError> {
// 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::<String>(args[0].to_owned()).unwrap_or_default()
} else {
String::from("")
};
match input.value().first() {
Some(RadonTypes::String(_)) => {
let string_list: Vec<String> = 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],
Expand Down
2 changes: 1 addition & 1 deletion rad/src/operators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions rad/src/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl Operable for RadonArray {
array_operators::get::<RadonString, _>(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),
Expand Down

0 comments on commit 88c8674

Please sign in to comment.