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 new parse_bytes function #1198

Merged
merged 12 commits into from
Jan 6, 2025
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/vectordotdev/vrl"
readme = "README.md"
keywords = ["vector", "datadog", "compiler"]
categories = ["compilers"]
rust-version = "1.80" # msrv
rust-version = "1.81" # msrv

[workspace]
members = [
Expand Down Expand Up @@ -93,6 +93,7 @@ stdlib = [
"dep:nom",
"dep:ofb",
"dep:once_cell",
"dep:parse-size",
"dep:percent-encoding",
"dep:prost",
"dep:prost-reflect",
Expand Down Expand Up @@ -160,6 +161,7 @@ once_cell = { version = "1", default-features = false, features = ["std"], optio
ordered-float = { version = "4", default-features = false, optional = true }
md-5 = { version = "0.10", optional = true }
paste = { version = "1", default-features = false, optional = true }
parse-size = { version = "1.1.0", optional = true }
peeking_take_while = { version = "1", default-features = false, optional = true }
percent-encoding = { version = "2", optional = true }
pest = { version = "2", default-features = false, optional = true, features = ["std"] }
Expand Down
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ owo-colors,https://github.com/jam1garner/owo-colors,MIT,jam1garner <8260240+jam1
pad,https://github.com/ogham/rust-pad,MIT,Ben S <[email protected]>
parking,https://github.com/smol-rs/parking,Apache-2.0 OR MIT,"Stjepan Glavina <[email protected]>, The Rust Project Developers"
parking_lot,https://github.com/Amanieu/parking_lot,MIT OR Apache-2.0,Amanieu d'Antras <[email protected]>
parse-size,https://github.com/kennytm/parse-size,MIT,kennytm <[email protected]>
paste,https://github.com/dtolnay/paste,MIT OR Apache-2.0,David Tolnay <[email protected]>
peeking_take_while,https://github.com/fitzgen/peeking_take_while,MIT OR Apache-2.0,Nick Fitzgerald <[email protected]>
pest,https://github.com/pest-parser/pest,MIT OR Apache-2.0,Dragoș Tiselice <[email protected]>
Expand Down
10 changes: 10 additions & 0 deletions benches/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ criterion_group!(
parse_aws_alb_log,
parse_aws_cloudwatch_log_subscription_message,
parse_aws_vpc_flow_log,
parse_bytes,
parse_common_log,
parse_csv,
parse_duration,
Expand Down Expand Up @@ -1644,6 +1645,15 @@ bench_function! {
}
}

bench_function! {
parse_bytes => vrl::stdlib::ParseBytes;

literal {
args: func_args![value: "1024KiB", unit: "MiB"],
want: Ok(1.0),
}
}

bench_function! {
parse_common_log => vrl::stdlib::ParseCommonLog;

Expand Down
1 change: 1 addition & 0 deletions changelog.d/1198.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added new `parse_bytes` function to parse given bytes string such as `1MiB` or `1TB` either in binary or decimal base.
3 changes: 3 additions & 0 deletions lib/tests/tests/functions/parse_bytes.vrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# result: 1024.0

parse_bytes!("1KB", unit: "B")
3 changes: 3 additions & 0 deletions src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ cfg_if::cfg_if! {
mod parse_aws_alb_log;
mod parse_aws_cloudwatch_log_subscription_message;
mod parse_aws_vpc_flow_log;
mod parse_bytes;
mod parse_cef;
mod parse_cbor;
mod parse_common_log;
Expand Down Expand Up @@ -326,6 +327,7 @@ cfg_if::cfg_if! {
pub use parse_aws_alb_log::ParseAwsAlbLog;
pub use parse_aws_cloudwatch_log_subscription_message::ParseAwsCloudWatchLogSubscriptionMessage;
pub use parse_aws_vpc_flow_log::ParseAwsVpcFlowLog;
pub use parse_bytes::ParseBytes;
pub use parse_cbor::ParseCbor;
pub use parse_cef::ParseCef;
pub use parse_common_log::ParseCommonLog;
Expand Down Expand Up @@ -517,6 +519,7 @@ pub fn all() -> Vec<Box<dyn Function>> {
Box::new(ParseAwsAlbLog),
Box::new(ParseAwsCloudWatchLogSubscriptionMessage),
Box::new(ParseAwsVpcFlowLog),
Box::new(ParseBytes),
Box::new(ParseCbor),
Box::new(ParseCef),
Box::new(ParseCommonLog),
Expand Down
Loading
Loading