Skip to content
Draft
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
20 changes: 20 additions & 0 deletions docs/content/variants_and_tagged_unions.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,26 @@ The resulting JSON looks like this:

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).

## `std::monostate`

`std::monostate` is serialized as `null`. A `std::variant` or `rfl::Variant` whose
first alternative is `std::monostate` behaves like `std::optional`: as a field, it is
omitted when it holds `std::monostate` and a missing field is read as `std::monostate`.

```cpp
struct Order {
std::variant<std::monostate, std::string, int> price;
};

rfl::json::write(Order{.price = std::monostate{}}); // {}
rfl::json::write(Order{.price = 2}); // {"price":2}
```

Both `{}` and `{"price":null}` are read as `std::monostate`.

As with `std::optional`, passing `rfl::NoOptionals` writes the field as `null`
(`{"price":null}`) and makes it required on read.


## The visitor pattern

Expand Down
58 changes: 58 additions & 0 deletions include/rfl/parsing/ParserMonostate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef RFL_PARSING_PARSER_MONOSTATE_HPP_
#define RFL_PARSING_PARSER_MONOSTATE_HPP_

#include <map>
#include <string>
#include <variant>

#include "../Result.hpp"
#include "Parent.hpp"
#include "Parser_base.hpp"
#include "is_monostate.hpp"
#include "schema/Type.hpp"

namespace rfl::parsing {

template <class R, class W, class ProcessorsType>
struct ParserMonostate {
using InputVarType = typename R::InputVarType;

using ParentType = Parent<W>;

/**
* @brief Reads a std::monostate from the input, which must be null.
* @param _r The reader to use.
* @param _var The input variable to read.
* @return A Result containing the std::monostate or an error.
*/
static Result<std::monostate> read(const R& _r,
const InputVarType& _var) noexcept {
if (!_r.is_empty(_var)) {
return error("Expected null for std::monostate.");
}
return std::monostate{};
}

/**
* @brief Writes a std::monostate to the output as null.
* @tparam P The type of the parent.
* @param _w The writer to use.
* @param _parent The parent object.
*/
template <class P>
static void write(const W& _w, const std::monostate&, const P& _parent) {
ParentType::add_null(_w, _parent);
}

/**
* @brief Generates the schema for std::monostate.
* @return The null schema type.
*/
static schema::Type to_schema(std::map<std::string, schema::Type>*) {
return schema::Type{schema::Type::Null{}};
}
};

} // namespace rfl::parsing

#endif
10 changes: 10 additions & 0 deletions include/rfl/parsing/Parser_default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "ParserEnum.hpp"
#include "ParserExpected.hpp"
#include "ParserFilepath.hpp"
#include "ParserMonostate.hpp"
#include "ParserOptional.hpp"
#include "ParserPair.hpp"
#include "ParserPositional.hpp"
Expand Down Expand Up @@ -152,6 +153,9 @@ struct Parser {
} else if constexpr (is_filepath_v<T>) {
return ParserFilepath<R, W, ProcessorsType>::read(_r, _var);

} else if constexpr (is_monostate_v<T>) {
return ParserMonostate<R, W, ProcessorsType>::read(_r, _var);

} else if constexpr (is_skip_v<T>) {
using U = std::remove_cvref_t<T>;
return ParserSkip<R, W, typename U::Type, U::skip_serialization_,
Expand Down Expand Up @@ -339,6 +343,9 @@ struct Parser {
} else if constexpr (is_filepath_v<T>) {
ParserFilepath<R, W, ProcessorsType>::write(_w, _var, _parent);

} else if constexpr (is_monostate_v<T>) {
ParserMonostate<R, W, ProcessorsType>::write(_w, _var, _parent);

} else if constexpr (is_skip_v<T>) {
using U = std::remove_cvref_t<T>;
ParserSkip<R, W, typename U::Type, U::skip_serialization_,
Expand Down Expand Up @@ -512,6 +519,9 @@ struct Parser {
} else if constexpr (is_filepath_v<U>) {
return ParserFilepath<R, W, ProcessorsType>::to_schema(_definitions);

} else if constexpr (is_monostate_v<U>) {
return ParserMonostate<R, W, ProcessorsType>::to_schema(_definitions);

} else if constexpr (is_skip_v<U>) {
return ParserSkip<R, W, typename U::Type, U::skip_serialization_,
U::skip_deserialization_,
Expand Down
3 changes: 3 additions & 0 deletions include/rfl/parsing/is_empty.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../internal/is_rename.hpp"
#include "../internal/is_skip.hpp"
#include "is_map_like.hpp"
#include "is_monostate_variant.hpp"
#include "is_vector_like.hpp"

namespace rfl {
Expand Down Expand Up @@ -36,6 +37,8 @@ static bool is_empty(const T& _var) {
}
} else if constexpr (internal::is_rename_v<Type>) {
return is_empty(_var.value());
} else if constexpr (is_monostate_variant_v<Type>) {
return _var.index() == 0;
} else if constexpr (is_map_like_v<Type> || is_vector_like_v<Type>) {
return _var.begin() == _var.end();
} else {
Expand Down
20 changes: 20 additions & 0 deletions include/rfl/parsing/is_monostate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef RFL_PARSING_IS_MONOSTATE_HPP_
#define RFL_PARSING_IS_MONOSTATE_HPP_

#include <type_traits>
#include <variant>

namespace rfl::parsing {

template <class T>
struct is_monostate : std::false_type {};

template <>
struct is_monostate<std::monostate> : std::true_type {};

template <class T>
constexpr bool is_monostate_v = is_monostate<std::remove_cvref_t<T>>::value;

} // namespace rfl::parsing

#endif
29 changes: 29 additions & 0 deletions include/rfl/parsing/is_monostate_variant.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef RFL_PARSING_IS_MONOSTATE_VARIANT_HPP_
#define RFL_PARSING_IS_MONOSTATE_VARIANT_HPP_

#include <type_traits>
#include <variant>

#include "../Variant.hpp"
#include "is_monostate.hpp"

namespace rfl::parsing {

template <class T>
struct is_monostate_variant : std::false_type {};

template <class T, class... Ts>
struct is_monostate_variant<std::variant<T, Ts...>>
: std::bool_constant<is_monostate_v<T>> {};

template <class T, class... Ts>
struct is_monostate_variant<rfl::Variant<T, Ts...>>
: std::bool_constant<is_monostate_v<T>> {};

template <class T>
constexpr bool is_monostate_variant_v =
is_monostate_variant<std::remove_cvref_t<T>>::value;

} // namespace rfl::parsing

#endif
3 changes: 2 additions & 1 deletion include/rfl/parsing/is_required.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "../internal/is_rename.hpp"
#include "../internal/is_short.hpp"
#include "is_map_like.hpp"
#include "is_monostate_variant.hpp"
#include "is_vector_like.hpp"

namespace rfl {
Expand Down Expand Up @@ -64,7 +65,7 @@ consteval bool is_required() {
} else if constexpr (internal::is_short_v<Type>) {
return is_required<typename Type::Type, _ignore_empty_containers>();
} else {
return !(is_never_required_v<Type> ||
return !(is_never_required_v<Type> || is_monostate_variant_v<Type> ||
(_ignore_empty_containers &&
(is_vector_like_v<Type> || is_map_like_v<Type>)));
}
Expand Down
8 changes: 5 additions & 3 deletions include/rfl/parsing/schema/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace rfl::parsing::schema {
struct RFL_API Type {
struct Boolean {};

struct Null {};

struct Bytestring {};

struct Vectorstring {};
Expand Down Expand Up @@ -115,9 +117,9 @@ struct RFL_API Type {
};

using VariantType =
rfl::Variant<Boolean, Bytestring, Vectorstring, Int32, Int64, UInt32,
UInt64, Integer, Float, Double, String, AnyOf, DefaultVal,
Deprecated, Description, DescribedLiteral,
rfl::Variant<Boolean, Null, Bytestring, Vectorstring, Int32, Int64,
UInt32, UInt64, Integer, Float, Double, String, AnyOf,
DefaultVal, Deprecated, Description, DescribedLiteral,
FixedSizeTypedArray, Literal, Object, Optional, Reference,
StringMap, Tuple, TypedArray, Validated>;

Expand Down
3 changes: 3 additions & 0 deletions src/rfl/avro/to_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ schema::Type type_to_avro_schema_type(
if constexpr (std::is_same<T, Type::Boolean>()) {
return schema::Type{.value = schema::Type::Boolean{}};

} else if constexpr (std::is_same<T, Type::Null>()) {
return schema::Type{.value = schema::Type::Null{}};

} else if constexpr (std::is_same<T, Type::Bytestring>() ||
std::is_same<T, Type::Vectorstring>()) {
return schema::Type{.value = schema::Type::Bytes{}};
Expand Down
3 changes: 3 additions & 0 deletions src/rfl/capnproto/to_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ schema::Type type_to_capnproto_schema_type(
if constexpr (std::is_same<T, Type::Boolean>()) {
return schema::Type{.value = schema::Type::Bool{}};

} else if constexpr (std::is_same<T, Type::Null>()) {
return schema::Type{.value = schema::Type::Void{}};

} else if constexpr (std::is_same<T, Type::Bytestring>() ||
std::is_same<T, Type::Vectorstring>()) {
return schema::Type{.value = schema::Type::Data{}};
Expand Down
8 changes: 8 additions & 0 deletions src/rfl/json/to_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ bool is_optional(const parsing::schema::Type& _t) {
} else if constexpr (std::is_same_v<T, parsing::schema::Type::Validated>) {
return is_optional(*_v.type_);

} else if constexpr (std::is_same_v<T, parsing::schema::Type::AnyOf>) {
return !_v.types_.empty() &&
rfl::holds_alternative<parsing::schema::Type::Null>(
_v.types_.front().variant_);

} else {
return std::is_same_v<T, parsing::schema::Type::Optional> ||
std::is_same_v<T, parsing::schema::Type::DefaultVal>;
Expand Down Expand Up @@ -199,6 +204,9 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
if constexpr (std::is_same<T, Type::Boolean>()) {
return schema::Type{.value = schema::Type::Boolean{}};

} else if constexpr (std::is_same<T, Type::Null>()) {
return schema::Type{.value = schema::Type::Null{}};

} else if constexpr (std::is_same<T, Type::Int32>() ||
std::is_same<T, Type::Int64>() ||
std::is_same<T, Type::UInt32>() ||
Expand Down
29 changes: 29 additions & 0 deletions tests/avro/test_monostate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <rfl.hpp>
#include <string>
#include <variant>

#include "write_and_read.hpp"

namespace test_monostate {

struct Order {
std::variant<std::monostate, std::string, int> price;
};

struct RflOrder {
rfl::Variant<std::monostate, std::string, int> price;
};

TEST(avro, test_monostate) {
write_and_read(Order{.price = std::monostate{}});
write_and_read(Order{.price = "1.25"});
write_and_read(Order{.price = 2});
}

TEST(avro, test_monostate_rfl_variant) {
write_and_read(RflOrder{.price = std::monostate{}});
write_and_read(RflOrder{.price = std::string("1.25")});
write_and_read(RflOrder{.price = 2});
}

} // namespace test_monostate
39 changes: 39 additions & 0 deletions tests/cbor/test_monostate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <rfl.hpp>
#include <string>
#include <variant>

#include "write_and_read.hpp"

namespace test_monostate {

struct Order {
std::variant<std::monostate, std::string, int> price;
};

struct RflOrder {
rfl::Variant<std::monostate, std::string, int> price;
};

struct Empty {};

TEST(cbor, test_monostate) {
write_and_read(Order{.price = std::monostate{}});
write_and_read(Order{.price = "1.25"});
write_and_read(Order{.price = 2});

const auto res = rfl::cbor::read<Order>(rfl::cbor::write(Empty{}));
ASSERT_TRUE(res) << res.error().what();
EXPECT_TRUE(std::holds_alternative<std::monostate>(res->price));
}

TEST(cbor, test_monostate_rfl_variant) {
write_and_read(RflOrder{.price = std::monostate{}});
write_and_read(RflOrder{.price = std::string("1.25")});
write_and_read(RflOrder{.price = 2});

const auto res = rfl::cbor::read<RflOrder>(rfl::cbor::write(Empty{}));
ASSERT_TRUE(res) << res.error().what();
EXPECT_TRUE(rfl::holds_alternative<std::monostate>(res->price));
}

} // namespace test_monostate
Loading
Loading