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

Fix conversion of intervals through JSON #4056

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

timwoj
Copy link
Member

@timwoj timwoj commented Nov 21, 2024

I'm lifting this out of the storage framework work since it doesn't need to be in there exclusively. This fixes the conversion of interval values from JSON to IntervalVal objects. It also adds a simple btest to test round-trip conversion of Vals through JSON.

src/Val.cc Outdated Show resolved Hide resolved
@timwoj timwoj force-pushed the topic/timw/json-interval-conversion branch from 89ceaf5 to de35626 Compare November 27, 2024 22:41
@timwoj timwoj force-pushed the topic/timw/json-interval-conversion branch from de35626 to d81090c Compare November 27, 2024 23:04
@awelzel
Copy link
Contributor

awelzel commented Nov 28, 2024

Adding a type-specific flag to all of those methods breaks the generic-ness of that whole call-chain.

Ah, that's a good point. I see you did anyway - to some degree only_loggable and re apply only to records already.

I didn't realize the entry is Val::ToJSON() - I thought it's freestanding (BuildJSON() is) - I guess if it was a non-member function utils::to_json(const ValPtr& v) or so, this would be mood.

I could see making a new version of to_json that takes a record of options and passing a RecordVal through to the C++, but I worry about having to do a bunch of repeat record lookups on a potential logging hotpath. Maybe I'm just overthinking it though.

I was thinking converting the options RecordVal to a plain struct that's then passed down. Though that one extra parameter for now seems fine.

Copy link
Contributor

@awelzel awelzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nits, feel free to reject or address.

I like where this ended!

T
T
T
F
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one looks a bit off, is it print f$n == f2_v$n; ?

@@ -0,0 +1,102 @@
# @TEST-DOCS: Test round-trip JSON encoding and decoding using the Zeek methods
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! Could also see this one in bifs/, next to the existing from_json.zeek test. I thought base/utils is used for what's actually located in base/utils.

@@ -5103,14 +5103,18 @@ function anonymize_addr%(a: addr, cl: IPAddrAnonymizationClass%): addr
## rendered name. The default pattern strips a leading
## underscore.
##
## interval_as_double: If true, interval values will be logged as doubles
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## interval_as_double: If true, interval values will be logged as doubles
## interval_as_double: If T, interval values will be logged as doubles

@@ -5103,14 +5103,18 @@ function anonymize_addr%(a: addr, cl: IPAddrAnonymizationClass%): addr
## rendered name. The default pattern strips a leading
## underscore.
##
## interval_as_double: If true, interval values will be logged as doubles
## instead of the broken-out version with units.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to mention type string. Maybe this?

Suggested change
## instead of the broken-out version with units.
## instead of the broken-out version with units as strings.

return make_intrusive<IntervalVal>(j.GetDouble());

if ( j.IsString() ) {
auto parts = util::split(j.GetString(), " ");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we care all that much, but IIRC superfluous spaces would yield empty parts. Could filter out empty parts to support more than one spaces as separators. Mostly relevant if someone crafts input by hand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants