-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also - add `value.is_truthy()` - add `#[serde(deny_unknown_fields)]` to Operation's
- Loading branch information
1 parent
2b898f7
commit 2a1aa9e
Showing
8 changed files
with
276 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use serde::Deserialize; | ||
|
||
use super::Operation; | ||
use crate::{Context, Engine, RenderError, Template, Value}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct IfOperation { | ||
#[serde(rename = "$if")] | ||
pub condition: Box<Template>, | ||
#[serde(rename = "then")] | ||
pub consequent: Option<Box<Template>>, | ||
#[serde(rename = "else")] | ||
pub alternative: Option<Box<Template>>, | ||
} | ||
|
||
impl Operation for IfOperation { | ||
fn render(&self, engine: &Engine, context: &Context) -> Result<Value, RenderError> { | ||
let condition = engine.render(&self.condition, context)?; | ||
|
||
let value: Value = if let Value::String(condition) = condition { | ||
engine.evaluate(&condition, context)? | ||
} else { | ||
condition | ||
}; | ||
|
||
if value.is_truthy() { | ||
if let Some(consequent) = &self.consequent { | ||
engine.render(consequent, context) | ||
} else { | ||
Ok(Value::Null) | ||
} | ||
} else { | ||
if let Some(alternative) = &self.alternative { | ||
engine.render(alternative, context) | ||
} else { | ||
Ok(Value::Null) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::error::Error; | ||
|
||
use super::*; | ||
use crate::{Number, Value}; | ||
|
||
use map_macro::btree_map; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn if_() -> Result<(), Box<dyn Error>> { | ||
let content = r#" | ||
zero: | ||
$if: ten > five | ||
then: | ||
$eval: five | ||
else: | ||
$eval: ten | ||
"#; | ||
let template: Template = serde_yaml::from_str(content)?; | ||
|
||
let engine = Engine::default(); | ||
let mut context = Context::new(); | ||
context.insert("five", Value::Number(Number::Signed(5))); | ||
context.insert("ten", Value::Number(Number::Signed(10))); | ||
|
||
let actual: Value = engine.render(&template, &context)?; | ||
|
||
let expected: Value = Value::Object(btree_map! { | ||
"zero".into() => Value::Number(Number::Signed(5)) | ||
}); | ||
|
||
assert_eq!(expected, actual); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
- title: $if-then-else, true | ||
context: {cond: true} | ||
template: {$if: 'cond', then: 1, else: 2} | ||
output: 1 | ||
|
||
- title: $if-then-else, false | ||
context: {cond: false} | ||
template: {$if: 'cond', then: 1, else: 2} | ||
output: 2 | ||
|
||
- title: $if-then in array, true | ||
context: {cond: true} | ||
template: [0, {$if: 'cond', then: 1}] | ||
output: [0, 1] | ||
|
||
- title: $if-then in array, false | ||
context: {cond: false} | ||
template: [0, {$if: 'cond', then: 1}] | ||
output: [0] # missing else branch should return a delete-marker | ||
|
||
- title: $if-then in object, true | ||
context: {cond: true} | ||
template: {key: {$if: 'cond', then: 1}, k2: 3} | ||
output: {key: 1, k2: 3} | ||
|
||
- title: $if-then in object, false | ||
context: {cond: false} | ||
template: {key: {$if: 'cond', then: 1}, k2: 3} | ||
output: {k2: 3} # missing else branch should return a delete-marker | ||
|
||
- title: $if -> delete-marker, true | ||
context: {cond: true} | ||
template: {key: {$if: 'cond'}, k2: 3} | ||
output: {k2: 3} # missing then/else branches should return a delete-marker | ||
|
||
- title: $if -> delete-marker, false | ||
context: {cond: false} | ||
template: {key: {$if: 'cond'}, k2: 3} | ||
output: {k2: 3} # missing then/else branches should return a delete-marker | ||
|
||
- title: $if->then, then => $eval, true | ||
context: {key: {b: 1}} | ||
template: {$if: 'true', then: {$eval: 'key'}} | ||
output: {b: 1} | ||
|
||
- title: $if->else, else => $eval, false | ||
context: {key: {b: 1}} | ||
template: {$if: 'false', else: {$eval: 'key'}} | ||
output: {b: 1} | ||
|
||
- title: $if->then, then => ${}, true | ||
context: {key: 'one'} | ||
template: {$if: 'true', then: '${key}'} | ||
output: 'one' | ||
|
||
- title: $if->else, else => ${}, false | ||
context: {key: 'one'} | ||
template: {$if: 'false', else: '${key}'} | ||
output: 'one' | ||
|
||
- title: $if->then, then => object, true | ||
context: {cond: true} | ||
template: {$if: 'cond', then: {key: 'hello world'}} | ||
output: {key: 'hello world'} | ||
|
||
- title: $if->else, else => object, false | ||
context: {cond: false} | ||
template: {$if: 'cond', else: {key: 'hello world'}} | ||
output: {key: 'hello world'} | ||
|
||
- title: $if->then, then => object, $eval, true | ||
context: {cond: true, key: 'hello world'} | ||
template: {$if: 'cond', then: {key: {$eval: 'key'}}} | ||
output: {key: 'hello world'} | ||
|
||
- title: $if->else, else => object, $eval, false | ||
context: {cond: false, key: 'hello world'} | ||
template: {$if: 'cond', else: {key: {$eval: 'key'}}} | ||
output: {key: 'hello world'} | ||
|
||
- title: $if->then, then => object, interpolation, true | ||
context: {cond: true, key: 'world'} | ||
template: {$if: 'cond', then: {key: 'hello ${key}'}} | ||
output: {key: 'hello world'} | ||
|
||
- title: $if->else, else => object, interpolation, false | ||
context: {cond: false, key: 'world'} | ||
template: {$if: 'cond', else: {key: 'hello ${key}'}} | ||
output: {key: 'hello world'} | ||
|
||
- title: $if->then->else, empty string | ||
context: {cond: "", key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: f | ||
|
||
- title: $if->then->else, nonempty string | ||
context: {cond: "stuff", key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: t | ||
|
||
- title: $if->then->else, string "0" # once upon a time, this was false in PHP.. maybe still is | ||
context: {cond: "0", key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: t | ||
|
||
- title: $if->then->else, zero | ||
context: {cond: 0, key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: f | ||
|
||
- title: $if->then->else, one | ||
context: {cond: 1, key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: t | ||
|
||
- title: $if->then->else, null | ||
context: {cond: null, key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: f | ||
|
||
- title: $if->then->else, empty array | ||
context: {cond: [], key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: f | ||
|
||
- title: $if->then->else, nonempty array | ||
context: {cond: [1, 2], key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: t | ||
|
||
- title: $if->then->else, empty object | ||
context: {cond: {}, key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: f | ||
|
||
- title: $if->then->else, nonempty object | ||
context: {cond: {a: 2}, key: 'world'} | ||
template: {$if: 'cond', then: "t", else: "f"} | ||
output: t | ||
|
||
#- title: $if->then->else, function | ||
# context: {} | ||
# template: {$if: 'min', then: "t", else: "f"} | ||
# output: t | ||
|
||
- title: $if->then evaluating to nothing at the top level is null | ||
context: {cond: false} | ||
template: {$if: 'cond', then: "t"} | ||
output: null | ||
|
||
- title: $if-then-else with undefined properties | ||
context: {cond: true} | ||
template: {$if: 'cond', then: 1, foo: 'bar', else: 2, bing: 'baz'} | ||
error: | ||
type: ParseError | ||
message: 'value error: unknown field `bing`, expected one of `$if`, `then`, `else`' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters