Serialize/Deserialize vector<char> as bytestring (#466)

This commit is contained in:
Brett Jia
2025-07-20 12:57:37 +02:00
committed by GitHub
parent 908ee226a9
commit 372345888c
28 changed files with 284 additions and 45 deletions
+1
View File
@@ -45,6 +45,7 @@
#include "rfl/UnderlyingEnums.hpp"
#include "rfl/Validator.hpp"
#include "rfl/Variant.hpp"
#include "rfl/Vectorstring.hpp"
#include "rfl/always_false.hpp"
#include "rfl/apply.hpp"
#include "rfl/as.hpp"
+13
View File
@@ -0,0 +1,13 @@
#ifndef RFL_VECTORSTRING_HPP_
#define RFL_VECTORSTRING_HPP_
#include <cstddef>
#include <vector>
namespace rfl {
using Vectorstring = std::vector<char>;
} // namespace rfl
#endif
+14 -4
View File
@@ -12,6 +12,7 @@
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/is_literal.hpp"
#include "../parsing/schemaful/IsSchemafulReader.hpp"
@@ -68,15 +69,24 @@ struct Reader {
}
return std::string(c_str, size - 1);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
using VectorType = std::remove_cvref_t<T>;
using ValueType = typename VectorType::value_type;
const void* ptr = nullptr;
size_t size = 0;
const auto err = avro_value_get_bytes(_var.val_, &ptr, &size);
if (err) {
return error("Could not cast to bytestring.");
if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
return error("Could not cast to bytestring.");
} else {
return error("Could not cast to vectorstring.");
}
}
const auto data = internal::ptr_cast<const std::byte*>(ptr);
return rfl::Bytestring(data, data + size);
const auto data = internal::ptr_cast<const ValueType*>(ptr);
return VectorType(data, data + size);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (type != AVRO_BOOLEAN) {
return rfl::error("Could not cast to boolean.");
+4 -1
View File
@@ -19,6 +19,7 @@
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/is_literal.hpp"
@@ -186,7 +187,9 @@ class Writer {
avro_value_set_string_len(_val, _var.c_str(), _var.size() + 1);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
auto var = _var;
avro_value_set_bytes(_val, var.data(), var.size());
+24 -7
View File
@@ -21,6 +21,7 @@
#include "../Box.hpp"
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"
@@ -112,18 +113,34 @@ struct Reader {
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
using VectorType = std::remove_cvref_t<T>;
using ValueType = typename VectorType::value_type;
if (btype != BSON_TYPE_BINARY) {
return error("Could not cast to bytestring.");
if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
return error("Could not cast to bytestring.");
} else {
return error("Could not cast to vectorstring.");
}
}
if (value.v_binary.subtype != BSON_SUBTYPE_BINARY) {
return error(
"The BSON subtype must be a binary in order to read into a "
"bytestring.");
if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
return error(
"The BSON subtype must be a binary in order to read into a "
"bytestring.");
} else {
return error(
"The BSON subtype must be a binary in order to read into a "
"vectorstring.");
}
}
const auto data =
internal::ptr_cast<const std::byte*>(value.v_binary.data);
return rfl::Bytestring(data, data + value.v_binary.data_len);
internal::ptr_cast<const ValueType*>(value.v_binary.data);
return VectorType(data, data + value.v_binary.data_len);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (btype != BSON_TYPE_BOOL) {
+7 -2
View File
@@ -18,6 +18,7 @@
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"
@@ -101,7 +102,9 @@ class Writer {
bson_array_builder_append_utf8(_parent->val_, _var.c_str(),
static_cast<int>(_var.size()));
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
bson_array_builder_append_binary(
_parent->val_, BSON_SUBTYPE_BINARY,
internal::ptr_cast<const uint8_t*>(_var.data()),
@@ -131,7 +134,9 @@ class Writer {
static_cast<int>(_name.size()), _var.c_str(),
static_cast<int>(_var.size()));
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
bson_append_binary(_parent->val_, _name.data(),
static_cast<int>(_name.size()), BSON_SUBTYPE_BINARY,
internal::ptr_cast<const uint8_t*>(_var.data()),
+14 -4
View File
@@ -12,6 +12,7 @@
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/is_literal.hpp"
#include "../internal/ptr_cast.hpp"
@@ -64,13 +65,22 @@ class Reader {
return std::string(_var.val_.as<capnp::Text>().cStr());
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
using VectorType = std::remove_cvref_t<T>;
using ValueType = typename VectorType::value_type;
if (type != capnp::DynamicValue::DATA) {
return error("Could not cast to bytestring.");
if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
return error("Could not cast to bytestring.");
} else {
return error("Could not cast to vectorstring.");
}
}
const auto data = _var.val_.as<capnp::Data>();
const auto begin = internal::ptr_cast<const std::byte*>(data.begin());
return rfl::Bytestring(begin, begin + data.size());
const auto begin = internal::ptr_cast<const ValueType*>(data.begin());
return VectorType(begin, begin + data.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (type != capnp::DynamicValue::BOOL) {
+4 -1
View File
@@ -20,6 +20,7 @@
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/is_literal.hpp"
#include "../internal/ptr_cast.hpp"
@@ -168,7 +169,9 @@ class Writer {
_parent->val_.set(_parent->ix_++, _var.c_str());
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
const auto array_ptr = kj::ArrayPtr<const kj::byte>(
internal::ptr_cast<const unsigned char*>(_var.data()), _var.size());
_parent->val_.set(_parent->ix_++, capnp::Data::Reader(array_ptr));
+14 -4
View File
@@ -11,6 +11,7 @@
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"
@@ -73,13 +74,22 @@ class Reader {
return _var.val_->as<std::string>();
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
using VectorType = std::remove_cvref_t<T>;
using ValueType = typename VectorType::value_type;
if (!_var.val_->is_byte_string()) {
return error("Could not cast to bytestring.");
if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
return error("Could not cast to bytestring.");
} else {
return error("Could not cast to vectorstring.");
}
}
const auto vec = _var.val_->as<std::vector<uint8_t>>();
const auto data = internal::ptr_cast<const std::byte*>(vec.data());
return rfl::Bytestring(data, data + vec.size());
const auto data = internal::ptr_cast<const ValueType*>(vec.data());
return VectorType(data, data + vec.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var.val_->is_bool()) {
+4 -1
View File
@@ -17,6 +17,7 @@
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
namespace rfl::cbor {
@@ -97,7 +98,9 @@ class Writer {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
encoder_->string_value(_var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
encoder_->byte_string_value(_var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
encoder_->bool_value(_var);
+14 -4
View File
@@ -15,6 +15,7 @@
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"
@@ -76,13 +77,22 @@ struct Reader {
return std::string(_var.AsString().c_str());
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
using VectorType = std::remove_cvref_t<T>;
using ValueType = typename VectorType::value_type;
if (!_var.IsBlob()) {
return error("Could not cast to a bytestring.");
if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
return error("Could not cast to bytestring.");
} else {
return error("Could not cast to vectorstring.");
}
}
const auto blob = _var.AsBlob();
const auto data = internal::ptr_cast<const std::byte*>(blob.data());
return rfl::Bytestring(data, data + blob.size());
const auto data = internal::ptr_cast<const ValueType*>(blob.data());
return VectorType(data, data + blob.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var.IsBool()) {
+4 -1
View File
@@ -18,6 +18,7 @@
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
namespace rfl {
@@ -96,7 +97,9 @@ struct Writer {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
fbb_->String(_name.data(), _var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
fbb_->Blob(_name.data(), _var.data(), _var.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
fbb_->Bool(_name.data(), _var);
-1
View File
@@ -11,7 +11,6 @@
#include <type_traits>
#include <vector>
#include "../Bytestring.hpp"
#include "../Generic.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
+13 -4
View File
@@ -11,6 +11,7 @@
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"
@@ -66,13 +67,21 @@ struct Reader {
return std::string(str.ptr, str.size);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
using VectorType = std::remove_cvref_t<T>;
using ValueType = typename VectorType::value_type;
if (type != MSGPACK_OBJECT_BIN) {
return error("Could not cast to a bytestring.");
if constexpr (std::is_same<std::remove_cvref_t<T>, rfl::Bytestring>()) {
return error("Could not cast to bytestring.");
} else {
return error("Could not cast to vectorstring.");
}
}
const auto bin = _var.via.bin;
const auto data = internal::ptr_cast<const std::byte*>(bin.ptr);
return rfl::Bytestring(data, data + bin.size);
const auto data = internal::ptr_cast<const ValueType*>(bin.ptr);
return VectorType(data, data + bin.size);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (type != MSGPACK_OBJECT_BOOLEAN) {
+3 -2
View File
@@ -14,10 +14,11 @@
#include <vector>
#include "../Box.hpp"
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../Bytestring.hpp"
namespace rfl::msgpack {
@@ -97,7 +98,7 @@ class Writer {
if constexpr (std::is_same<Type, std::string>()) {
msgpack_pack_str(pk_, _var.size());
msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
} else if constexpr (std::is_same<Type, rfl::Bytestring>()) {
} else if constexpr (std::is_same<Type, rfl::Bytestring>() || std::is_same<Type, rfl::Vectorstring>()) {
msgpack_pack_bin(pk_, _var.size());
msgpack_pack_bin_body(pk_, _var.data(), _var.size());
} else if constexpr (std::is_same<Type, bool>()) {
+1
View File
@@ -28,6 +28,7 @@
#include "Parser_tuple.hpp"
#include "Parser_unique_ptr.hpp"
#include "Parser_variant.hpp"
#include "Parser_vectorstring.hpp"
#include "Parser_vector_like.hpp"
#include "Parser_wstring.hpp"
@@ -0,0 +1,38 @@
#ifndef RFL_PARSING_PARSER_VECTORSRING_HPP_
#define RFL_PARSING_PARSER_VECTORSRING_HPP_
#include <map>
#include "../Vectorstring.hpp"
#include "../Result.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"
namespace rfl::parsing {
template <class R, class W, class ProcessorsType>
requires AreReaderAndWriter<R, W, Vectorstring>
struct Parser<R, W, Vectorstring, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using ParentType = Parent<W>;
static Result<Vectorstring> read(const R& _r,
const InputVarType& _var) noexcept {
return _r.template to_basic_type<Vectorstring>(_var);
}
template <class P>
static void write(const W& _w, const Vectorstring& _b,
const P& _parent) noexcept {
ParentType::add_value(_w, _b, _parent);
}
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
return schema::Type{schema::Type::Vectorstring{}};
}
};
} // namespace rfl::parsing
#endif
+3
View File
@@ -9,6 +9,7 @@
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "MapParser.hpp"
#include "Parent.hpp"
@@ -40,6 +41,8 @@ struct VectorParser {
static_assert(!std::is_same_v<std::remove_cvref_t<VecType>, Bytestring>,
"Cannot be a bytestring.");
static_assert(!std::is_same_v<std::remove_cvref_t<VecType>, Vectorstring>,
"Cannot be a vectorstring.");
static Result<VecType> read(const R& _r, const InputVarType& _var) noexcept {
if constexpr (treat_as_map()) {
+3 -1
View File
@@ -18,6 +18,8 @@ struct Type {
struct Bytestring {};
struct Vectorstring {};
struct Int32 {};
struct Int64 {};
@@ -88,7 +90,7 @@ struct Type {
};
using VariantType =
rfl::Variant<Boolean, Bytestring, Int32, Int64, UInt32, UInt64, Integer,
rfl::Variant<Boolean, Bytestring, Vectorstring, Int32, Int64, UInt32, UInt64, Integer,
Float, Double, String, AnyOf, Description,
FixedSizeTypedArray, Literal, Object, Optional, Reference,
StringMap, Tuple, TypedArray, Validated>;
+13 -4
View File
@@ -11,6 +11,7 @@
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"
@@ -73,13 +74,21 @@ class Reader {
return _var.val_->as<std::string>();
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
using VectorType = std::remove_cvref_t<T>;
using ValueType = typename VectorType::value_type;
if (!_var.val_->is<std::vector<uint8_t>>()) {
return error("Could not cast to bytestring.");
if constexpr (std::is_same<std::remove_cvref_t<T>, rfl::Bytestring>()) {
return error("Could not cast to bytestring.");
} else {
return error("Could not cast to vectorstring.");
}
}
const auto vec = _var.val_->as<std::vector<uint8_t>>();
const auto data = internal::ptr_cast<const std::byte*>(vec.data());
return rfl::Bytestring(data, data + vec.size());
const auto data = internal::ptr_cast<const ValueType*>(vec.data());
return VectorType(data, data + vec.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var.val_->is_bool()) {
+4 -1
View File
@@ -17,6 +17,7 @@
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
namespace rfl::ubjson {
@@ -97,7 +98,9 @@ class Writer {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
encoder_->string_value(_var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
encoder_->byte_string_value(_var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
encoder_->bool_value(_var);
+2 -1
View File
@@ -50,7 +50,8 @@ 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::Bytestring>()) {
} else if constexpr (std::is_same<T, Type::Bytestring>() ||
std::is_same<T, Type::Vectorstring>()) {
return schema::Type{.value = schema::Type::Bytes{}};
} else if constexpr (std::is_same<T, Type::Int32>() ||
+2 -1
View File
@@ -150,7 +150,8 @@ 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::Bytestring>()) {
} else if constexpr (std::is_same<T, Type::Bytestring>() ||
std::is_same<T, Type::Vectorstring>()) {
return schema::Type{.value = schema::Type::Data{}};
} else if constexpr (std::is_same<T, Type::Int32>() ||
+2 -1
View File
@@ -196,7 +196,8 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
return schema::Type{.value = schema::Type::Number{}};
} else if constexpr (std::is_same<T, Type::String>() ||
std::is_same<T, Type::Bytestring>()) {
std::is_same<T, Type::Bytestring>() ||
std::is_same<T, Type::Vectorstring>()) {
return schema::Type{.value = schema::Type::String{}};
} else if constexpr (std::is_same<T, Type::AnyOf>()) {
+23
View File
@@ -0,0 +1,23 @@
#include <iostream>
#include <rfl.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_vectorstring {
struct TestStruct {
rfl::Vectorstring vectorstring;
std::vector<rfl::Vectorstring> vectorstrings;
};
TEST(bson, test_vectorstring) {
const auto vstr = rfl::Vectorstring(
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'});
const auto test = TestStruct{
.vectorstring = vstr,
.vectorstrings = std::vector<rfl::Vectorstring>({vstr, vstr, vstr})};
write_and_read(test);
}
} // namespace test_vectorstring
+20
View File
@@ -0,0 +1,20 @@
#include <iostream>
#include <rfl.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_vectorstring {
struct TestStruct {
rfl::Vectorstring vectorstring;
};
TEST(cbor, test_vectorstring) {
const auto test = TestStruct{
.vectorstring = rfl::Vectorstring({'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'})};
write_and_read(test);
}
} // namespace test_vectorstring
+20
View File
@@ -0,0 +1,20 @@
#include <iostream>
#include <rfl.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_vectorstring {
struct TestStruct {
rfl::Vectorstring vectorstring;
};
TEST(msgpack, test_vectorstring) {
const auto test =
TestStruct{.vectorstring = rfl::Vectorstring({'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'})};
write_and_read(test);
}
} // namespace test_vectorstring
+20
View File
@@ -0,0 +1,20 @@
#include <iostream>
#include <rfl.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_vectorstring {
struct TestStruct {
rfl::Vectorstring vectorstring;
};
TEST(ubjson, test_vectorstring) {
const auto test =
TestStruct{.vectorstring = rfl::Vectorstring({'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'})};
write_and_read(test);
}
} // namespace test_vectorstring