Skip to content

Commit

Permalink
Finished writing the reader
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jan 15, 2024
1 parent 800a442 commit 9594cbe
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
57 changes: 47 additions & 10 deletions include/rfl/bson/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace rfl {
namespace bson {

/// Please refer to https://mongoc.org/libbson/current/api.html
struct Reader {
struct BSONInputArray {
bson_iter_t iter_;
Expand All @@ -43,10 +44,19 @@ struct Reader {
});

rfl::Result<InputVarType> get_field(
const std::string& _name, const InputObjectType& _obj) const noexcept {}
const std::string& _name, const InputObjectType& _obj) const noexcept {
auto iter = _obj.iter_;
while (bson_iter_next(&iter)) {
auto key = std::string(bson_iter_key(&iter));
if (key == _name) {
return InputVarType{iter};
}
}
return Error("No field named '" + _name + "' was found.");
}

bool is_empty(const InputVarType _var) const noexcept {
return bson_iter_type(_var.iter_) == BSON_TYPE_NULL;
bool is_empty(const InputVarType& _var) const noexcept {
return bson_iter_type(&_var.iter_) == BSON_TYPE_NULL;
}

template <class T>
Expand All @@ -68,7 +78,7 @@ struct Reader {
if (bson_iter_type(&_var.iter_) != BSON_TYPE_DOUBLE) {
return rfl::Error("Could not cast to double.");
}
return static_cast<T>(bson_iter_double(_var.val_));
return static_cast<T>(bson_iter_double(&_var.iter_));
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
const auto btype = bson_iter_type(&_var.iter_);
if (btype != BSON_TYPE_DOUBLE && btype != BSON_TYPE_BOOL &&
Expand All @@ -83,20 +93,40 @@ struct Reader {

rfl::Result<InputArrayType> to_array(
const InputVarType& _var) const noexcept {
if (bson_iter_type(&_var.iter_) != BSON_TYPE_ARRAY) {
auto iter = _obj.iter_;
if (bson_iter_type(iter) != BSON_TYPE_ARRAY) {
return Error("Could not cast to an array.");
}
InputArrayType arr;
bson_iter_recurse(&_var.iter_, &arr.iter_);
bson_iter_recurse(&iter, &arr.iter_);
return arr;
}

template <size_t size, class FunctionType>
std::array<std::optional<InputVarType>, size> to_fields_array(
const FunctionType _fct, const InputObjectType _obj) const noexcept {}
const FunctionType& _fct, const InputObjectType& _obj) const noexcept {
auto iter = _obj.iter_;
std::array<std::optional<InputVarType>, size> f_arr;
while (bson_iter_next(&iter)) {
const char* k = bson_iter_key(&iter);
const auto ix = _fct(std::string_view(k));
if (ix != -1) {
f_arr[ix] = InputVarType{iter};
}
}
return f_arr;
}

std::vector<std::pair<std::string, InputVarType>> to_map(
const InputObjectType _obj) const noexcept {}
const InputObjectType& _obj) const noexcept {
auto iter = _obj.iter_;
std::vector<std::pair<std::string, InputVarType>> map;
while (bson_iter_next(&iter)) {
auto key = std::string(bson_iter_key(&iter));
map.emplace_back(std::make_pair(std::move(key), iter));
}
return map;
}

rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept {
Expand All @@ -108,11 +138,18 @@ struct Reader {
return obj;
}

std::vector<InputVarType> to_vec(const InputArrayType _arr) const noexcept {}
std::vector<InputVarType> to_vec(const InputArrayType& _arr) const noexcept {
auto iter = _obj.iter_;
std::vector<InputVarType> vec;
while (bson_iter_next(&iter)) {
vec.push_back(iter);
}
return vec;
}

template <class T>
rfl::Result<T> use_custom_constructor(
const InputVarType _var) const noexcept {
const InputVarType& _var) const noexcept {
try {
return T::from_bson_obj(_var);
} catch (std::exception& e) {
Expand Down
15 changes: 15 additions & 0 deletions include/rfl/bson/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ auto read(const InputVarType& _obj) {
return Parser<T>::read(r, _obj);
}

/// Parses an object from flexbuf using reflection.
template <class T>
auto read(const char* _bytes, const size_t _size) {
InputVarType doc;
const bool success = bson_init_static(
&doc.iter_, reinterpret_cast<const uint8_t*>(_bytes.data()),
bytes.size());
if (!success) {
return Error("Bytestring was not a valid BSON string.");
}
const auto result = read<T>(doc);
bson_destroy(&doc.iter_);
return result;
}

/// Parses an object from BSON using reflection.
template <class T>
Result<internal::wrap_in_rfl_array_t<T>> read(const std::vector<char>& _bytes) {
Expand Down

0 comments on commit 9594cbe

Please sign in to comment.