-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add API for deserializing data envelopes from JSON #395
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#include "broker/data_envelope.hh" | ||
|
||
#include "broker/broker-test.test.hh" | ||
|
||
using namespace broker; | ||
using namespace std::literals; | ||
|
||
namespace { | ||
|
||
// A data message that has one of everything. | ||
constexpr std::string_view json = R"_({ | ||
"type": "data-message", | ||
"topic": "/foo/bar", | ||
"@data-type": "vector", | ||
"data": [ | ||
{ | ||
"@data-type": "none", | ||
"data": {} | ||
}, | ||
{ | ||
"@data-type": "boolean", | ||
"data": true | ||
}, | ||
{ | ||
"@data-type": "count", | ||
"data": 42 | ||
}, | ||
{ | ||
"@data-type": "integer", | ||
"data": 23 | ||
}, | ||
{ | ||
"@data-type": "real", | ||
"data": 12.48 | ||
}, | ||
{ | ||
"@data-type": "string", | ||
"data": "this is a string" | ||
}, | ||
{ | ||
"@data-type": "address", | ||
"data": "2001:db8::" | ||
}, | ||
{ | ||
"@data-type": "subnet", | ||
"data": "255.255.255.0/24" | ||
}, | ||
{ | ||
"@data-type": "port", | ||
"data": "8080/tcp" | ||
}, | ||
{ | ||
"@data-type": "timestamp", | ||
"data": "2022-04-10T16:07:00.000" | ||
}, | ||
{ | ||
"@data-type": "timespan", | ||
"data": "23s" | ||
}, | ||
{ | ||
"@data-type": "enum-value", | ||
"data": "foo" | ||
}, | ||
{ | ||
"@data-type": "set", | ||
"data": [ | ||
{ | ||
"@data-type": "integer", | ||
"data": 1 | ||
}, | ||
{ | ||
"@data-type": "integer", | ||
"data": 2 | ||
}, | ||
{ | ||
"@data-type": "integer", | ||
"data": 3 | ||
} | ||
] | ||
}, | ||
{ | ||
"@data-type": "table", | ||
"data": [ | ||
{ | ||
"key": { | ||
"@data-type": "string", | ||
"data": "first-name" | ||
}, | ||
"value": { | ||
"@data-type": "string", | ||
"data": "John" | ||
} | ||
}, | ||
{ | ||
"key": { | ||
"@data-type": "string", | ||
"data": "last-name" | ||
}, | ||
"value": { | ||
"@data-type": "string", | ||
"data": "Doe" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
})_"; | ||
|
||
} // namespace | ||
|
||
TEST(JSON can be deserialized to a data message) { | ||
auto maybe_envelope = envelope::deserialize_json(json.data(), json.size()); | ||
REQUIRE(maybe_envelope); | ||
} | ||
|
||
TEST(an invalid JSON payload results in an error) { | ||
auto no_json = "this is not json!"sv; | ||
auto maybe_envelope = envelope::deserialize_json(no_json.data(), | ||
no_json.size()); | ||
CHECK(!maybe_envelope); | ||
} | ||
|
||
TEST(a JSON payload that does not contain a broker data results in an error) { | ||
std::string_view obj = R"_({"foo": "bar"})_"; | ||
auto maybe_envelope = envelope::deserialize_json(obj.data(), obj.size()); | ||
CHECK(!maybe_envelope); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I realize I'm looking at parsing one layer further in: The idea was JSON contains only the serialized event vector without the
data-message
andtopic
.In my use-case, "type" and "topic" are in message headers, not within the payload and the payload isn't meant to be structured like the websocket protocol.
Let me check if the JSON serialization in #391 even does what I thought it did.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you'd basically want to have a function that parses you just the
variant
from the JSON format? I'll add that later.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, the following snippet:
Produces JSON as follows (where the object encoding a Zeek event is at the top-level), similarly how this works for the binary protocol shown in #391. There's no
topic
ortype
, just the "event vector directly".And that is what I was hoping to parse. For the binary protocol that can be achieved via the following:
Am I looking for
broker::data_envelop::deserialize_json(...)
instead? Maybe that doesn't make overly much sense.Now, the main motivation is to "just" re-use existing encoding/serialization functionality from broker for events. If you think that API doesn't make much sense to have, feel free to push back. On the other hand, there is an API for serialization, so it might make sense to offer one for de-serialization as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No push back at all, I just didn't think of that use case yet. 🙂
I'll add functions that do the JSON (de)-serialization for
variant
as well.