|
| 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 LastIndexOf {} |
| 13 | + |
| 14 | +impl Function for LastIndexOf { |
| 15 | + fn get_metadata(&self) -> FunctionMetadata { |
| 16 | + FunctionMetadata { |
| 17 | + name: "lastIndexOf".to_string(), |
| 18 | + description: t!("functions.lastIndexOf.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.lastIndexOf.invoked")); |
| 33 | + |
| 34 | + let Some(array) = args[0].as_array() else { |
| 35 | + return Err(DscError::Parser(t!("functions.lastIndexOf.invalidArrayArg").to_string())); |
| 36 | + }; |
| 37 | + |
| 38 | + let item_to_find = &args[1]; |
| 39 | + |
| 40 | + if let Some(pos) = array.iter().rposition(|v| v == item_to_find) { |
| 41 | + let index_i64 = i64::try_from(pos).map_err(|_| { |
| 42 | + DscError::Parser("Array index too large to represent as integer".to_string()) |
| 43 | + })?; |
| 44 | + return Ok(Value::Number(index_i64.into())); |
| 45 | + } |
| 46 | + |
| 47 | + Ok(Value::Number((-1i64).into())) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod tests { |
| 53 | + use crate::configure::context::Context; |
| 54 | + use crate::parser::Statement; |
| 55 | + |
| 56 | + #[test] |
| 57 | + fn finds_last_occurrence_string() { |
| 58 | + let mut parser = Statement::new().unwrap(); |
| 59 | + let result = parser.parse_and_execute("[lastIndexOf(createArray('a','b','a','c'), 'a')]", &Context::new()).unwrap(); |
| 60 | + assert_eq!(result, 2); |
| 61 | + } |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn finds_last_occurrence_number() { |
| 65 | + let mut parser = Statement::new().unwrap(); |
| 66 | + let result = parser.parse_and_execute("[lastIndexOf(createArray(10,20,30,20), 20)]", &Context::new()).unwrap(); |
| 67 | + assert_eq!(result, 3); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn not_found_returns_minus_one() { |
| 72 | + let mut parser = Statement::new().unwrap(); |
| 73 | + let result = parser.parse_and_execute("[lastIndexOf(createArray('x','y'), 'z')]", &Context::new()).unwrap(); |
| 74 | + assert_eq!(result, -1); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn finds_last_occurrence_array() { |
| 79 | + let mut parser = Statement::new().unwrap(); |
| 80 | + let result = parser.parse_and_execute( |
| 81 | + "[lastIndexOf(createArray(createArray('a','b'), createArray('c','d'), createArray('a','b')), createArray('a','b'))]", |
| 82 | + &Context::new(), |
| 83 | + ) |
| 84 | + .unwrap(); |
| 85 | + assert_eq!(result, 2); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn finds_last_occurrence_nested_array() { |
| 90 | + let mut parser = Statement::new().unwrap(); |
| 91 | + let result = parser |
| 92 | + .parse_and_execute( |
| 93 | + "[lastIndexOf(createArray(createArray(1,2), createArray(3,4), createArray(1,2)), createArray(1,2))]", |
| 94 | + &Context::new(), |
| 95 | + ) |
| 96 | + .unwrap(); |
| 97 | + assert_eq!(result, 2); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn finds_last_occurrence_object() { |
| 102 | + let mut parser = Statement::new().unwrap(); |
| 103 | + let result = parser.parse_and_execute( |
| 104 | + "[lastIndexOf(createArray(createObject('name','John'), createObject('name','Jane'), createObject('name','John')), createObject('name','John'))]", |
| 105 | + &Context::new(), |
| 106 | + ) |
| 107 | + .unwrap(); |
| 108 | + assert_eq!(result, 2); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn finds_object_regardless_of_property_order() { |
| 113 | + let mut parser = Statement::new().unwrap(); |
| 114 | + let result = parser |
| 115 | + .parse_and_execute( |
| 116 | + "[lastIndexOf(createArray(createObject('a',1,'b',2), createObject('b',2,'a',1)), createObject('a',1,'b',2))]", |
| 117 | + &Context::new(), |
| 118 | + ) |
| 119 | + .unwrap(); |
| 120 | + assert_eq!(result, 1); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn mismatched_types_do_not_match() { |
| 125 | + let mut parser = Statement::new().unwrap(); |
| 126 | + let result = parser |
| 127 | + .parse_and_execute("[lastIndexOf(createArray('1','2','3'), 1)]", &Context::new()) |
| 128 | + .unwrap(); |
| 129 | + assert_eq!(result, -1); |
| 130 | + |
| 131 | + let result = parser |
| 132 | + .parse_and_execute("[lastIndexOf(createArray(1,2,3), '1')]", &Context::new()) |
| 133 | + .unwrap(); |
| 134 | + assert_eq!(result, -1); |
| 135 | + } |
| 136 | +} |
0 commit comments