Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: typedefs with query side-effects #258

Merged
merged 1 commit into from
May 26, 2023
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
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