Skip to content

Commit 24927df

Browse files
Made sure TaggedUnion can be used in combination with rfl::NoExtraFields; fixes #567 (#573)
1 parent c3f2628 commit 24927df

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

include/rfl/parsing/Parser_tagged_union.hpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,13 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
154154
return Error(stream.str());
155155
};
156156

157-
if constexpr (no_field_names_) {
158-
using T = tagged_union_wrapper_no_ptr_t<std::invoke_result_t<
159-
decltype(wrap_if_necessary<AlternativeType>), AlternativeType>>;
160-
*_result = Parser<R, W, T, ProcessorsType>::read(_r, _var)
161-
.transform(get_fields)
162-
.transform(to_tagged_union)
163-
.transform_error(embellish_error);
164-
} else {
165-
*_result = Parser<R, W, AlternativeType, ProcessorsType>::read(_r, _var)
166-
.transform(to_tagged_union)
167-
.transform_error(embellish_error);
168-
}
157+
using T = tagged_union_wrapper_no_ptr_t<std::invoke_result_t<
158+
decltype(wrap_if_necessary<AlternativeType>), AlternativeType>>;
159+
160+
*_result = Parser<R, W, T, ProcessorsType>::read(_r, _var)
161+
.transform(get_fields)
162+
.transform(to_tagged_union)
163+
.transform_error(embellish_error);
169164

170165
*_match_found = true;
171166
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <rfl.hpp>
2+
#include <rfl/json.hpp>
3+
4+
#include "write_and_read.hpp"
5+
6+
namespace test_tagged_union_with_no_extra_fields {
7+
8+
struct Circle {
9+
double radius;
10+
};
11+
12+
struct Rectangle {
13+
double height;
14+
double width;
15+
};
16+
17+
struct Square {
18+
double width;
19+
};
20+
21+
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
22+
23+
TEST(json, test_tagged_union_with_no_extra_fields) {
24+
const Shapes r = Rectangle{.height = 10, .width = 5};
25+
26+
write_and_read<rfl::NoExtraFields>(
27+
r, R"({"shape":"Rectangle","height":10.0,"width":5.0})");
28+
}
29+
} // namespace test_tagged_union_with_no_extra_fields

0 commit comments

Comments
 (0)