Added documentation on external tags

This commit is contained in:
liuzicheng1987
2023-10-03 12:11:15 +02:00
committed by GitHub
parent ce5ec98815
commit 1a9b765233
+85 -12
View File
@@ -1,10 +1,10 @@
# Variants and Tagged Unions
# `std::variant` and `rfl::TaggedUnion`
## Variants
## `std::variant`
Sometimes you know that the JSON object can be one of several alternatives. For example,
you might have several shapes like `Circle`, `Rectangle` or `Square`. For these kind of
cases, the standard library contains `std::variant`:
cases, the C++ standard library contains `std::variant`:
```cpp
struct Circle {
@@ -36,13 +36,50 @@ several problems with this:
2) It leads to confusing error messages: If none of the alternatives can be matched, you will get an error message telling you why each of the alternatives couldn't be matched. Such error messages are very long-winding and hard to read.
3) It is dangerous. Imagine we had written `std::variant<Circle, Square, Rectangle>` instead of `std::variant<Circle, Rectangle, Square>`. This would mean that `Rectangle` could never be matched, because the fields in `Square` are a subset of `Rectangle`. This leads to very confusing bugs.
## Tagged Unions
## Tagging `std::variant` (externally tagged)
That is why there is an alternative: `rfl::TaggedUnion`.
From a functional programming point-of-view, the most straightforward way to handle these problems is to add tags.
You can do that using `rfl::Field`:
```cpp
using TaggedVariant = std::variant<rfl::Field<"option1", Type1>, rfl::Field<"option2", Type2>, ...>;
```
The parser can now figure this out and will only try to parse the field that was indicated by the field name.
Duplicate field names will lead to compile-time errors.
We can rewrite the example from above:
```cpp
// Circle, Rectangle and Square are the same as above.
using Shapes = std::variant<rfl::Field<"circle", Circle>,
rfl::Field<"rectangle", Rectangle>,
rfl::Field<"square", Square>>;
const Shapes r =
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
const auto json_string = rfl::json::write(r);
const auto r2 = rfl::json::read<Shapes>(json_string);
```
The resulting JSON looks like this:
```json
{"rectangle":{"height":10.0,"width":5.0}}
```
Because the tag is external, this is called *externally tagged*. It is the standard in Rust's [serde-json](https://serde.rs/enum-representations.html).
## `rfl::TaggedUnion` (internally tagged)
But sometimes you also want the tag to be inside the class itself. That is why we have provided a helper class for these purposes: `rfl::TaggedUnion`.
TaggedUnions require some kind of identifying field on your struct, which must be expressed as an `rfl::Literal`. It will then try to take that field from the JSON object, match it to the correct alternative and then only parse the correct alternative.
We will now rewrite the example from above using a Tagged Union:
We will now rewrite the example from above using `rfl::TaggedUnion`:
```cpp
struct Circle {
@@ -76,12 +113,48 @@ const auto json_string = rfl::json::write(r);
const auto r2 = rfl::json::read<Shapes>(json_string);
```
This solves all of the three problems we have listed above.
The resulting JSON looks like this:
```json
{"shape":"Rectangle","height":10.0,"width":5.0}
```
## Resolving tagged unions
Because the tag is inside the JSON object, this is called *internally tagged*.
It is the standard in Python's [pydantic](https://docs.pydantic.dev/latest/api/standard_library_types/#union).
`rfl::TaggedUnion` is just an `std::variant` under-the-hood. The original variant can be
retrieved using `.variant()`. You can then resolve it using the [visitor pattern](https://en.cppreference.com/w/cpp/utility/variant/visit):
## The visitor pattern
In C++, the idiomatic way to handle `std::variant` and `rfl::TaggedUnion` is the [visitor pattern](https://en.cppreference.com/w/cpp/utility/variant/visit).
For instance, the externally tagged `std::variant` from the example above could be handled like this:
```cpp
using Shapes = std::variant<rfl::Field<"circle", Circle>,
rfl::Field<"rectangle", Rectangle>,
rfl::Field<"square", Square>>;
const Shapes my_shape =
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
const auto handle_shapes = [](const auto& field) {
using Type = typename std::decay_t(decltype(field))::Type;
if constexpr (std::is_same<Type, Circle>()) {
std::cout << is circle, radius: << field.value().radius() << std::endl;
} else if constexpr (std::is_same<Type, Rectangle>()) {
std::cout << is rectangle, width: << field.value().width() << ", height: " << field.value().height() << std::endl;
} else if constexpr (std::is_same<Type, Square>()) {
std::cout << is square, width: << field.value().width() << std::endl;
} else {
// reflect-cpp also provides this very useful helper that ensures
// at compile-time that you didn't forget anything.
static_assert(rfl::always_false_v<Type>, "Not all cases were covered.");
}
};
std::visit(handle_shapes, my_shape);
```
Likewise, `rfl::TaggedUnion` is just an `std::variant` under-the-hood. The original variant can be
retrieved using `.variant()`:
```cpp
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
@@ -97,11 +170,11 @@ const auto handle_shapes = [](const auto& s) {
} else if constexpr (std::is_same<Type, Square>()) {
std::cout << is square, width: << s.width() << std::endl;
} else {
// reflect-cpp also provides this very useful helper that ensures
// at compile-time that you didn't forget anything.
static_assert(rfl::always_false_v<Type>, "Not all cases were covered.");
}
};
std::visit(handle_shapes, my_shape.variant());
```