Skip to content

Commit

Permalink
temporarily allow None values in conditions
Browse files Browse the repository at this point in the history
Previously, accessing a path was a fallible operation, and the remap
condition implementation is configured to allow a script to fail (in
which case the condition returns `false`).

Paths queries have been changed to never return an error, but return
`None` if the path doesn't exist.

This means the condition check now also has to accept none values, and
consider them `false`.

This is a temporary change, as optional values are going to be removed
from the Remap language. Non-existing paths will instead resolve to
`null`.

Signed-off-by: Jean Mertz <git@jeanmertz.com>
  • Loading branch information
JeanMertz committed Nov 12, 2020
1 parent 3a7e485 commit 1c7be2a
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/conditions/remap.rs
Expand Up @@ -23,11 +23,12 @@ impl ConditionConfig for RemapConfig {
fn build(&self) -> crate::Result<Box<dyn Condition>> {
let expected_result = TypeDef {
fallible: true,
optional: false,
optional: true,
constraint: value::Constraint::Exact(value::Kind::Boolean),
};

let program = Program::new(&self.source, &crate::remap::FUNCTIONS, expected_result)?;
let program = Program::new(&self.source, &crate::remap::FUNCTIONS, expected_result)
.map_err(|e| e.to_string())?;

Ok(Box::new(Remap { program }))
}
Expand Down Expand Up @@ -63,7 +64,7 @@ impl Condition for Remap {
self.execute(&event)
.map(|opt| match opt {
Some(value) => value,
None => unreachable!("non-optional constraint set"),
None => Value::Boolean(false),
})
.map(|value| match value {
Value::Boolean(boolean) => boolean,
Expand All @@ -76,14 +77,16 @@ impl Condition for Remap {
}

fn check_with_context(&self, event: &Event) -> Result<(), String> {
self.execute(event)
let result = self
.execute(event)
.map_err(|err| format!("source execution failed: {:#}", err))?
.ok_or_else(|| unreachable!("non-optional constraint set"))
.and_then(|value| match value {
Value::Boolean(v) if v => Ok(()),
Value::Boolean(v) if !v => Err("source execution resolved to false".into()),
_ => unreachable!("boolean type constraint set"),
})
.unwrap_or_else(|| Value::Boolean(false));

match result {
Value::Boolean(v) if v => Ok(()),
Value::Boolean(v) if !v => Err("source execution resolved to false".into()),
_ => unreachable!("boolean type constraint set"),
}
}
}

Expand Down

0 comments on commit 1c7be2a

Please sign in to comment.