Skip to content
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
3 changes: 3 additions & 0 deletions dsc_lib/locales/en-us.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ divideByZero = "Cannot divide by zero"
[functions.envvar]
notFound = "Environment variable not found"

[functions.if]
conditionNotBoolean = "Condition is not a boolean"

[functions.int]
invalidInput = "invalid input string"
parseStringError = "unable to parse string to int"
Expand Down
65 changes: 65 additions & 0 deletions dsc_lib/src/functions/if.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::DscError;
use crate::configure::context::Context;
use crate::functions::AcceptedArgKind;
use super::Function;
use rust_i18n::t;
use serde_json::Value;

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

impl Function for If {
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> {
vec![AcceptedArgKind::Boolean, AcceptedArgKind::String, AcceptedArgKind::Number, AcceptedArgKind::Array, AcceptedArgKind::Object]
}

fn min_args(&self) -> usize {
3
}

fn max_args(&self) -> usize {
3
}

fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
let Some(condition) = args[0].as_bool() else {
return Err(DscError::Function("if".to_string(), t!("functions.if.conditionNotBoolean").to_string()));
};

if condition {
Ok(args[1].clone())
} else {
Ok(args[2].clone())
}
}
}

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

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

#[test]
fn condition_true() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[if(true, 'left', 'right')]", &Context::new()).unwrap();
assert_eq!(result, "left");
}

#[test]
fn condition_false() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[if(false, 'left', 'right')]", &Context::new()).unwrap();
assert_eq!(result, "right");
}
}
2 changes: 2 additions & 0 deletions dsc_lib/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod create_array;
pub mod div;
pub mod envvar;
pub mod equals;
pub mod r#if;
pub mod int;
pub mod max;
pub mod min;
Expand Down Expand Up @@ -75,6 +76,7 @@ impl FunctionDispatcher {
functions.insert("div".to_string(), Box::new(div::Div{}));
functions.insert("envvar".to_string(), Box::new(envvar::Envvar{}));
functions.insert("equals".to_string(), Box::new(equals::Equals{}));
functions.insert("if".to_string(), Box::new(r#if::If{}));
functions.insert("int".to_string(), Box::new(int::Int{}));
functions.insert("max".to_string(), Box::new(max::Max{}));
functions.insert("min".to_string(), Box::new(min::Min{}));
Expand Down
Loading