|
| 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::{AcceptedArgKind, Function, FunctionCategory}; |
| 7 | +use rust_i18n::t; |
| 8 | +use serde_json::Value; |
| 9 | +use tracing::debug; |
| 10 | + |
| 11 | +#[derive(Debug, Default)] |
| 12 | +pub struct Contains {} |
| 13 | + |
| 14 | +impl Function for Contains { |
| 15 | + fn description(&self) -> String { |
| 16 | + t!("functions.contains.description").to_string() |
| 17 | + } |
| 18 | + |
| 19 | + fn category(&self) -> FunctionCategory { |
| 20 | + FunctionCategory::Array |
| 21 | + } |
| 22 | + |
| 23 | + fn min_args(&self) -> usize { |
| 24 | + 2 |
| 25 | + } |
| 26 | + |
| 27 | + fn max_args(&self) -> usize { |
| 28 | + usize::MAX |
| 29 | + } |
| 30 | + |
| 31 | + fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { |
| 32 | + vec![AcceptedArgKind::Array, AcceptedArgKind::Object, AcceptedArgKind::String, AcceptedArgKind::Number] |
| 33 | + } |
| 34 | + |
| 35 | + fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 36 | + debug!("{}", t!("functions.contains.invoked")); |
| 37 | + let mut found = false; |
| 38 | + |
| 39 | + let (string_to_find, number_to_find) = if let Some(string) = args[1].as_str() { |
| 40 | + (string.to_string(), 0) |
| 41 | + } else if let Some(number) = args[1].as_i64() { |
| 42 | + (number.to_string(), number) |
| 43 | + } else { |
| 44 | + return Err(DscError::Parser(t!("functions.contains.invalidItemToFind").to_string())); |
| 45 | + }; |
| 46 | + |
| 47 | + // for array, we check if the string or number exists |
| 48 | + if let Some(array) = args[0].as_array() { |
| 49 | + for item in array { |
| 50 | + if let Some(item_str) = item.as_str() { |
| 51 | + if item_str == string_to_find { |
| 52 | + found = true; |
| 53 | + break; |
| 54 | + } |
| 55 | + } else if let Some(item_num) = item.as_i64() { |
| 56 | + if item_num == number_to_find { |
| 57 | + found = true; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return Ok(Value::Bool(found)); |
| 63 | + } |
| 64 | + |
| 65 | + // for object, we check if the key exists |
| 66 | + if let Some(object) = args[0].as_object() { |
| 67 | + // see if key exists |
| 68 | + for key in object.keys() { |
| 69 | + if key == &string_to_find { |
| 70 | + found = true; |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + return Ok(Value::Bool(found)); |
| 75 | + } |
| 76 | + |
| 77 | + // for string, we check if the string contains the substring or number |
| 78 | + if let Some(str) = args[0].as_str() { |
| 79 | + if str.contains(&string_to_find) { |
| 80 | + found = true; |
| 81 | + } |
| 82 | + return Ok(Value::Bool(found)); |
| 83 | + } |
| 84 | + |
| 85 | + Err(DscError::Parser(t!("functions.contains.invalidArgType").to_string())) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + use crate::configure::context::Context; |
| 92 | + use crate::parser::Statement; |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn string_contains_string() { |
| 96 | + let mut parser = Statement::new().unwrap(); |
| 97 | + let result = parser.parse_and_execute("[contains('hello', 'lo')]", &Context::new()).unwrap(); |
| 98 | + assert_eq!(result, true); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn string_does_not_contain_string() { |
| 103 | + let mut parser = Statement::new().unwrap(); |
| 104 | + let result = parser.parse_and_execute("[contains('hello', 'world')]", &Context::new()).unwrap(); |
| 105 | + assert_eq!(result, false); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn string_contains_number() { |
| 110 | + let mut parser = Statement::new().unwrap(); |
| 111 | + let result = parser.parse_and_execute("[contains('hello123', 123)]", &Context::new()).unwrap(); |
| 112 | + assert_eq!(result, true); |
| 113 | + } |
| 114 | +} |
| 115 | + |
0 commit comments