From 927e8fe9eef958737ca02572ef2fa26b7f0eabd1 Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Thu, 17 Jul 2025 08:16:13 -0400 Subject: [PATCH 1/9] introduce Vectorstring alias to use vector as a byte string --- include/rfl/Bytestring.hpp | 1 + include/rfl/bson/Reader.hpp | 14 ++++++++++++++ include/rfl/bson/Writer.hpp | 4 +++- include/rfl/cbor/Reader.hpp | 9 +++++++++ include/rfl/cbor/Writer.hpp | 4 +++- include/rfl/msgpack/Reader.hpp | 9 +++++++++ include/rfl/msgpack/Writer.hpp | 2 +- include/rfl/ubjson/Reader.hpp | 9 +++++++++ include/rfl/ubjson/Writer.hpp | 4 +++- 9 files changed, 52 insertions(+), 4 deletions(-) diff --git a/include/rfl/Bytestring.hpp b/include/rfl/Bytestring.hpp index 197531e5..2f9ea572 100644 --- a/include/rfl/Bytestring.hpp +++ b/include/rfl/Bytestring.hpp @@ -7,6 +7,7 @@ namespace rfl { using Bytestring = std::vector; +using Vectorstring = std::vector; } // namespace rfl diff --git a/include/rfl/bson/Reader.hpp b/include/rfl/bson/Reader.hpp index 02de12ab..73a1e48e 100644 --- a/include/rfl/bson/Reader.hpp +++ b/include/rfl/bson/Reader.hpp @@ -125,6 +125,20 @@ struct Reader { internal::ptr_cast(value.v_binary.data); return rfl::Bytestring(data, data + value.v_binary.data_len); + } else if constexpr (std::is_same, + rfl::Vectorstring>()) { + if (btype != BSON_TYPE_BINARY) { + 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 " + "vectorstring."); + } + const auto data = + internal::ptr_cast(value.v_binary.data); + return rfl::Vectorstring(data, data + value.v_binary.data_len); + } else if constexpr (std::is_same, bool>()) { if (btype != BSON_TYPE_BOOL) { return error("Could not cast to boolean."); diff --git a/include/rfl/bson/Writer.hpp b/include/rfl/bson/Writer.hpp index a3cc42f8..cf1fc4b7 100644 --- a/include/rfl/bson/Writer.hpp +++ b/include/rfl/bson/Writer.hpp @@ -101,7 +101,9 @@ class Writer { bson_array_builder_append_utf8(_parent->val_, _var.c_str(), static_cast(_var.size())); } else if constexpr (std::is_same, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { bson_array_builder_append_binary( _parent->val_, BSON_SUBTYPE_BINARY, internal::ptr_cast(_var.data()), diff --git a/include/rfl/cbor/Reader.hpp b/include/rfl/cbor/Reader.hpp index e3a40538..1f9eb1ae 100644 --- a/include/rfl/cbor/Reader.hpp +++ b/include/rfl/cbor/Reader.hpp @@ -81,6 +81,15 @@ class Reader { const auto data = internal::ptr_cast(vec.data()); return rfl::Bytestring(data, data + vec.size()); + } else if constexpr (std::is_same, + rfl::Vectorstring()) { + if (!_var.val_->is_byte_string()) { + return error("Could not cast to vectorstring."); + } + const auto vec = _var.val_->as>(); + const auto data = internal::ptr_cast(vec.data()); + return rfl::Vectorstring(data, data + vec.size()); + } else if constexpr (std::is_same, bool>()) { if (!_var.val_->is_bool()) { return error("Could not cast to boolean."); diff --git a/include/rfl/cbor/Writer.hpp b/include/rfl/cbor/Writer.hpp index f31d2d61..d9521678 100644 --- a/include/rfl/cbor/Writer.hpp +++ b/include/rfl/cbor/Writer.hpp @@ -97,7 +97,9 @@ class Writer { if constexpr (std::is_same, std::string>()) { encoder_->string_value(_var); } else if constexpr (std::is_same, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { encoder_->byte_string_value(_var); } else if constexpr (std::is_same, bool>()) { encoder_->bool_value(_var); diff --git a/include/rfl/msgpack/Reader.hpp b/include/rfl/msgpack/Reader.hpp index 8e9039b7..ed18ef70 100644 --- a/include/rfl/msgpack/Reader.hpp +++ b/include/rfl/msgpack/Reader.hpp @@ -74,6 +74,15 @@ struct Reader { const auto data = internal::ptr_cast(bin.ptr); return rfl::Bytestring(data, data + bin.size); + } else if constexpr (std::is_same, + rfl::Vectorstring>()) { + if (type != MSGPACK_OBJECT_BIN) { + return error("Could not cast to a vectorstring."); + } + const auto bin = _var.via.bin; + const auto data = internal::ptr_cast(bin.ptr); + return rfl::Vectorstring(data, data + bin.size); + } else if constexpr (std::is_same, bool>()) { if (type != MSGPACK_OBJECT_BOOLEAN) { return error("Could not cast to boolean."); diff --git a/include/rfl/msgpack/Writer.hpp b/include/rfl/msgpack/Writer.hpp index 13cc4e4b..ff79963f 100644 --- a/include/rfl/msgpack/Writer.hpp +++ b/include/rfl/msgpack/Writer.hpp @@ -97,7 +97,7 @@ class Writer { if constexpr (std::is_same()) { msgpack_pack_str(pk_, _var.size()); msgpack_pack_str_body(pk_, _var.c_str(), _var.size()); - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same() || std::is_same()) { msgpack_pack_bin(pk_, _var.size()); msgpack_pack_bin_body(pk_, _var.data(), _var.size()); } else if constexpr (std::is_same()) { diff --git a/include/rfl/ubjson/Reader.hpp b/include/rfl/ubjson/Reader.hpp index 8ae37af7..2174254a 100644 --- a/include/rfl/ubjson/Reader.hpp +++ b/include/rfl/ubjson/Reader.hpp @@ -81,6 +81,15 @@ class Reader { const auto data = internal::ptr_cast(vec.data()); return rfl::Bytestring(data, data + vec.size()); + } else if constexpr (std::is_same, + rfl::Vectorstring>()) { + if (!_var.val_->is>()) { + return error("Could not cast to vectorstring."); + } + const auto vec = _var.val_->as>(); + const auto data = internal::ptr_cast(vec.data()); + return rfl::Vectorstring(data, data + vec.size()); + } else if constexpr (std::is_same, bool>()) { if (!_var.val_->is_bool()) { return error("Could not cast to boolean."); diff --git a/include/rfl/ubjson/Writer.hpp b/include/rfl/ubjson/Writer.hpp index 9c5905e8..2dcc57a5 100644 --- a/include/rfl/ubjson/Writer.hpp +++ b/include/rfl/ubjson/Writer.hpp @@ -97,7 +97,9 @@ class Writer { if constexpr (std::is_same, std::string>()) { encoder_->string_value(_var); } else if constexpr (std::is_same, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { encoder_->byte_string_value(_var); } else if constexpr (std::is_same, bool>()) { encoder_->bool_value(_var); From c9322827419898aaaaced073e54764d3b4e49e23 Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Thu, 17 Jul 2025 08:23:00 -0400 Subject: [PATCH 2/9] add tests --- tests/bson/test_vectorstring.cpp | 23 +++++++++++++++++++++++ tests/cbor/test_vectorstring.cpp | 20 ++++++++++++++++++++ tests/msgpack/test_vectorstring.cpp | 20 ++++++++++++++++++++ tests/ubjson/test_vectorstring.cpp | 20 ++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 tests/bson/test_vectorstring.cpp create mode 100644 tests/cbor/test_vectorstring.cpp create mode 100644 tests/msgpack/test_vectorstring.cpp create mode 100644 tests/ubjson/test_vectorstring.cpp diff --git a/tests/bson/test_vectorstring.cpp b/tests/bson/test_vectorstring.cpp new file mode 100644 index 00000000..0cea16d1 --- /dev/null +++ b/tests/bson/test_vectorstring.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_vectorstring { + +struct TestStruct { + rfl::Vectorstring vectorstring; + std::vector 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({vstr, vstr, vstr})}; + write_and_read(test); +} +} // namespace test_vectorstring diff --git a/tests/cbor/test_vectorstring.cpp b/tests/cbor/test_vectorstring.cpp new file mode 100644 index 00000000..dc424614 --- /dev/null +++ b/tests/cbor/test_vectorstring.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +#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 diff --git a/tests/msgpack/test_vectorstring.cpp b/tests/msgpack/test_vectorstring.cpp new file mode 100644 index 00000000..11e83a67 --- /dev/null +++ b/tests/msgpack/test_vectorstring.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +#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 \ No newline at end of file diff --git a/tests/ubjson/test_vectorstring.cpp b/tests/ubjson/test_vectorstring.cpp new file mode 100644 index 00000000..7e7c44ff --- /dev/null +++ b/tests/ubjson/test_vectorstring.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +#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 \ No newline at end of file From 9a82b6c004943573077340a43a158b80297864b5 Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Thu, 17 Jul 2025 08:33:37 -0400 Subject: [PATCH 3/9] add missing branch to bson writer --- include/rfl/bson/Writer.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/rfl/bson/Writer.hpp b/include/rfl/bson/Writer.hpp index cf1fc4b7..d27bf2cb 100644 --- a/include/rfl/bson/Writer.hpp +++ b/include/rfl/bson/Writer.hpp @@ -133,7 +133,9 @@ class Writer { static_cast(_name.size()), _var.c_str(), static_cast(_var.size())); } else if constexpr (std::is_same, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { bson_append_binary(_parent->val_, _name.data(), static_cast(_name.size()), BSON_SUBTYPE_BINARY, internal::ptr_cast(_var.data()), From 2a999137216ae33cf97f474543c2de339aaf271c Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Thu, 17 Jul 2025 08:35:46 -0400 Subject: [PATCH 4/9] fix compiler error --- include/rfl/cbor/Reader.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rfl/cbor/Reader.hpp b/include/rfl/cbor/Reader.hpp index 1f9eb1ae..e579f3d8 100644 --- a/include/rfl/cbor/Reader.hpp +++ b/include/rfl/cbor/Reader.hpp @@ -82,7 +82,7 @@ class Reader { return rfl::Bytestring(data, data + vec.size()); } else if constexpr (std::is_same, - rfl::Vectorstring()) { + rfl::Vectorstring>()) { if (!_var.val_->is_byte_string()) { return error("Could not cast to vectorstring."); } From a817d404ae84778525b36cdc782e92325f4d1cfe Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Thu, 17 Jul 2025 22:13:49 -0400 Subject: [PATCH 5/9] include parser specializations for vectorstring --- include/rfl.hpp | 1 + include/rfl/Bytestring.hpp | 1 - include/rfl/Vectorstring.hpp | 13 +++++++ include/rfl/parsing/Parser_vectorstring.hpp | 38 +++++++++++++++++++++ include/rfl/parsing/VectorParser.hpp | 3 ++ include/rfl/parsing/schema/Type.hpp | 2 ++ 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 include/rfl/Vectorstring.hpp create mode 100644 include/rfl/parsing/Parser_vectorstring.hpp diff --git a/include/rfl.hpp b/include/rfl.hpp index 7c1e1f1e..fb4fea60 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -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" diff --git a/include/rfl/Bytestring.hpp b/include/rfl/Bytestring.hpp index 2f9ea572..197531e5 100644 --- a/include/rfl/Bytestring.hpp +++ b/include/rfl/Bytestring.hpp @@ -7,7 +7,6 @@ namespace rfl { using Bytestring = std::vector; -using Vectorstring = std::vector; } // namespace rfl diff --git a/include/rfl/Vectorstring.hpp b/include/rfl/Vectorstring.hpp new file mode 100644 index 00000000..8d3d26c9 --- /dev/null +++ b/include/rfl/Vectorstring.hpp @@ -0,0 +1,13 @@ +#ifndef RFL_VECTORSTRING_HPP_ +#define RFL_VECTORSTRING_HPP_ + +#include +#include + +namespace rfl { + +using Vectorstring = std::vector; + +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser_vectorstring.hpp b/include/rfl/parsing/Parser_vectorstring.hpp new file mode 100644 index 00000000..480e77bc --- /dev/null +++ b/include/rfl/parsing/Parser_vectorstring.hpp @@ -0,0 +1,38 @@ +#ifndef RFL_PARSING_PARSER_VECTORSRING_HPP_ +#define RFL_PARSING_PARSER_VECTORSRING_HPP_ + +#include + +#include "../Vectorstring.hpp" +#include "../Result.hpp" +#include "Parser_base.hpp" +#include "schema/Type.hpp" + +namespace rfl::parsing { + +template + requires AreReaderAndWriter +struct Parser { + using InputVarType = typename R::InputVarType; + using ParentType = Parent; + + static Result read(const R& _r, + const InputVarType& _var) noexcept { + return _r.template to_basic_type(_var); + } + + template + 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* _definitions) { + return schema::Type{schema::Type::Vectorstring{}}; + } +}; + +} // namespace rfl::parsing + +#endif diff --git a/include/rfl/parsing/VectorParser.hpp b/include/rfl/parsing/VectorParser.hpp index b6ac9cfe..2c92762d 100644 --- a/include/rfl/parsing/VectorParser.hpp +++ b/include/rfl/parsing/VectorParser.hpp @@ -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, Bytestring>, "Cannot be a bytestring."); + static_assert(!std::is_same_v, Vectorstring>, + "Cannot be a vectorstring."); static Result read(const R& _r, const InputVarType& _var) noexcept { if constexpr (treat_as_map()) { diff --git a/include/rfl/parsing/schema/Type.hpp b/include/rfl/parsing/schema/Type.hpp index a5372308..49705c05 100644 --- a/include/rfl/parsing/schema/Type.hpp +++ b/include/rfl/parsing/schema/Type.hpp @@ -18,6 +18,8 @@ struct Type { struct Bytestring {}; + struct Vectorstring {}; + struct Int32 {}; struct Int64 {}; From e211b6868e2ca2530431093d21a444e3fe506e0b Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Thu, 17 Jul 2025 22:17:09 -0400 Subject: [PATCH 6/9] actually include the parser specialization header --- include/rfl/parsing/Parser.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index e7f2ed8d..a1a9c8a7 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -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" From b4c87c5c928d5380b8fdb308fde94192410252de Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Thu, 17 Jul 2025 22:22:53 -0400 Subject: [PATCH 7/9] add vectorstring to schema variant --- include/rfl/parsing/schema/Type.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rfl/parsing/schema/Type.hpp b/include/rfl/parsing/schema/Type.hpp index 49705c05..0515a9d9 100644 --- a/include/rfl/parsing/schema/Type.hpp +++ b/include/rfl/parsing/schema/Type.hpp @@ -90,7 +90,7 @@ struct Type { }; using VariantType = - rfl::Variant; From fcc8290ee593a23611d0e084c4393d04561e4803 Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Thu, 17 Jul 2025 23:33:07 -0400 Subject: [PATCH 8/9] allow usage of vectorstring everywhere + consolidate some templates --- include/rfl/avro/Reader.hpp | 17 ++++++++++--- include/rfl/avro/Writer.hpp | 5 +++- include/rfl/bson/Reader.hpp | 42 +++++++++++++++++--------------- include/rfl/bson/Writer.hpp | 1 + include/rfl/capnproto/Reader.hpp | 17 ++++++++++--- include/rfl/capnproto/Writer.hpp | 5 +++- include/rfl/cbor/Reader.hpp | 24 +++++++++--------- include/rfl/cbor/Writer.hpp | 1 + include/rfl/flexbuf/Reader.hpp | 17 ++++++++++--- include/rfl/flexbuf/Writer.hpp | 5 +++- include/rfl/generic/Reader.hpp | 1 - include/rfl/msgpack/Reader.hpp | 23 +++++++++-------- include/rfl/msgpack/Writer.hpp | 3 ++- include/rfl/ubjson/Reader.hpp | 23 +++++++++-------- include/rfl/ubjson/Writer.hpp | 1 + src/rfl/avro/to_schema.cpp | 3 ++- src/rfl/capnproto/to_schema.cpp | 3 ++- src/rfl/json/to_schema.cpp | 3 ++- 18 files changed, 118 insertions(+), 76 deletions(-) diff --git a/include/rfl/avro/Reader.hpp b/include/rfl/avro/Reader.hpp index 4c4724ba..e8e4c66c 100644 --- a/include/rfl/avro/Reader.hpp +++ b/include/rfl/avro/Reader.hpp @@ -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,23 @@ struct Reader { } return std::string(c_str, size - 1); } else if constexpr (std::is_same, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { + using VectorType = std::remove_cvref_t; 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, + rfl::Bytestring>()) { + return error("Could not cast to bytestring."); + } else { + return error("Could not cast to vectorstring."); + } } - const auto data = internal::ptr_cast(ptr); - return rfl::Bytestring(data, data + size); + const auto data = internal::ptr_cast(ptr); + return VectorType(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 e07c1a19..b530191d 100644 --- a/include/rfl/avro/Writer.hpp +++ b/include/rfl/avro/Writer.hpp @@ -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, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { auto var = _var; avro_value_set_bytes(_val, var.data(), var.size()); diff --git a/include/rfl/bson/Reader.hpp b/include/rfl/bson/Reader.hpp index 73a1e48e..9ac47c10 100644 --- a/include/rfl/bson/Reader.hpp +++ b/include/rfl/bson/Reader.hpp @@ -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,32 +113,33 @@ struct Reader { } } else if constexpr (std::is_same, - rfl::Bytestring>()) { - if (btype != BSON_TYPE_BINARY) { - return error("Could not cast to bytestring."); - } - if (value.v_binary.subtype != BSON_SUBTYPE_BINARY) { - return error( - "The BSON subtype must be a binary in order to read into a " - "bytestring."); - } - const auto data = - internal::ptr_cast(value.v_binary.data); - return rfl::Bytestring(data, data + value.v_binary.data_len); - - } else if constexpr (std::is_same, + rfl::Bytestring>() || + std::is_same, rfl::Vectorstring>()) { + using VectorType = std::remove_cvref_t; if (btype != BSON_TYPE_BINARY) { - return error("Could not cast to vectorstring."); + if constexpr (std::is_same, + 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 " - "vectorstring."); + if constexpr (std::is_same, + 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(value.v_binary.data); - return rfl::Vectorstring(data, data + value.v_binary.data_len); + internal::ptr_cast(value.v_binary.data); + return VectorType(data, data + value.v_binary.data_len); } else if constexpr (std::is_same, bool>()) { if (btype != BSON_TYPE_BOOL) { diff --git a/include/rfl/bson/Writer.hpp b/include/rfl/bson/Writer.hpp index d27bf2cb..c120350c 100644 --- a/include/rfl/bson/Writer.hpp +++ b/include/rfl/bson/Writer.hpp @@ -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" diff --git a/include/rfl/capnproto/Reader.hpp b/include/rfl/capnproto/Reader.hpp index 767eccf8..5e0c775e 100644 --- a/include/rfl/capnproto/Reader.hpp +++ b/include/rfl/capnproto/Reader.hpp @@ -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,21 @@ class Reader { return std::string(_var.val_.as().cStr()); } else if constexpr (std::is_same, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { + using VectorType = std::remove_cvref_t; if (type != capnp::DynamicValue::DATA) { - return error("Could not cast to bytestring."); + if constexpr (std::is_same, + rfl::Bytestring>()) { + return error("Could not cast to bytestring."); + } else { + return error("Could not cast to vectorstring."); + } } const auto data = _var.val_.as(); - const auto begin = internal::ptr_cast(data.begin()); - return rfl::Bytestring(begin, begin + data.size()); + const auto begin = internal::ptr_cast(data.begin()); + return VectorType(begin, begin + data.size()); } else if constexpr (std::is_same, bool>()) { if (type != capnp::DynamicValue::BOOL) { diff --git a/include/rfl/capnproto/Writer.hpp b/include/rfl/capnproto/Writer.hpp index 32c2e58a..7d7f0e31 100644 --- a/include/rfl/capnproto/Writer.hpp +++ b/include/rfl/capnproto/Writer.hpp @@ -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, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { const auto array_ptr = kj::ArrayPtr( internal::ptr_cast(_var.data()), _var.size()); _parent->val_.set(_parent->ix_++, capnp::Data::Reader(array_ptr)); diff --git a/include/rfl/cbor/Reader.hpp b/include/rfl/cbor/Reader.hpp index e579f3d8..5b0d45fb 100644 --- a/include/rfl/cbor/Reader.hpp +++ b/include/rfl/cbor/Reader.hpp @@ -11,6 +11,7 @@ #include "../Bytestring.hpp" #include "../Result.hpp" +#include "../Vectorstring.hpp" #include "../always_false.hpp" #include "../internal/ptr_cast.hpp" @@ -73,22 +74,21 @@ class Reader { return _var.val_->as(); } else if constexpr (std::is_same, - rfl::Bytestring>()) { - if (!_var.val_->is_byte_string()) { - return error("Could not cast to bytestring."); - } - const auto vec = _var.val_->as>(); - const auto data = internal::ptr_cast(vec.data()); - return rfl::Bytestring(data, data + vec.size()); - - } else if constexpr (std::is_same, + rfl::Bytestring>() || + std::is_same, rfl::Vectorstring>()) { + using VectorType = std::remove_cvref_t; if (!_var.val_->is_byte_string()) { - return error("Could not cast to vectorstring."); + if constexpr (std::is_same, + rfl::Bytestring>()) { + return error("Could not cast to bytestring."); + } else { + return error("Could not cast to vectorstring."); + } } const auto vec = _var.val_->as>(); - const auto data = internal::ptr_cast(vec.data()); - return rfl::Vectorstring(data, data + vec.size()); + const auto data = internal::ptr_cast(vec.data()); + return VectorType(data, data + vec.size()); } else if constexpr (std::is_same, bool>()) { if (!_var.val_->is_bool()) { diff --git a/include/rfl/cbor/Writer.hpp b/include/rfl/cbor/Writer.hpp index d9521678..28ef5d4b 100644 --- a/include/rfl/cbor/Writer.hpp +++ b/include/rfl/cbor/Writer.hpp @@ -17,6 +17,7 @@ #include "../Bytestring.hpp" #include "../Ref.hpp" #include "../Result.hpp" +#include "../Vectorstring.hpp" #include "../always_false.hpp" namespace rfl::cbor { diff --git a/include/rfl/flexbuf/Reader.hpp b/include/rfl/flexbuf/Reader.hpp index d23a3717..7f1bb352 100644 --- a/include/rfl/flexbuf/Reader.hpp +++ b/include/rfl/flexbuf/Reader.hpp @@ -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,21 @@ struct Reader { return std::string(_var.AsString().c_str()); } else if constexpr (std::is_same, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { + using VectorType = std::remove_cvref_t; if (!_var.IsBlob()) { - return error("Could not cast to a bytestring."); + if constexpr (std::is_same, + 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(blob.data()); - return rfl::Bytestring(data, data + blob.size()); + const auto data = internal::ptr_cast(blob.data()); + return VectorType(data, data + blob.size()); } else if constexpr (std::is_same, bool>()) { if (!_var.IsBool()) { diff --git a/include/rfl/flexbuf/Writer.hpp b/include/rfl/flexbuf/Writer.hpp index 90b6302b..9f5f4703 100644 --- a/include/rfl/flexbuf/Writer.hpp +++ b/include/rfl/flexbuf/Writer.hpp @@ -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::string>()) { fbb_->String(_name.data(), _var); } else if constexpr (std::is_same, - rfl::Bytestring>()) { + rfl::Bytestring>() || + std::is_same, + rfl::Vectorstring>()) { fbb_->Blob(_name.data(), _var.data(), _var.size()); } else if constexpr (std::is_same, bool>()) { fbb_->Bool(_name.data(), _var); diff --git a/include/rfl/generic/Reader.hpp b/include/rfl/generic/Reader.hpp index 8d679448..df5e7c4c 100644 --- a/include/rfl/generic/Reader.hpp +++ b/include/rfl/generic/Reader.hpp @@ -11,7 +11,6 @@ #include #include -#include "../Bytestring.hpp" #include "../Generic.hpp" #include "../Result.hpp" #include "../always_false.hpp" diff --git a/include/rfl/msgpack/Reader.hpp b/include/rfl/msgpack/Reader.hpp index ed18ef70..9777719a 100644 --- a/include/rfl/msgpack/Reader.hpp +++ b/include/rfl/msgpack/Reader.hpp @@ -11,6 +11,7 @@ #include "../Bytestring.hpp" #include "../Result.hpp" +#include "../Vectorstring.hpp" #include "../always_false.hpp" #include "../internal/ptr_cast.hpp" @@ -66,22 +67,20 @@ struct Reader { return std::string(str.ptr, str.size); } else if constexpr (std::is_same, - rfl::Bytestring>()) { - if (type != MSGPACK_OBJECT_BIN) { - return error("Could not cast to a bytestring."); - } - const auto bin = _var.via.bin; - const auto data = internal::ptr_cast(bin.ptr); - return rfl::Bytestring(data, data + bin.size); - - } else if constexpr (std::is_same, + rfl::Bytestring>() || + std::is_same, rfl::Vectorstring>()) { + using VectorType = std::remove_cvref_t; if (type != MSGPACK_OBJECT_BIN) { - return error("Could not cast to a vectorstring."); + if constexpr (std::is_same, 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(bin.ptr); - return rfl::Vectorstring(data, data + bin.size); + const auto data = internal::ptr_cast(bin.ptr); + return VectorType(data, data + bin.size); } else if constexpr (std::is_same, bool>()) { if (type != MSGPACK_OBJECT_BOOLEAN) { diff --git a/include/rfl/msgpack/Writer.hpp b/include/rfl/msgpack/Writer.hpp index ff79963f..4f1e6767 100644 --- a/include/rfl/msgpack/Writer.hpp +++ b/include/rfl/msgpack/Writer.hpp @@ -14,10 +14,11 @@ #include #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 { diff --git a/include/rfl/ubjson/Reader.hpp b/include/rfl/ubjson/Reader.hpp index 2174254a..1cd8421e 100644 --- a/include/rfl/ubjson/Reader.hpp +++ b/include/rfl/ubjson/Reader.hpp @@ -11,6 +11,7 @@ #include "../Bytestring.hpp" #include "../Result.hpp" +#include "../Vectorstring.hpp" #include "../always_false.hpp" #include "../internal/ptr_cast.hpp" @@ -73,22 +74,20 @@ class Reader { return _var.val_->as(); } else if constexpr (std::is_same, - rfl::Bytestring>()) { - if (!_var.val_->is>()) { - return error("Could not cast to bytestring."); - } - const auto vec = _var.val_->as>(); - const auto data = internal::ptr_cast(vec.data()); - return rfl::Bytestring(data, data + vec.size()); - - } else if constexpr (std::is_same, + rfl::Bytestring>() || + std::is_same, rfl::Vectorstring>()) { + using VectorType = std::remove_cvref_t; if (!_var.val_->is>()) { - return error("Could not cast to vectorstring."); + if constexpr (std::is_same, rfl::Bytestring>()) { + return error("Could not cast to bytestring."); + } else { + return error("Could not cast to vectorstring."); + } } const auto vec = _var.val_->as>(); - const auto data = internal::ptr_cast(vec.data()); - return rfl::Vectorstring(data, data + vec.size()); + const auto data = internal::ptr_cast(vec.data()); + return VectorType(data, data + vec.size()); } else if constexpr (std::is_same, bool>()) { if (!_var.val_->is_bool()) { diff --git a/include/rfl/ubjson/Writer.hpp b/include/rfl/ubjson/Writer.hpp index 2dcc57a5..4208e611 100644 --- a/include/rfl/ubjson/Writer.hpp +++ b/include/rfl/ubjson/Writer.hpp @@ -17,6 +17,7 @@ #include "../Bytestring.hpp" #include "../Ref.hpp" #include "../Result.hpp" +#include "../Vectorstring.hpp" #include "../always_false.hpp" namespace rfl::ubjson { diff --git a/src/rfl/avro/to_schema.cpp b/src/rfl/avro/to_schema.cpp index a9c86fc6..1e40d938 100644 --- a/src/rfl/avro/to_schema.cpp +++ b/src/rfl/avro/to_schema.cpp @@ -50,7 +50,8 @@ schema::Type type_to_avro_schema_type( if constexpr (std::is_same()) { return schema::Type{.value = schema::Type::Boolean{}}; - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same() || + std::is_same()) { return schema::Type{.value = schema::Type::Bytes{}}; } else if constexpr (std::is_same() || diff --git a/src/rfl/capnproto/to_schema.cpp b/src/rfl/capnproto/to_schema.cpp index 3057a60e..dbef44c4 100644 --- a/src/rfl/capnproto/to_schema.cpp +++ b/src/rfl/capnproto/to_schema.cpp @@ -150,7 +150,8 @@ schema::Type type_to_capnproto_schema_type( if constexpr (std::is_same()) { return schema::Type{.value = schema::Type::Bool{}}; - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same() || + std::is_same()) { return schema::Type{.value = schema::Type::Data{}}; } else if constexpr (std::is_same() || diff --git a/src/rfl/json/to_schema.cpp b/src/rfl/json/to_schema.cpp index d487d922..33274695 100644 --- a/src/rfl/json/to_schema.cpp +++ b/src/rfl/json/to_schema.cpp @@ -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() || - std::is_same()) { + std::is_same() || + std::is_same()) { return schema::Type{.value = schema::Type::String{}}; } else if constexpr (std::is_same()) { From 711cab3c08ea5dc46a6ed762011c977e88de0bba Mon Sep 17 00:00:00 2001 From: Brett Jia Date: Thu, 17 Jul 2025 23:57:18 -0400 Subject: [PATCH 9/9] add ValueType to fix compiler error --- include/rfl/avro/Reader.hpp | 3 ++- include/rfl/bson/Reader.hpp | 3 ++- include/rfl/capnproto/Reader.hpp | 3 ++- include/rfl/cbor/Reader.hpp | 3 ++- include/rfl/flexbuf/Reader.hpp | 3 ++- include/rfl/msgpack/Reader.hpp | 3 ++- include/rfl/ubjson/Reader.hpp | 3 ++- 7 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/rfl/avro/Reader.hpp b/include/rfl/avro/Reader.hpp index e8e4c66c..faea20c4 100644 --- a/include/rfl/avro/Reader.hpp +++ b/include/rfl/avro/Reader.hpp @@ -73,6 +73,7 @@ struct Reader { std::is_same, rfl::Vectorstring>()) { using VectorType = std::remove_cvref_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); @@ -84,7 +85,7 @@ struct Reader { return error("Could not cast to vectorstring."); } } - const auto data = internal::ptr_cast(ptr); + const auto data = internal::ptr_cast(ptr); return VectorType(data, data + size); } else if constexpr (std::is_same, bool>()) { if (type != AVRO_BOOLEAN) { diff --git a/include/rfl/bson/Reader.hpp b/include/rfl/bson/Reader.hpp index 9ac47c10..d7736bef 100644 --- a/include/rfl/bson/Reader.hpp +++ b/include/rfl/bson/Reader.hpp @@ -117,6 +117,7 @@ struct Reader { std::is_same, rfl::Vectorstring>()) { using VectorType = std::remove_cvref_t; + using ValueType = typename VectorType::value_type; if (btype != BSON_TYPE_BINARY) { if constexpr (std::is_same, rfl::Bytestring>()) { @@ -138,7 +139,7 @@ struct Reader { } } const auto data = - internal::ptr_cast(value.v_binary.data); + internal::ptr_cast(value.v_binary.data); return VectorType(data, data + value.v_binary.data_len); } else if constexpr (std::is_same, bool>()) { diff --git a/include/rfl/capnproto/Reader.hpp b/include/rfl/capnproto/Reader.hpp index 5e0c775e..7d3a74f4 100644 --- a/include/rfl/capnproto/Reader.hpp +++ b/include/rfl/capnproto/Reader.hpp @@ -69,6 +69,7 @@ class Reader { std::is_same, rfl::Vectorstring>()) { using VectorType = std::remove_cvref_t; + using ValueType = typename VectorType::value_type; if (type != capnp::DynamicValue::DATA) { if constexpr (std::is_same, rfl::Bytestring>()) { @@ -78,7 +79,7 @@ class Reader { } } const auto data = _var.val_.as(); - const auto begin = internal::ptr_cast(data.begin()); + const auto begin = internal::ptr_cast(data.begin()); return VectorType(begin, begin + data.size()); } else if constexpr (std::is_same, bool>()) { diff --git a/include/rfl/cbor/Reader.hpp b/include/rfl/cbor/Reader.hpp index 5b0d45fb..3672c974 100644 --- a/include/rfl/cbor/Reader.hpp +++ b/include/rfl/cbor/Reader.hpp @@ -78,6 +78,7 @@ class Reader { std::is_same, rfl::Vectorstring>()) { using VectorType = std::remove_cvref_t; + using ValueType = typename VectorType::value_type; if (!_var.val_->is_byte_string()) { if constexpr (std::is_same, rfl::Bytestring>()) { @@ -87,7 +88,7 @@ class Reader { } } const auto vec = _var.val_->as>(); - const auto data = internal::ptr_cast(vec.data()); + const auto data = internal::ptr_cast(vec.data()); return VectorType(data, data + vec.size()); } else if constexpr (std::is_same, bool>()) { diff --git a/include/rfl/flexbuf/Reader.hpp b/include/rfl/flexbuf/Reader.hpp index 7f1bb352..8772be56 100644 --- a/include/rfl/flexbuf/Reader.hpp +++ b/include/rfl/flexbuf/Reader.hpp @@ -81,6 +81,7 @@ struct Reader { std::is_same, rfl::Vectorstring>()) { using VectorType = std::remove_cvref_t; + using ValueType = typename VectorType::value_type; if (!_var.IsBlob()) { if constexpr (std::is_same, rfl::Bytestring>()) { @@ -90,7 +91,7 @@ struct Reader { } } const auto blob = _var.AsBlob(); - const auto data = internal::ptr_cast(blob.data()); + const auto data = internal::ptr_cast(blob.data()); return VectorType(data, data + blob.size()); } else if constexpr (std::is_same, bool>()) { diff --git a/include/rfl/msgpack/Reader.hpp b/include/rfl/msgpack/Reader.hpp index 9777719a..71bbc946 100644 --- a/include/rfl/msgpack/Reader.hpp +++ b/include/rfl/msgpack/Reader.hpp @@ -71,6 +71,7 @@ struct Reader { std::is_same, rfl::Vectorstring>()) { using VectorType = std::remove_cvref_t; + using ValueType = typename VectorType::value_type; if (type != MSGPACK_OBJECT_BIN) { if constexpr (std::is_same, rfl::Bytestring>()) { return error("Could not cast to bytestring."); @@ -79,7 +80,7 @@ struct Reader { } } const auto bin = _var.via.bin; - const auto data = internal::ptr_cast(bin.ptr); + const auto data = internal::ptr_cast(bin.ptr); return VectorType(data, data + bin.size); } else if constexpr (std::is_same, bool>()) { diff --git a/include/rfl/ubjson/Reader.hpp b/include/rfl/ubjson/Reader.hpp index 1cd8421e..cb359f21 100644 --- a/include/rfl/ubjson/Reader.hpp +++ b/include/rfl/ubjson/Reader.hpp @@ -78,6 +78,7 @@ class Reader { std::is_same, rfl::Vectorstring>()) { using VectorType = std::remove_cvref_t; + using ValueType = typename VectorType::value_type; if (!_var.val_->is>()) { if constexpr (std::is_same, rfl::Bytestring>()) { return error("Could not cast to bytestring."); @@ -86,7 +87,7 @@ class Reader { } } const auto vec = _var.val_->as>(); - const auto data = internal::ptr_cast(vec.data()); + const auto data = internal::ptr_cast(vec.data()); return VectorType(data, data + vec.size()); } else if constexpr (std::is_same, bool>()) {