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: typedef of array/object literals containing undefined values #401

Merged
merged 3 commits into from
Aug 18, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- added `pretty` parameter for `encode_json` vrl function to produce pretty-printed JSON string (https://github.com/vectordotdev/vrl/pull/370)
- `encode_gzip` and `encode_zlib` now correctly check the compression level (preventing a panic) (https://github.com/vectordotdev/vrl/pull/393)
- fix the type definition of array/object literal expressions where one of the values is undefined (https://github.com/vectordotdev/vrl/pull/401)

## `0.6.0` (2023-08-02)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# result: {"x": [null], "typedef": {"array": {"0": {"null": true}}}}

. = {}

# x is an array of size one that is assigned an undefined value that is upgraded to null
x = [.x]
{"x": x, "typedef": type_def(x)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# result: {"x": {"foo": null}, "typedef": {"object": {"foo": {"null": true}}}}

. = {}

# x is an object containing a single field foo that is assigned an undefined value that is upgraded to null
x = {"foo": .x}
{"x": x, "typedef": type_def(x)}
2 changes: 1 addition & 1 deletion src/compiler/expression/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Expression for Array {
let mut fallible = false;

for expr in &self.inner {
let type_def = expr.apply_type_info(&mut state);
let type_def = expr.apply_type_info(&mut state).upgrade_undefined();

// If any expression is fallible, the entire array is fallible.
fallible |= type_def.is_fallible();
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/expression/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Expression for Object {

let mut type_defs = BTreeMap::new();
for (k, expr) in &self.inner {
let type_def = expr.apply_type_info(&mut state);
let type_def = expr.apply_type_info(&mut state).upgrade_undefined();

// If any expression is fallible, the entire object is fallible.
fallible |= type_def.is_fallible();
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ impl TypeDef {
self
}

/// VRL has an interesting property where accessing an undefined value "upgrades"
/// it to a "null" value.
/// This should be used in places those implicit upgrades can occur.
// see: https://github.com/vectordotdev/vector/issues/13594
#[must_use]
pub fn upgrade_undefined(mut self) -> Self {
self.kind = self.kind.upgrade_undefined();
self
}

/// Collects any subtypes that can contain multiple indexed types (array, object) and collects
/// them into a single type for all indexes.
///
Expand Down
Loading