|
| 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 TryGet {} |
| 13 | + |
| 14 | +impl Function for TryGet { |
| 15 | + fn get_metadata(&self) -> FunctionMetadata { |
| 16 | + FunctionMetadata { |
| 17 | + name: "tryGet".to_string(), |
| 18 | + description: t!("functions.tryGet.description").to_string(), |
| 19 | + category: vec![FunctionCategory::Array, FunctionCategory::Object], |
| 20 | + min_args: 2, |
| 21 | + max_args: 2, |
| 22 | + accepted_arg_ordered_types: vec![ |
| 23 | + vec![FunctionArgKind::Array, FunctionArgKind::Object], |
| 24 | + vec![FunctionArgKind::Number, FunctionArgKind::String], |
| 25 | + ], |
| 26 | + remaining_arg_accepted_types: None, |
| 27 | + return_types: vec![FunctionArgKind::Array, FunctionArgKind::Boolean, FunctionArgKind::Null, FunctionArgKind::Number, FunctionArgKind::Object, FunctionArgKind::String], |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 32 | + debug!("{}", t!("functions.tryGet.invoked")); |
| 33 | + |
| 34 | + if let Some(object) = args[0].as_object() { |
| 35 | + if let Some(key) = args[1].as_str() { |
| 36 | + if let Some(value) = object.get(key) { |
| 37 | + return Ok(value.clone()); |
| 38 | + } |
| 39 | + return Ok(Value::Null); |
| 40 | + } |
| 41 | + return Err(DscError::Parser(t!("functions.tryGet.invalidKeyType").to_string())); |
| 42 | + } |
| 43 | + |
| 44 | + if let Some(array) = args[0].as_array() { |
| 45 | + if let Some(index) = args[1].as_i64() { |
| 46 | + let Ok(index) = usize::try_from(index) else { |
| 47 | + // handle negative index |
| 48 | + return Ok(Value::Null); |
| 49 | + }; |
| 50 | + let index = if index >= array.len() { |
| 51 | + return Ok(Value::Null); |
| 52 | + } else { |
| 53 | + index |
| 54 | + }; |
| 55 | + return Ok(array[index].clone()); |
| 56 | + } |
| 57 | + return Err(DscError::Parser(t!("functions.tryGet.invalidIndexType").to_string())); |
| 58 | + } |
| 59 | + |
| 60 | + Err(DscError::Parser(t!("functions.invalidArgType").to_string())) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use crate::configure::context::Context; |
| 67 | + use crate::parser::Statement; |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn try_get_object() { |
| 71 | + let mut parser = Statement::new().unwrap(); |
| 72 | + let result = parser.parse_and_execute("[tryGet(createObject('key1', 'value1'), 'key1')]", &Context::new()).unwrap(); |
| 73 | + assert_eq!(result, serde_json::json!("value1")); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn try_get_object_not_found() { |
| 78 | + let mut parser = Statement::new().unwrap(); |
| 79 | + let result = parser.parse_and_execute("[tryGet(createObject('key1', 'value1'), 'key2')]", &Context::new()).unwrap(); |
| 80 | + assert_eq!(result, serde_json::json!(null)); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn try_get_array() { |
| 85 | + let mut parser = Statement::new().unwrap(); |
| 86 | + let result = parser.parse_and_execute("[tryGet(createArray('value1', 'value2'), 1)]", &Context::new()).unwrap(); |
| 87 | + assert_eq!(result, serde_json::json!("value2")); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn try_get_array_negative_index() { |
| 92 | + let mut parser = Statement::new().unwrap(); |
| 93 | + let result = parser.parse_and_execute("[tryGet(createArray('value1', 'value2'), -1)]", &Context::new()).unwrap(); |
| 94 | + assert_eq!(result, serde_json::json!(null)); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn try_get_array_index_not_found() { |
| 99 | + let mut parser = Statement::new().unwrap(); |
| 100 | + let result = parser.parse_and_execute("[tryGet(createArray('value1', 'value2'), 2)]", &Context::new()).unwrap(); |
| 101 | + assert_eq!(result, serde_json::json!(null)); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn try_get_object_invalid_key_type() { |
| 106 | + let mut parser = Statement::new().unwrap(); |
| 107 | + let result = parser.parse_and_execute("[tryGet(createObject('key1', 'value1'), 1)]", &Context::new()); |
| 108 | + assert!(result.is_err()); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn try_get_array_invalid_index_type() { |
| 113 | + let mut parser = Statement::new().unwrap(); |
| 114 | + let result = parser.parse_and_execute("[tryGet(createArray('value1', 'value2'), '1')]", &Context::new()); |
| 115 | + assert!(result.is_err()); |
| 116 | + } |
| 117 | +} |
0 commit comments