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 min function #361

Merged
merged 7 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 115 additions & 0 deletions dsc_lib/src/functions/min.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 Min {}

impl Function for Min {
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]
SteveL-MSFT marked this conversation as resolved.
Show resolved Hide resolved
}

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

fn find_min(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().min().ok_or(DscError::Parser("Unable to find min 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("[min(3,2,5,4)]", &Context::new()).unwrap();
tgauth marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(result, 2);
}

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

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

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

#[test]
fn arrays() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[min(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("[min('a', 1)]", &Context::new());
assert!(result.is_err());
}

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

#[test]
fn int_and_array() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[min(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("[min(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 min;
pub mod mod_function;
pub mod mul;
pub mod parameters;
Expand Down Expand Up @@ -66,6 +67,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("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{}));
functions.insert("parameters".to_string(), Box::new(parameters::Parameters{}));
Expand Down