Skip to content

Commit

Permalink
fix(remap): Preserve type defs when assigning fields (#7118)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
jszwedko authored Apr 16, 2021
1 parent a5a2127 commit 5f46118
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/reference/remap.cue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 5 additions & 11 deletions lib/vrl/compiler/src/expression/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,12 @@ impl Target {
new_type_def: TypeDef,
path: &Option<Path>,
) -> 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)
}
}

Expand Down
7 changes: 7 additions & 0 deletions lib/vrl/tests/tests/issues/6792_lost_type_defs.vrl
Original file line number Diff line number Diff line change
@@ -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)
.

0 comments on commit 5f46118

Please sign in to comment.