Skip to content

Commit d675bfa

Browse files
Added a separate parser for the Bytestring to avoid it being serialized as an array; #407 (#408)
1 parent ef0b0b4 commit d675bfa

File tree

6 files changed

+47
-13
lines changed

6 files changed

+47
-13
lines changed

docs/bytestring.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ using Bytestring = std::vector<std::byte>;
1010
}
1111
```
1212
13-
Bytestrings are supported by BSON, CBOR, flexbuffers and msgpack. Textual formats
14-
do not support them.
13+
Bytestrings are supported by binary formats such as BSON, CBOR, flexbuffers and msgpack.
14+
Textual formats do not support them.

include/rfl/Bytestring.hpp

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

77
namespace rfl {
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-
};
8+
9+
using Bytestring = std::vector<std::byte>;
1510

1611
} // namespace rfl
1712

include/rfl/parsing/Parser.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Parser_array.hpp"
55
#include "Parser_base.hpp"
66
#include "Parser_box.hpp"
7+
#include "Parser_bytestring.hpp"
78
#include "Parser_c_array.hpp"
89
#include "Parser_default.hpp"
910
#include "Parser_duration.hpp"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef RFL_PARSING_PARSER_BYTESTRING_HPP_
2+
#define RFL_PARSING_PARSER_BYTESTRING_HPP_
3+
4+
#include <map>
5+
6+
#include "../Bytestring.hpp"
7+
#include "../Result.hpp"
8+
#include "Parser_base.hpp"
9+
#include "schema/Type.hpp"
10+
11+
namespace rfl::parsing {
12+
13+
template <class R, class W, class ProcessorsType>
14+
requires AreReaderAndWriter<R, W, Bytestring>
15+
struct Parser<R, W, Bytestring, ProcessorsType> {
16+
using InputVarType = typename R::InputVarType;
17+
using ParentType = Parent<W>;
18+
19+
static Result<Bytestring> read(const R& _r,
20+
const InputVarType& _var) noexcept {
21+
return _r.template to_basic_type<Bytestring>(_var);
22+
}
23+
24+
template <class P>
25+
static void write(const W& _w, const Bytestring& _b,
26+
const P& _parent) noexcept {
27+
ParentType::add_value(_w, _b, _parent);
28+
}
29+
30+
static schema::Type to_schema(
31+
std::map<std::string, schema::Type>* _definitions) {
32+
return schema::Type{schema::Type::Bytestring{}};
33+
}
34+
};
35+
36+
} // namespace rfl::parsing
37+
38+
#endif

include/rfl/parsing/Parser_default.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <stdexcept>
66
#include <type_traits>
77

8-
#include "../Bytestring.hpp"
98
#include "../Result.hpp"
109
#include "../always_false.hpp"
1110
#include "../from_named_tuple.hpp"
@@ -152,9 +151,6 @@ struct Parser {
152151
if constexpr (std::is_same<U, bool>()) {
153152
return Type{Type::Boolean{}};
154153

155-
} else if constexpr (std::is_same<U, rfl::Bytestring>()) {
156-
return Type{Type::Bytestring{}};
157-
158154
} else if constexpr (std::is_same<U, std::int32_t>()) {
159155
return Type{Type::Int32{}};
160156

include/rfl/parsing/VectorParser.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <string>
88
#include <type_traits>
99

10+
#include "../Bytestring.hpp"
1011
#include "../Result.hpp"
1112
#include "../always_false.hpp"
1213
#include "MapParser.hpp"
@@ -37,6 +38,9 @@ struct VectorParser {
3738

3839
using T = typename VecType::value_type;
3940

41+
static_assert(!std::is_same_v<std::remove_cvref_t<VecType>, Bytestring>,
42+
"Cannot be a bytestring.");
43+
4044
static Result<VecType> read(const R& _r, const InputVarType& _var) noexcept {
4145
if constexpr (treat_as_map()) {
4246
return MapParser<R, W, VecType, ProcessorsType>::read(_r, _var);

0 commit comments

Comments
 (0)