Skip to content

Commit 3723458

Browse files
authored
Serialize/Deserialize vector<char> as bytestring (#466)
1 parent 908ee22 commit 3723458

File tree

28 files changed

+284
-45
lines changed

28 files changed

+284
-45
lines changed

include/rfl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "rfl/UnderlyingEnums.hpp"
4646
#include "rfl/Validator.hpp"
4747
#include "rfl/Variant.hpp"
48+
#include "rfl/Vectorstring.hpp"
4849
#include "rfl/always_false.hpp"
4950
#include "rfl/apply.hpp"
5051
#include "rfl/as.hpp"

include/rfl/Vectorstring.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef RFL_VECTORSTRING_HPP_
2+
#define RFL_VECTORSTRING_HPP_
3+
4+
#include <cstddef>
5+
#include <vector>
6+
7+
namespace rfl {
8+
9+
using Vectorstring = std::vector<char>;
10+
11+
} // namespace rfl
12+
13+
#endif

include/rfl/avro/Reader.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "../Bytestring.hpp"
1414
#include "../Result.hpp"
15+
#include "../Vectorstring.hpp"
1516
#include "../always_false.hpp"
1617
#include "../internal/is_literal.hpp"
1718
#include "../parsing/schemaful/IsSchemafulReader.hpp"
@@ -68,15 +69,24 @@ struct Reader {
6869
}
6970
return std::string(c_str, size - 1);
7071
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
71-
rfl::Bytestring>()) {
72+
rfl::Bytestring>() ||
73+
std::is_same<std::remove_cvref_t<T>,
74+
rfl::Vectorstring>()) {
75+
using VectorType = std::remove_cvref_t<T>;
76+
using ValueType = typename VectorType::value_type;
7277
const void* ptr = nullptr;
7378
size_t size = 0;
7479
const auto err = avro_value_get_bytes(_var.val_, &ptr, &size);
7580
if (err) {
76-
return error("Could not cast to bytestring.");
81+
if constexpr (std::is_same<std::remove_cvref_t<T>,
82+
rfl::Bytestring>()) {
83+
return error("Could not cast to bytestring.");
84+
} else {
85+
return error("Could not cast to vectorstring.");
86+
}
7787
}
78-
const auto data = internal::ptr_cast<const std::byte*>(ptr);
79-
return rfl::Bytestring(data, data + size);
88+
const auto data = internal::ptr_cast<const ValueType*>(ptr);
89+
return VectorType(data, data + size);
8090
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
8191
if (type != AVRO_BOOLEAN) {
8292
return rfl::error("Could not cast to boolean.");

include/rfl/avro/Writer.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "../Bytestring.hpp"
2020
#include "../Ref.hpp"
2121
#include "../Result.hpp"
22+
#include "../Vectorstring.hpp"
2223
#include "../always_false.hpp"
2324
#include "../internal/is_literal.hpp"
2425

@@ -186,7 +187,9 @@ class Writer {
186187
avro_value_set_string_len(_val, _var.c_str(), _var.size() + 1);
187188

188189
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
189-
rfl::Bytestring>()) {
190+
rfl::Bytestring>() ||
191+
std::is_same<std::remove_cvref_t<T>,
192+
rfl::Vectorstring>()) {
190193
auto var = _var;
191194
avro_value_set_bytes(_val, var.data(), var.size());
192195

include/rfl/bson/Reader.hpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "../Box.hpp"
2222
#include "../Bytestring.hpp"
2323
#include "../Result.hpp"
24+
#include "../Vectorstring.hpp"
2425
#include "../always_false.hpp"
2526
#include "../internal/ptr_cast.hpp"
2627

@@ -112,18 +113,34 @@ struct Reader {
112113
}
113114

114115
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
115-
rfl::Bytestring>()) {
116+
rfl::Bytestring>() ||
117+
std::is_same<std::remove_cvref_t<T>,
118+
rfl::Vectorstring>()) {
119+
using VectorType = std::remove_cvref_t<T>;
120+
using ValueType = typename VectorType::value_type;
116121
if (btype != BSON_TYPE_BINARY) {
117-
return error("Could not cast to bytestring.");
122+
if constexpr (std::is_same<std::remove_cvref_t<T>,
123+
rfl::Bytestring>()) {
124+
return error("Could not cast to bytestring.");
125+
} else {
126+
return error("Could not cast to vectorstring.");
127+
}
118128
}
119129
if (value.v_binary.subtype != BSON_SUBTYPE_BINARY) {
120-
return error(
121-
"The BSON subtype must be a binary in order to read into a "
122-
"bytestring.");
130+
if constexpr (std::is_same<std::remove_cvref_t<T>,
131+
rfl::Bytestring>()) {
132+
return error(
133+
"The BSON subtype must be a binary in order to read into a "
134+
"bytestring.");
135+
} else {
136+
return error(
137+
"The BSON subtype must be a binary in order to read into a "
138+
"vectorstring.");
139+
}
123140
}
124141
const auto data =
125-
internal::ptr_cast<const std::byte*>(value.v_binary.data);
126-
return rfl::Bytestring(data, data + value.v_binary.data_len);
142+
internal::ptr_cast<const ValueType*>(value.v_binary.data);
143+
return VectorType(data, data + value.v_binary.data_len);
127144

128145
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
129146
if (btype != BSON_TYPE_BOOL) {

include/rfl/bson/Writer.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "../Bytestring.hpp"
1919
#include "../Ref.hpp"
2020
#include "../Result.hpp"
21+
#include "../Vectorstring.hpp"
2122
#include "../always_false.hpp"
2223
#include "../internal/ptr_cast.hpp"
2324

@@ -101,7 +102,9 @@ class Writer {
101102
bson_array_builder_append_utf8(_parent->val_, _var.c_str(),
102103
static_cast<int>(_var.size()));
103104
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
104-
rfl::Bytestring>()) {
105+
rfl::Bytestring>() ||
106+
std::is_same<std::remove_cvref_t<T>,
107+
rfl::Vectorstring>()) {
105108
bson_array_builder_append_binary(
106109
_parent->val_, BSON_SUBTYPE_BINARY,
107110
internal::ptr_cast<const uint8_t*>(_var.data()),
@@ -131,7 +134,9 @@ class Writer {
131134
static_cast<int>(_name.size()), _var.c_str(),
132135
static_cast<int>(_var.size()));
133136
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
134-
rfl::Bytestring>()) {
137+
rfl::Bytestring>() ||
138+
std::is_same<std::remove_cvref_t<T>,
139+
rfl::Vectorstring>()) {
135140
bson_append_binary(_parent->val_, _name.data(),
136141
static_cast<int>(_name.size()), BSON_SUBTYPE_BINARY,
137142
internal::ptr_cast<const uint8_t*>(_var.data()),

include/rfl/capnproto/Reader.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "../Bytestring.hpp"
1414
#include "../Result.hpp"
15+
#include "../Vectorstring.hpp"
1516
#include "../always_false.hpp"
1617
#include "../internal/is_literal.hpp"
1718
#include "../internal/ptr_cast.hpp"
@@ -64,13 +65,22 @@ class Reader {
6465
return std::string(_var.val_.as<capnp::Text>().cStr());
6566

6667
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
67-
rfl::Bytestring>()) {
68+
rfl::Bytestring>() ||
69+
std::is_same<std::remove_cvref_t<T>,
70+
rfl::Vectorstring>()) {
71+
using VectorType = std::remove_cvref_t<T>;
72+
using ValueType = typename VectorType::value_type;
6873
if (type != capnp::DynamicValue::DATA) {
69-
return error("Could not cast to bytestring.");
74+
if constexpr (std::is_same<std::remove_cvref_t<T>,
75+
rfl::Bytestring>()) {
76+
return error("Could not cast to bytestring.");
77+
} else {
78+
return error("Could not cast to vectorstring.");
79+
}
7080
}
7181
const auto data = _var.val_.as<capnp::Data>();
72-
const auto begin = internal::ptr_cast<const std::byte*>(data.begin());
73-
return rfl::Bytestring(begin, begin + data.size());
82+
const auto begin = internal::ptr_cast<const ValueType*>(data.begin());
83+
return VectorType(begin, begin + data.size());
7484

7585
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
7686
if (type != capnp::DynamicValue::BOOL) {

include/rfl/capnproto/Writer.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "../Bytestring.hpp"
2121
#include "../Ref.hpp"
2222
#include "../Result.hpp"
23+
#include "../Vectorstring.hpp"
2324
#include "../always_false.hpp"
2425
#include "../internal/is_literal.hpp"
2526
#include "../internal/ptr_cast.hpp"
@@ -168,7 +169,9 @@ class Writer {
168169
_parent->val_.set(_parent->ix_++, _var.c_str());
169170

170171
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
171-
rfl::Bytestring>()) {
172+
rfl::Bytestring>() ||
173+
std::is_same<std::remove_cvref_t<T>,
174+
rfl::Vectorstring>()) {
172175
const auto array_ptr = kj::ArrayPtr<const kj::byte>(
173176
internal::ptr_cast<const unsigned char*>(_var.data()), _var.size());
174177
_parent->val_.set(_parent->ix_++, capnp::Data::Reader(array_ptr));

include/rfl/cbor/Reader.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "../Bytestring.hpp"
1313
#include "../Result.hpp"
14+
#include "../Vectorstring.hpp"
1415
#include "../always_false.hpp"
1516
#include "../internal/ptr_cast.hpp"
1617

@@ -73,13 +74,22 @@ class Reader {
7374
return _var.val_->as<std::string>();
7475

7576
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
76-
rfl::Bytestring>()) {
77+
rfl::Bytestring>() ||
78+
std::is_same<std::remove_cvref_t<T>,
79+
rfl::Vectorstring>()) {
80+
using VectorType = std::remove_cvref_t<T>;
81+
using ValueType = typename VectorType::value_type;
7782
if (!_var.val_->is_byte_string()) {
78-
return error("Could not cast to bytestring.");
83+
if constexpr (std::is_same<std::remove_cvref_t<T>,
84+
rfl::Bytestring>()) {
85+
return error("Could not cast to bytestring.");
86+
} else {
87+
return error("Could not cast to vectorstring.");
88+
}
7989
}
8090
const auto vec = _var.val_->as<std::vector<uint8_t>>();
81-
const auto data = internal::ptr_cast<const std::byte*>(vec.data());
82-
return rfl::Bytestring(data, data + vec.size());
91+
const auto data = internal::ptr_cast<const ValueType*>(vec.data());
92+
return VectorType(data, data + vec.size());
8393

8494
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
8595
if (!_var.val_->is_bool()) {

include/rfl/cbor/Writer.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "../Bytestring.hpp"
1818
#include "../Ref.hpp"
1919
#include "../Result.hpp"
20+
#include "../Vectorstring.hpp"
2021
#include "../always_false.hpp"
2122

2223
namespace rfl::cbor {
@@ -97,7 +98,9 @@ class Writer {
9798
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
9899
encoder_->string_value(_var);
99100
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
100-
rfl::Bytestring>()) {
101+
rfl::Bytestring>() ||
102+
std::is_same<std::remove_cvref_t<T>,
103+
rfl::Vectorstring>()) {
101104
encoder_->byte_string_value(_var);
102105
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
103106
encoder_->bool_value(_var);

0 commit comments

Comments
 (0)