From 16fe76643e6916848db85841e0a4ddf45dc68030 Mon Sep 17 00:00:00 2001 From: George Evmenov Date: Wed, 9 Apr 2025 18:02:08 +0300 Subject: [PATCH 1/2] fix bytestring being serialized as vector of flag enums --- include/rfl/Bytestring.hpp | 9 +++++++-- include/rfl/bson/Writer.hpp | 4 ++-- include/rfl/flexbuf/Writer.hpp | 4 ++-- include/rfl/internal/enums/StringConverter.hpp | 3 +++ include/rfl/msgpack/Writer.hpp | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/rfl/Bytestring.hpp b/include/rfl/Bytestring.hpp index 197531e5..56f47cda 100644 --- a/include/rfl/Bytestring.hpp +++ b/include/rfl/Bytestring.hpp @@ -5,8 +5,13 @@ #include namespace rfl { - -using Bytestring = std::vector; +// custom type to avoid serializing this as a vector of enums +// in other means this is the same as +// using Bytestring = std::vector; +class Bytestring : public std::vector { +public: + using std::vector::vector; +}; } // namespace rfl diff --git a/include/rfl/bson/Writer.hpp b/include/rfl/bson/Writer.hpp index 27307c23..a3cc42f8 100644 --- a/include/rfl/bson/Writer.hpp +++ b/include/rfl/bson/Writer.hpp @@ -104,7 +104,7 @@ class Writer { rfl::Bytestring>()) { bson_array_builder_append_binary( _parent->val_, BSON_SUBTYPE_BINARY, - internal::ptr_cast(_var.c_str()), + internal::ptr_cast(_var.data()), static_cast(_var.size())); } else if constexpr (std::is_same, bool>()) { bson_array_builder_append_bool(_parent->val_, _var); @@ -134,7 +134,7 @@ class Writer { rfl::Bytestring>()) { bson_append_binary(_parent->val_, _name.data(), static_cast(_name.size()), BSON_SUBTYPE_BINARY, - internal::ptr_cast(_var.c_str()), + internal::ptr_cast(_var.data()), static_cast(_var.size())); } else if constexpr (std::is_same, bool>()) { bson_append_bool(_parent->val_, _name.data(), diff --git a/include/rfl/flexbuf/Writer.hpp b/include/rfl/flexbuf/Writer.hpp index d43a00ae..90b6302b 100644 --- a/include/rfl/flexbuf/Writer.hpp +++ b/include/rfl/flexbuf/Writer.hpp @@ -97,7 +97,7 @@ struct Writer { fbb_->String(_name.data(), _var); } else if constexpr (std::is_same, rfl::Bytestring>()) { - fbb_->Blob(_name.data(), _var.c_str(), _var.size()); + fbb_->Blob(_name.data(), _var.data(), _var.size()); } else if constexpr (std::is_same, bool>()) { fbb_->Bool(_name.data(), _var); } else if constexpr (std::is_floating_point>()) { @@ -116,7 +116,7 @@ struct Writer { fbb_->String(_var); } else if constexpr (std::is_same, rfl::Bytestring>()) { - fbb_->Blob(_var.c_str(), _var.size()); + fbb_->Blob(_var.data(), _var.size()); } else if constexpr (std::is_same, bool>()) { fbb_->Bool(_var); } else if constexpr (std::is_floating_point>()) { diff --git a/include/rfl/internal/enums/StringConverter.hpp b/include/rfl/internal/enums/StringConverter.hpp index c91f2cad..b2f3d1b6 100644 --- a/include/rfl/internal/enums/StringConverter.hpp +++ b/include/rfl/internal/enums/StringConverter.hpp @@ -36,6 +36,9 @@ class StringConverter { /// Transforms a string to the matching enum. static Result string_to_enum(const std::string& _str) { + static_assert(names_.size != 0, + "No enum could be identified. Please choose enum values " + "between 0 to 127 or for flag enums choose 1,2,4,8,16,..."); if constexpr (is_flag_enum_) { return string_to_flag_enum(_str); } else { diff --git a/include/rfl/msgpack/Writer.hpp b/include/rfl/msgpack/Writer.hpp index 3f64efb6..13cc4e4b 100644 --- a/include/rfl/msgpack/Writer.hpp +++ b/include/rfl/msgpack/Writer.hpp @@ -99,7 +99,7 @@ class Writer { msgpack_pack_str_body(pk_, _var.c_str(), _var.size()); } else if constexpr (std::is_same()) { msgpack_pack_bin(pk_, _var.size()); - msgpack_pack_bin_body(pk_, _var.c_str(), _var.size()); + msgpack_pack_bin_body(pk_, _var.data(), _var.size()); } else if constexpr (std::is_same()) { if (_var) { msgpack_pack_true(pk_); From c842e0b4444a07796b469195a9a99d7a36c1aa41 Mon Sep 17 00:00:00 2001 From: George Evmenov Date: Wed, 9 Apr 2025 19:07:51 +0300 Subject: [PATCH 2/2] fix avro bytestring serialization --- include/rfl/avro/Reader.hpp | 2 +- include/rfl/avro/Writer.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rfl/avro/Reader.hpp b/include/rfl/avro/Reader.hpp index 6a3b442a..4c4724ba 100644 --- a/include/rfl/avro/Reader.hpp +++ b/include/rfl/avro/Reader.hpp @@ -76,7 +76,7 @@ struct Reader { return error("Could not cast to bytestring."); } const auto data = internal::ptr_cast(ptr); - return rfl::Bytestring(data, data + size - 1); + return rfl::Bytestring(data, data + size); } else if constexpr (std::is_same, bool>()) { if (type != AVRO_BOOLEAN) { return rfl::error("Could not cast to boolean."); diff --git a/include/rfl/avro/Writer.hpp b/include/rfl/avro/Writer.hpp index a55637b4..e07c1a19 100644 --- a/include/rfl/avro/Writer.hpp +++ b/include/rfl/avro/Writer.hpp @@ -188,7 +188,7 @@ class Writer { } else if constexpr (std::is_same, rfl::Bytestring>()) { auto var = _var; - avro_value_set_bytes(_val, var.data(), var.size() + 1); + avro_value_set_bytes(_val, var.data(), var.size()); } else if constexpr (std::is_same, bool>()) { avro_value_set_boolean(_val, _var);