Skip to content
Merged
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
19 changes: 7 additions & 12 deletions include/rfl/parsing/Parser_tagged_union.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,13 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
return Error(stream.str());
};

if constexpr (no_field_names_) {
using T = tagged_union_wrapper_no_ptr_t<std::invoke_result_t<
decltype(wrap_if_necessary<AlternativeType>), AlternativeType>>;
*_result = Parser<R, W, T, ProcessorsType>::read(_r, _var)
.transform(get_fields)
.transform(to_tagged_union)
.transform_error(embellish_error);
} else {
*_result = Parser<R, W, AlternativeType, ProcessorsType>::read(_r, _var)
.transform(to_tagged_union)
.transform_error(embellish_error);
}
using T = tagged_union_wrapper_no_ptr_t<std::invoke_result_t<
decltype(wrap_if_necessary<AlternativeType>), AlternativeType>>;

*_result = Parser<R, W, T, ProcessorsType>::read(_r, _var)
.transform(get_fields)
.transform(to_tagged_union)
.transform_error(embellish_error);

*_match_found = true;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/json/test_tagged_union_with_no_extra_fields.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <rfl.hpp>
#include <rfl/json.hpp>

#include "write_and_read.hpp"

namespace test_tagged_union_with_no_extra_fields {

struct Circle {
double radius;
};

struct Rectangle {
double height;
double width;
};

struct Square {
double width;
};

using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;

TEST(json, test_tagged_union_with_no_extra_fields) {
const Shapes r = Rectangle{.height = 10, .width = 5};

write_and_read<rfl::NoExtraFields>(
r, R"({"shape":"Rectangle","height":10.0,"width":5.0})");
}
} // namespace test_tagged_union_with_no_extra_fields
Loading