Skip to content

Commit

Permalink
fix typedefs with query side-effects (vectordotdev#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuchsnj authored and itkovian committed Jun 1, 2023
1 parent 484bc9d commit 0b92c96
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lib/tests/tests/expressions/query/container_side_effect.vrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# result: {"type": { "bytes": true }, "value": "foo"}
. = 3
{. = "foo"}.x
{
"value": .,
"type": type_def(.)
}
7 changes: 7 additions & 0 deletions lib/tests/tests/expressions/query/function_side_effect.vrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# result: {"type": { "null": true }, "value": null}
. = 3
del(.).x
{
"value": .,
"type": type_def(.)
}
24 changes: 16 additions & 8 deletions src/compiler/expression/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,22 @@ impl Expression for Query {
fn type_info(&self, state: &TypeState) -> TypeInfo {
use Target::{Container, External, FunctionCall, Internal};

let result = match &self.target {
External(prefix) => state.external.kind(*prefix).at_path(&self.path).into(),
Internal(variable) => variable.type_def(state).at_path(&self.path),
FunctionCall(call) => call.type_def(state).at_path(&self.path),
Container(container) => container.type_def(state).at_path(&self.path),
};

TypeInfo::new(state, result)
match &self.target {
External(prefix) => {
let result = state.external.kind(*prefix).at_path(&self.path).into();
TypeInfo::new(state, result)
}
Internal(variable) => {
let result = variable.type_def(state).at_path(&self.path);
TypeInfo::new(state, result)
}
FunctionCall(call) => call
.type_info(state)
.map_result(|result| result.at_path(&self.path)),
Container(container) => container
.type_info(state)
.map_result(|result| result.at_path(&self.path)),
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/compiler/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ impl TypeInfo {
result,
}
}

pub fn map_result(self, f: impl FnOnce(TypeDef) -> TypeDef) -> Self {
Self {
state: self.state,
result: f(self.result),
}
}
}

impl From<&TypeState> for TypeState {
Expand Down

0 comments on commit 0b92c96

Please sign in to comment.