Skip to content

Commit

Permalink
feat(stdlib) Add new parse_bytes function (#1198)
Browse files Browse the repository at this point in the history
* Basic implementation for parse_bytes function

* Support exabyte

* Add base optional argument

* change default base to 2 and update testcase

* allow ambiguous unit

* Add more examples and add VRL test

* Add bench and fix clippy

* Adopt parse-size crate

* update comment

* Add change log

* Bump MSVC to 1.81 required by `parse-size` crate
  • Loading branch information
titaneric authored Jan 6, 2025
1 parent 890c123 commit d5fc848
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 1 deletion.
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

0 comments on commit d5fc848

Please sign in to comment.