-
Notifications
You must be signed in to change notification settings - Fork 0
3545: AVRO-4200: [C++] Encode and decode std::optional #20
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| #include "array" | ||
| #include <algorithm> | ||
| #include <map> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
|
|
@@ -165,6 +166,44 @@ struct codec_traits<double> { | |
| } | ||
| }; | ||
|
|
||
| /** | ||
| * codec_traits for Avro optional. | ||
| */ | ||
| template<typename T> | ||
| struct codec_traits<std::optional<T>> { | ||
| /** | ||
| * Encodes a given value. | ||
| */ | ||
| static void encode(Encoder &e, const std::optional<T> &b) { | ||
| if (b) { | ||
| e.encodeUnionIndex(1); | ||
| avro::encode(e, b.value()); | ||
| } else { | ||
| e.encodeUnionIndex(0); | ||
| e.encodeNull(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Decodes into a given value. | ||
| */ | ||
| static void decode(Decoder &d, std::optional<T> &s) { | ||
| size_t n = d.decodeUnionIndex(); | ||
| if (n >= 2) { throw avro::Exception("Union index too big"); } | ||
| switch (n) { | ||
| case 0: { | ||
| d.decodeNull(); | ||
| s = std::nullopt; | ||
| } break; | ||
| case 1: { | ||
| T t; | ||
| avro::decode(d, t); | ||
| s.emplace(t); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When decoding into 🤖 Was this useful? React with 👍 or 👎
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. value:good-to-have; category:bug; feedback:The Augment AI reviewer is correct that there is a implicit copy of memory here. It could be improved to use a std::move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Move Semantics for Optional ValuesThe decoded value
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. value:good-to-have; category:bug; feedback:The Bugbot AI reviewer is correct that there is a implicit copy of memory here. It could be improved to use a std::move |
||
| } break; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * codec_traits for Avro string. | ||
| */ | ||
|
|
||
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.
This header throws
avro::Exceptionhere, butException.hhisn’t included in this file; consider including it to avoid relying on transitive includes and ensureSpecific.hhcompiles standalone.🤖 Was this useful? React with 👍 or 👎
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.
value:useful; category:bug; feedback:The Augment AI reviewer is correct that the import is needed to be able to build Specific.hh by itself. Currently it compiles only because some other import imports Exception.hh transitively