Skip to content

Commit

Permalink
Merge pull request #361 from tgauth/add-min-function
Browse files Browse the repository at this point in the history
add min function
  • Loading branch information
SteveL-MSFT authored Mar 13, 2024
2 parents aa3d968 + a018dde commit 289d902
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
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]
}

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();
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, -9), 3)]", &Context::new()).unwrap();
assert_eq!(result, -9);
}

#[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

0 comments on commit 289d902

Please sign in to comment.