Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add max function #375

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions dsc_lib/src/functions/max.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::DscError;
use crate::configure::context::Context;
use crate::functions::{AcceptedArgKind, Function};
use serde_json::Value;
use tracing::debug;

#[derive(Debug, Default)]
pub struct Max {}

impl Function for Max {
fn min_args(&self) -> usize {
1
}

fn max_args(&self) -> usize {
usize::MAX
}

fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> {
vec![AcceptedArgKind::Number, AcceptedArgKind::Array]
}

fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
debug!("max function");
if args.len() == 1 {
if let Some(array) = args[0].as_array() {
find_max(array)
}
else {
Err(DscError::Parser("Array cannot be empty".to_string()))
}
}
else {
find_max(args)
}
}
}

fn find_max(args: &[Value]) -> Result<Value, DscError> {
let array = args.iter().map(|v| v.as_i64().ok_or(DscError::Parser("Input must only contain integers".to_string()))).collect::<Result<Vec<i64>, DscError>>()?;
let value = array.iter().max().ok_or(DscError::Parser("Unable to find max value".to_string()))?;
Ok(Value::Number((*value).into()))
}

#[cfg(test)]
mod tests {
use crate::configure::context::Context;
use crate::parser::Statement;

#[test]
fn list() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(3,2,5,4)]", &Context::new()).unwrap();
assert_eq!(result, 5);
}

#[test]
fn list_with_spaces() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(3, 2, 5, 4)]", &Context::new()).unwrap();
assert_eq!(result, 5);
}

#[test]
fn array() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(createArray(0, 3, 2, 7, 4)]", &Context::new()).unwrap();
assert_eq!(result, 7);
}

#[test]
fn array_single_value() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(createArray(0)]", &Context::new()).unwrap();
assert_eq!(result, 0);
}

#[test]
fn arrays() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(createArray(0, 3), createArray(2, 5))]", &Context::new());
assert!(result.is_err());
}

#[test]
fn string_and_numbers() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max('a', 1)]", &Context::new());
assert!(result.is_err());
}

#[test]
fn nested() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(-10, max(-2, -9), -5)]", &Context::new()).unwrap();
assert_eq!(result, -2);
}

#[test]
fn int_and_array() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(1, createArray(0,2))]", &Context::new());
assert!(result.is_err());
}

#[test]
fn array_and_int() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(createArray(0,2), 1)]", &Context::new());
assert!(result.is_err());
}
}
2 changes: 2 additions & 0 deletions dsc_lib/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod concat;
pub mod create_array;
pub mod div;
pub mod envvar;
pub mod max;
pub mod min;
pub mod mod_function;
pub mod mul;
Expand Down Expand Up @@ -67,6 +68,7 @@ impl FunctionDispatcher {
functions.insert("createArray".to_string(), Box::new(create_array::CreateArray{}));
functions.insert("div".to_string(), Box::new(div::Div{}));
functions.insert("envvar".to_string(), Box::new(envvar::Envvar{}));
functions.insert("max".to_string(), Box::new(max::Max{}));
functions.insert("min".to_string(), Box::new(min::Min{}));
functions.insert("mod".to_string(), Box::new(mod_function::Mod{}));
functions.insert("mul".to_string(), Box::new(mul::Mul{}));
Expand Down
Loading