Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lang/c++/include/avro/Specific.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
#include "array"
#include <algorithm>
#include <map>
#include <optional>
#include <string>
#include <vector>

#include "AvroTraits.hh"
#include "Config.hh"
#include "Decoder.hh"
#include "Encoder.hh"
#include "Exception.hh"

/**
* A bunch of templates and specializations for encoding and decoding
Expand Down Expand Up @@ -165,6 +167,43 @@ struct codec_traits<double> {
}
};

/**
* codec_traits for Avro optional assumming that the schema is ["null", T].
*/
template<typename T>
struct codec_traits<std::optional<T>> {
/**
* Encodes a given value.
*/
static void encode(Encoder &e, const std::optional<T> &b) {
Copy link
Member

Choose a reason for hiding this comment

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

This makes an assumption that the schema is ["null", T].
This is true most of the time but it is not mandatory. Without having the schema in scope I see no way how to improve it.

Copy link
Author

Choose a reason for hiding this comment

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

I add this remark in a comment not to lost it. Thanks.
Could you give me another schema example of an AVRO optional please.
I have some "optional" as ["null", U, V, T, ...] but I map them to std::variant for now (other PR to come maybe)

Copy link
Member

Choose a reason for hiding this comment

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

AVRO optional

There is no such thing as Avro optional. The type is Union. It can have from 2 to N variants.
"null" is not a mandatory variant. It could be at any index in the variants, but usually it is the first one. Without a Schema it is a wild guess.

Copy link
Author

Choose a reason for hiding this comment

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

Got it now, thanks for the explanation.
I don't see anything else to improve about your remark now that I added the comment in the documentation of the code_traits.
However, I now perfectly share your concern.

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the idea of standard encoding for optional<T>. Users are expected to use it only if the schema is [null, T]. We can even make the code generator use optional<T> whenever the schema is [null, T]. This will break compatibility, of course. So there should be a switch for the generator to make this optional (pun intended).

Since this PR does not break compatibility and makes its intent clear, I think, we should merge it.

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 for optional (expected 0 or 1, got " + std::to_string(n) + ")"); }
switch (n) {
case 0: {
d.decodeNull();
s.reset();
} break;
case 1: {
s.emplace();
avro::decode(d, *s);
} break;
}
}
};

/**
* codec_traits for Avro string.
*/
Expand Down
15 changes: 15 additions & 0 deletions lang/c++/test/SpecificTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

using std::array;
using std::map;
using std::optional;
using std::string;
using std::unique_ptr;
using std::vector;
Expand Down Expand Up @@ -127,6 +128,18 @@ void testDouble() {
BOOST_CHECK_CLOSE(b, n, 0.00000001);
}

void testNonEmptyOptional() {
optional<int64_t> n = -109;
optional<int64_t> b = encodeAndDecode(n);
BOOST_CHECK_EQUAL(b.value(), n.value());
}

void testEmptyOptional() {
optional<int64_t> n;
optional<int64_t> b = encodeAndDecode(n);
BOOST_CHECK(!b.has_value());
}

void testString() {
string n = "abc";
string b = encodeAndDecode(n);
Expand Down Expand Up @@ -191,6 +204,8 @@ init_unit_test_suite(int /*argc*/, char * /*argv*/[]) {
ts->add(BOOST_TEST_CASE(avro::specific::testLong));
ts->add(BOOST_TEST_CASE(avro::specific::testFloat));
ts->add(BOOST_TEST_CASE(avro::specific::testDouble));
ts->add(BOOST_TEST_CASE(avro::specific::testNonEmptyOptional));
ts->add(BOOST_TEST_CASE(avro::specific::testEmptyOptional));
ts->add(BOOST_TEST_CASE(avro::specific::testString));
ts->add(BOOST_TEST_CASE(avro::specific::testBytes));
ts->add(BOOST_TEST_CASE(avro::specific::testFixed));
Expand Down