| 29 | } |
| 30 | |
| 31 | fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 32 | debug!("{}", t!("functions.union.invoked")); |
| 33 | if args[0].is_array() { |
| 34 | let mut result = Vec::new(); |
| 35 | // iterate through array and skip elements that are already in result |
| 36 | for arg in args { |
| 37 | if let Some(array) = arg.as_array() { |
| 38 | for item in array { |
| 39 | if !result.contains(item) { |
| 40 | result.push(item.clone()); |
| 41 | } |
| 42 | } |
| 43 | } else { |
| 44 | return Err(DscError::Parser(t!("functions.union.invalidArgType").to_string())); |
| 45 | } |
| 46 | } |
| 47 | return Ok(Value::Array(result)); |
| 48 | } |
| 49 | |
| 50 | if args[0].is_object() { |
| 51 | let mut result = Map::new(); |
| 52 | // iterate through objects, duplicate keys are overwritten |
| 53 | for arg in args { |
| 54 | if let Some(object) = arg.as_object() { |
| 55 | for (key, value) in object { |
| 56 | result.insert(key.clone(), value.clone()); |
| 57 | } |
| 58 | } else { |
| 59 | return Err(DscError::Parser(t!("functions.union.invalidArgType").to_string())); |
| 60 | } |
| 61 | } |
| 62 | return Ok(Value::Object(result)); |
| 63 | } |
| 64 | |
| 65 | Err(DscError::Parser(t!("functions.union.invalidArgType").to_string())) |
| 66 | } |
| 67 | } |