Skip to content

Commit bd4e72d

Browse files
In place construction when decoding std::optional
Add missing header. Improve error message. Document expected corresponding schema. Prefere optional.reset() over optional = std::nullopt
1 parent 97bedb6 commit bd4e72d

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/etp/EtpMessages.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <avro/Specific.hh>
1515
#include <avro/Encoder.hh>
1616
#include <avro/Decoder.hh>
17+
#include <avro/Exception.hh>
1718

1819
namespace Energistics {
1920
namespace Etp {
@@ -4808,7 +4809,7 @@ namespace avro {
48084809

48094810
namespace avro {
48104811
/**
4811-
* codec_traits for Avro optional.
4812+
* codec_traits for Avro optional assumming that the schema is ["null", T].
48124813
*/
48134814
template<typename T>
48144815
struct codec_traits<std::optional<T>> {
@@ -4831,19 +4832,18 @@ namespace avro {
48314832
*/
48324833
static void decode(Decoder& d, std::optional<T>& s) {
48334834
size_t n = d.decodeUnionIndex();
4834-
if (n >= 2) { throw avro::Exception("Union index too big"); }
4835+
if (n >= 2) { throw avro::Exception("Union index too big for optional (expected 0 or 1, got " + std::to_string(n) + ")"); }
48354836
switch (n) {
48364837
case 0:
48374838
{
48384839
d.decodeNull();
4839-
s = std::nullopt;
4840+
s.reset();
48404841
}
48414842
break;
48424843
case 1:
48434844
{
4844-
T t;
4845-
avro::decode(d, t);
4846-
s.emplace(t);
4845+
s.emplace();
4846+
avro::decode(d, *s);
48474847
}
48484848
break;
48494849
}

0 commit comments

Comments
 (0)