Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions include/rfl/Vectorstring.hpp
Original file line number Diff line number Diff line change
@@ -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
18 changes: 14 additions & 4 deletions include/rfl/avro/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.");
Expand Down
5 changes: 4 additions & 1 deletion include/rfl/avro/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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());

Expand Down
31 changes: 24 additions & 7 deletions include/rfl/bson/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions include/rfl/bson/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down
18 changes: 14 additions & 4 deletions include/rfl/capnproto/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion include/rfl/capnproto/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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));
Expand Down
18 changes: 14 additions & 4 deletions include/rfl/cbor/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"

Expand Down Expand Up @@ -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()) {
Expand Down
5 changes: 4 additions & 1 deletion include/rfl/cbor/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"

namespace rfl::cbor {
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 14 additions & 4 deletions include/rfl/flexbuf/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"

Expand Down Expand Up @@ -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()) {
Expand Down
5 changes: 4 additions & 1 deletion include/rfl/flexbuf/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "../Bytestring.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"

namespace rfl {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion include/rfl/generic/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <type_traits>
#include <vector>

#include "../Bytestring.hpp"
#include "../Generic.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
Expand Down
17 changes: 13 additions & 4 deletions include/rfl/msgpack/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions include/rfl/msgpack/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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>()) {
Expand Down
1 change: 1 addition & 0 deletions include/rfl/parsing/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Loading
Loading