From 5f461184640db8b74f5bc4494acdb453f705af75 Mon Sep 17 00:00:00 2001 From: Jesse Szwedko Date: Fri, 16 Apr 2021 09:47:34 -0400 Subject: [PATCH] fix(remap): Preserve type defs when assigning fields (#7118) * fix(remap): Preserve type defs when assigning fields Previously, remap would overwrite the type def of `.` whenever a new field was assigned. That is: ``` .foo = 5 .bar = 6 ``` Would result in the compiler only having type info for `.bar`. This change causes it to merge the typedefs whenever a list of path segments appears. Signed-off-by: Jesse Szwedko --- docs/reference/remap.cue | 2 +- lib/vrl/compiler/src/expression/assignment.rs | 16 +++++----------- .../tests/tests/issues/6792_lost_type_defs.vrl | 7 +++++++ 3 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 lib/vrl/tests/tests/issues/6792_lost_type_defs.vrl diff --git a/docs/reference/remap.cue b/docs/reference/remap.cue index 2367ffd12905c..cc0a54c05ed73 100644 --- a/docs/reference/remap.cue +++ b/docs/reference/remap.cue @@ -104,7 +104,7 @@ remap: #Remap & { .tid = to_int!(.tid) # Extract structured data - message_parts = split(.message, ", ", limit: 2) ?? [] + message_parts = split(.message, ", ", limit: 2) structured = parse_key_value(message_parts[1], key_value_delimiter: ":", field_delimiter: ",") ?? {} .message = message_parts[0] . = merge(., structured) diff --git a/lib/vrl/compiler/src/expression/assignment.rs b/lib/vrl/compiler/src/expression/assignment.rs index c522428e284a0..b649502d4a821 100644 --- a/lib/vrl/compiler/src/expression/assignment.rs +++ b/lib/vrl/compiler/src/expression/assignment.rs @@ -197,18 +197,12 @@ impl Target { new_type_def: TypeDef, path: &Option, ) -> TypeDef { - // If the assignment is into a specific index, we want to keep the - // existing type def, and only update the type def at the provided - // index. - let is_index_assignment = path - .as_ref() - .and_then(|p| p.segments().last().map(|s| s.is_index())) - .unwrap_or_default(); - - if is_index_assignment { - current_type_def.clone().merge_overwrite(new_type_def) - } else { + // If the assignment is onto root or has no path (root variable assignment), use the + // new type def, otherwise merge the type defs. + if path.as_ref().map(|path| path.is_root()).unwrap_or(true) { new_type_def + } else { + current_type_def.clone().merge_overwrite(new_type_def) } } diff --git a/lib/vrl/tests/tests/issues/6792_lost_type_defs.vrl b/lib/vrl/tests/tests/issues/6792_lost_type_defs.vrl new file mode 100644 index 0000000000000..8634a0b97017b --- /dev/null +++ b/lib/vrl/tests/tests/issues/6792_lost_type_defs.vrl @@ -0,0 +1,7 @@ +# https://github.com/timberio/vector/issues/6792#issuecomment-817986771 +# result: {"field1": "2020-01-01T00:00:00Z", "field2": 1577836800, "timestamp": "2020-01-01T00:00:00+00:00"} + +.timestamp = "2020-01-01T00:00:00+00:00" +.field1 = parse_timestamp!(.timestamp, format: "%+") +.field2 = to_int(.field1) +.