|
| 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 IndexOf {} |
| 13 | + |
| 14 | +impl Function for IndexOf { |
| 15 | + fn get_metadata(&self) -> FunctionMetadata { |
| 16 | + FunctionMetadata { |
| 17 | + name: "indexOf".to_string(), |
| 18 | + description: t!("functions.indexOf.description").to_string(), |
| 19 | + category: FunctionCategory::Array, |
| 20 | + min_args: 2, |
| 21 | + max_args: 2, |
| 22 | + accepted_arg_ordered_types: vec![ |
| 23 | + vec![FunctionArgKind::Array], |
| 24 | + vec![FunctionArgKind::String, FunctionArgKind::Number, FunctionArgKind::Array, FunctionArgKind::Object], |
| 25 | + ], |
| 26 | + remaining_arg_accepted_types: None, |
| 27 | + return_types: vec![FunctionArgKind::Number], |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 32 | + debug!("{}", t!("functions.indexOf.invoked")); |
| 33 | + |
| 34 | + let Some(array) = args[0].as_array() else { |
| 35 | + return Err(DscError::Parser(t!("functions.indexOf.invalidArrayArg").to_string())); |
| 36 | + }; |
| 37 | + |
| 38 | + let item_to_find = &args[1]; |
| 39 | + |
| 40 | + for (index, item) in array.iter().enumerate() { |
| 41 | + if item == item_to_find { |
| 42 | + let index_i64 = i64::try_from(index).map_err(|_| { |
| 43 | + DscError::Parser("Array index too large to represent as integer".to_string()) |
| 44 | + })?; |
| 45 | + return Ok(Value::Number(index_i64.into())); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Not found is -1 |
| 50 | + Ok(Value::Number((-1i64).into())) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use crate::configure::context::Context; |
| 57 | + use crate::parser::Statement; |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn find_string_in_array() { |
| 61 | + let mut parser = Statement::new().unwrap(); |
| 62 | + let result = parser.parse_and_execute("[indexOf(createArray('apple', 'banana', 'cherry'), 'banana')]", &Context::new()).unwrap(); |
| 63 | + assert_eq!(result, 1); |
| 64 | + } |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn find_number_in_array() { |
| 68 | + let mut parser = Statement::new().unwrap(); |
| 69 | + let result = parser.parse_and_execute("[indexOf(createArray(10, 20, 30), 20)]", &Context::new()).unwrap(); |
| 70 | + assert_eq!(result, 1); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn find_first_occurrence() { |
| 75 | + let mut parser = Statement::new().unwrap(); |
| 76 | + let result = parser.parse_and_execute("[indexOf(createArray('a', 'b', 'a', 'c'), 'a')]", &Context::new()).unwrap(); |
| 77 | + assert_eq!(result, 0); |
| 78 | + } |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn item_not_found() { |
| 82 | + let mut parser = Statement::new().unwrap(); |
| 83 | + let result = parser.parse_and_execute("[indexOf(createArray('apple', 'banana'), 'orange')]", &Context::new()).unwrap(); |
| 84 | + assert_eq!(result, -1); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn case_sensitive_string() { |
| 89 | + let mut parser = Statement::new().unwrap(); |
| 90 | + let result = parser.parse_and_execute("[indexOf(createArray('Apple', 'Banana'), 'apple')]", &Context::new()).unwrap(); |
| 91 | + assert_eq!(result, -1); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn find_array_in_array() { |
| 96 | + let mut parser = Statement::new().unwrap(); |
| 97 | + let result = parser.parse_and_execute("[indexOf(createArray(createArray('a', 'b'), createArray('c', 'd')), createArray('c', 'd'))]", &Context::new()).unwrap(); |
| 98 | + assert_eq!(result, 1); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn find_object_in_array() { |
| 103 | + let mut parser = Statement::new().unwrap(); |
| 104 | + let result = parser.parse_and_execute("[indexOf(createArray(createObject('name', 'John'), createObject('name', 'Jane')), createObject('name', 'Jane'))]", &Context::new()).unwrap(); |
| 105 | + assert_eq!(result, 1); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn empty_array() { |
| 110 | + let mut parser = Statement::new().unwrap(); |
| 111 | + let result = parser.parse_and_execute("[indexOf(createArray(), 'test')]", &Context::new()).unwrap(); |
| 112 | + assert_eq!(result, -1); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn invalid_array_arg() { |
| 117 | + let mut parser = Statement::new().unwrap(); |
| 118 | + let result = parser.parse_and_execute("[indexOf('not_an_array', 'test')]", &Context::new()); |
| 119 | + assert!(result.is_err()); |
| 120 | + } |
| 121 | +} |
0 commit comments