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

feat(stdlib): add unflatten vrl function #993

Merged
merged 14 commits into from
Aug 27, 2024
Merged
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ iana-time-zone = { version = "0.1", optional = true }
idna = { version = "0.5", optional = true }
indexmap = { version = "~2.4.0", default-features = false, features = ["std"], optional = true}
indoc = {version = "2", optional = true }
itertools = { version = "0.13", default-features = false, optional = true }
itertools = { version = "0.13", default-features = false, features=["use_std"], optional = true }
jorgehermo9 marked this conversation as resolved.
Show resolved Hide resolved
lalrpop-util = { version = "0.20", optional = true }
mlua = { version = "0.9", default-features = false, features = ["lua54", "send", "vendored"], optional = true}
nom = { version = "7", default-features = false, features = ["std"], optional = true }
Expand Down
26 changes: 26 additions & 0 deletions benches/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ criterion_group!(
to_syslog_severity,
to_unix_timestamp,
truncate,
unflatten,
unique,
// TODO: Cannot pass a Path to bench_function
//unnest
Expand Down Expand Up @@ -2718,6 +2719,31 @@ bench_function! {
}
}

bench_function! {
unflatten => vrl::stdlib::Unflatten;

nested_map {
args: func_args![value: value!({"parent.child1": 1, "parent.child2": 2, key: "val"})],
want: Ok(value!({parent: {child1: 1, child2: 2}, key: "val"})),
}

map_and_array {
args: func_args![value: value!({
"parent.child1": [1, [2, 3]],
"parent.child2.grandchild1": 1,
"parent.child2.grandchild2": [1, [2, 3], 4],
"key": "val",
})],
want: Ok(value!({
"parent": {
"child1": [1, [2, 3]],
"child2": {"grandchild1": 1, "grandchild2": [1, [2, 3], 4]},
},
"key": "val",
})),
}
}

bench_function! {
unique => vrl::stdlib::Unique;

Expand Down
1 change: 1 addition & 0 deletions changelog.d/81.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `unflatten` function to inverse the result of the `flatten` function. This function is useful when you want to convert a flattened object back to its original form.
4 changes: 4 additions & 0 deletions lib/tests/tests/functions/flatten/from_unflatten.vrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# object: { "a.b.c.d": 1, "a.b.c.e": 2, "a.b.f": 3, "a.g": 4 }
# result: { "a.b.c.d": 1, "a.b.c.e": 2, "a.b.f": 3, "a.g": 4 }

flatten(unflatten(.))
jorgehermo9 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions lib/tests/tests/functions/flatten/simple.vrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# object: { "a": { "b": { "c": { "d": 1, "e": 2 }, "f": 3 }, "g": 4 } }
# result: { "a.b.c.d": 1, "a.b.c.e": 2, "a.b.f": 3, "a.g": 4 }

flatten(.)
jorgehermo9 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions lib/tests/tests/functions/unflatten/from_flatten.vrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# object: { "a": { "b": { "c": { "d": 1, "e": 2 }, "f": 3 }, "g": 4 } }
# result: { "a": { "b": { "c": { "d": 1, "e": 2 }, "f": 3 }, "g": 4 } }

unflatten(flatten(.))
4 changes: 4 additions & 0 deletions lib/tests/tests/functions/unflatten/simple.vrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# object: { "a.b.c.d": 1, "a.b.c.e": 2, "a.b.f": 3, "a.g": 4 }
# result: { "a": { "b": { "c": { "d": 1, "e": 2 }, "f": 3 }, "g": 4 } }

unflatten(.)
3 changes: 3 additions & 0 deletions src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ cfg_if::cfg_if! {
mod to_unix_timestamp;
mod community_id;
mod truncate;
mod unflatten;
mod type_def;
mod unique;
mod unnest;
Expand Down Expand Up @@ -379,6 +380,7 @@ cfg_if::cfg_if! {
pub use to_unix_timestamp::ToUnixTimestamp;
pub use truncate::Truncate;
pub use type_def::TypeDef;
pub use unflatten::Unflatten;
pub use unique::Unique;
pub use unnest::Unnest;
pub use upcase::Upcase;
Expand Down Expand Up @@ -566,6 +568,7 @@ pub fn all() -> Vec<Box<dyn Function>> {
Box::new(CommunityID),
Box::new(Truncate),
Box::new(TypeDef),
Box::new(Unflatten),
Box::new(Unique),
Box::new(Unnest),
Box::new(Upcase),
Expand Down
Loading
Loading