Skip to content

Commit 15742a8

Browse files
authored
Fix bytestring being serialized as vector of flag enums (#403)
1 parent d73d98f commit 15742a8

7 files changed

Lines changed: 17 additions & 9 deletions

File tree

include/rfl/Bytestring.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
#include <vector>
66

77
namespace rfl {
8-
9-
using Bytestring = std::vector<std::byte>;
8+
// custom type to avoid serializing this as a vector of enums
9+
// in other means this is the same as
10+
// using Bytestring = std::vector<std::byte>;
11+
class Bytestring : public std::vector<std::byte> {
12+
public:
13+
using std::vector<std::byte>::vector;
14+
};
1015

1116
} // namespace rfl
1217

include/rfl/avro/Reader.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct Reader {
7676
return error("Could not cast to bytestring.");
7777
}
7878
const auto data = internal::ptr_cast<const std::byte*>(ptr);
79-
return rfl::Bytestring(data, data + size - 1);
79+
return rfl::Bytestring(data, data + size);
8080
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
8181
if (type != AVRO_BOOLEAN) {
8282
return rfl::error("Could not cast to boolean.");

include/rfl/avro/Writer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class Writer {
188188
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
189189
rfl::Bytestring>()) {
190190
auto var = _var;
191-
avro_value_set_bytes(_val, var.data(), var.size() + 1);
191+
avro_value_set_bytes(_val, var.data(), var.size());
192192

193193
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
194194
avro_value_set_boolean(_val, _var);

include/rfl/bson/Writer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Writer {
104104
rfl::Bytestring>()) {
105105
bson_array_builder_append_binary(
106106
_parent->val_, BSON_SUBTYPE_BINARY,
107-
internal::ptr_cast<const uint8_t*>(_var.c_str()),
107+
internal::ptr_cast<const uint8_t*>(_var.data()),
108108
static_cast<uint32_t>(_var.size()));
109109
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
110110
bson_array_builder_append_bool(_parent->val_, _var);
@@ -134,7 +134,7 @@ class Writer {
134134
rfl::Bytestring>()) {
135135
bson_append_binary(_parent->val_, _name.data(),
136136
static_cast<int>(_name.size()), BSON_SUBTYPE_BINARY,
137-
internal::ptr_cast<const uint8_t*>(_var.c_str()),
137+
internal::ptr_cast<const uint8_t*>(_var.data()),
138138
static_cast<uint32_t>(_var.size()));
139139
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
140140
bson_append_bool(_parent->val_, _name.data(),

include/rfl/flexbuf/Writer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct Writer {
9797
fbb_->String(_name.data(), _var);
9898
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
9999
rfl::Bytestring>()) {
100-
fbb_->Blob(_name.data(), _var.c_str(), _var.size());
100+
fbb_->Blob(_name.data(), _var.data(), _var.size());
101101
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
102102
fbb_->Bool(_name.data(), _var);
103103
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
@@ -116,7 +116,7 @@ struct Writer {
116116
fbb_->String(_var);
117117
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
118118
rfl::Bytestring>()) {
119-
fbb_->Blob(_var.c_str(), _var.size());
119+
fbb_->Blob(_var.data(), _var.size());
120120
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
121121
fbb_->Bool(_var);
122122
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {

include/rfl/internal/enums/StringConverter.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class StringConverter {
3636

3737
/// Transforms a string to the matching enum.
3838
static Result<EnumType> string_to_enum(const std::string& _str) {
39+
static_assert(names_.size != 0,
40+
"No enum could be identified. Please choose enum values "
41+
"between 0 to 127 or for flag enums choose 1,2,4,8,16,...");
3942
if constexpr (is_flag_enum_) {
4043
return string_to_flag_enum(_str);
4144
} else {

include/rfl/msgpack/Writer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Writer {
9999
msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
100100
} else if constexpr (std::is_same<Type, rfl::Bytestring>()) {
101101
msgpack_pack_bin(pk_, _var.size());
102-
msgpack_pack_bin_body(pk_, _var.c_str(), _var.size());
102+
msgpack_pack_bin_body(pk_, _var.data(), _var.size());
103103
} else if constexpr (std::is_same<Type, bool>()) {
104104
if (_var) {
105105
msgpack_pack_true(pk_);

0 commit comments

Comments
 (0)