Skip to content

Commit

Permalink
Began implementing the CBOR reader
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jan 21, 2024
1 parent e56acb7 commit d71fc1d
Showing 1 changed file with 30 additions and 38 deletions.
68 changes: 30 additions & 38 deletions include/rfl/cbor/Reader.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef RFL_CBOR_READER_HPP_
#define RFL_CBOR_READER_HPP_

#include <bson/bson.h>
#include <cbor.h>

#include <array>
#include <concepts>
Expand All @@ -25,20 +25,16 @@ namespace cbor {

/// Please refer to https://mongoc.org/libbson/current/api.html
struct Reader {
struct CBORValue {
bson_value_t val_;
};

struct CBORInputArray {
CBORValue* val_;
CborValue* val_;
};

struct CBORInputObject {
CBORValue* val_;
CborValue* val_;
};

struct CBORInputVar {
CBORValue* val_;
CborValue* val_;
};

using InputArrayType = CBORInputArray;
Expand All @@ -47,22 +43,27 @@ struct Reader {

template <class T>
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
T::from_bson_obj(var);
T::from_cbor_obj(var);
});

rfl::Result<InputVarType> get_field(
const std::string& _name, const InputObjectType& _obj) const noexcept {
bson_t b;
bson_iter_t iter;
const auto doc = _obj.val_->val_.value.v_doc;
if (bson_init_static(&b, doc.data, doc.data_len)) {
if (bson_iter_init(&iter, &b)) {
while (bson_iter_next(&iter)) {
auto key = std::string(bson_iter_key(&iter));
if (key == _name) {
return to_input_var(&iter);
}
CborValue val;
size_t length = 0;
auto buffer = std::vector<char>();
if (cbor_value_enter_container(_obj.val_, &val) != CborError) {
while (!cbor_value_at_end(&val)) {
if (!cbor_value_is_text_string(&val)) {
return Error("Expected the key to be a string value.");
}
cbor_value_get_string_length(&val, &length);
buffer.resize(length + 1);
buffer[length] = '\0';
cbor_copy_text_string(&val, buffer.data(), &length, &val);
if (key == buffer.data()) {
return to_input_var(&val);
}
cbor_value_advance(&val);
}
}
return Error("No field named '" + _name + "' was found.");
Expand All @@ -74,9 +75,9 @@ struct Reader {

template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
const auto btype = _var.val_->val_.value_type;
const auto value = _var.val_->val_.value;
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
const auto btype = _var.val_->val_.value_type;

switch (btype) {
case CBOR_TYPE_UTF8:
return std::string(value.v_utf8.str, value.v_utf8.len);
Expand All @@ -89,10 +90,12 @@ struct Reader {
"Could not cast to string. The type must be UTF8 or symbol.");
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (btype != CBOR_TYPE_BOOL) {
if (!cbor_value_is_boolean(_var->val_)) {
return rfl::Error("Could not cast to boolean.");
}
return value.v_bool;
bool result = false;
cbor_value_get_boolean(_var->val_, &result);
return result;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
switch (btype) {
Expand Down Expand Up @@ -193,33 +196,22 @@ struct Reader {
rfl::Result<T> use_custom_constructor(
const InputVarType& _var) const noexcept {
try {
return T::from_bson_obj(_var);
return T::from_cbor_obj(_var);
} catch (std::exception& e) {
return rfl::Error(e.what());
}
}

private:
struct CBORValues {
std::vector<rfl::Box<CBORValue>> vec_;
~CBORValues() {
for (auto& v : vec_) {
bson_value_destroy(&(v->val_));
}
}
};

private:
InputVarType to_input_var(bson_iter_t* _iter) const noexcept {
values_->vec_.emplace_back(rfl::Box<CBORValue>::make());
InputVarType to_input_var(CborValur* _ptr) const noexcept {
values_->emplace_back(rfl::Box<CborValue>::make(*_ptr));
auto* last_value = values_->vec_.back().get();
bson_value_copy(bson_iter_value(_iter), &last_value->val_);
return InputVarType{last_value};
}

private:
/// Contains the values inside the object.
rfl::Ref<CBORValues> values_;
rfl::Box<std::vector<rfl::Box<CborValue>>> values_;
};

} // namespace cbor
Expand Down

0 comments on commit d71fc1d

Please sign in to comment.