Skip to content

Commit d71fc1d

Browse files
Began implementing the CBOR reader
1 parent e56acb7 commit d71fc1d

File tree

1 file changed

+30
-38
lines changed

1 file changed

+30
-38
lines changed

include/rfl/cbor/Reader.hpp

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef RFL_CBOR_READER_HPP_
22
#define RFL_CBOR_READER_HPP_
33

4-
#include <bson/bson.h>
4+
#include <cbor.h>
55

66
#include <array>
77
#include <concepts>
@@ -25,20 +25,16 @@ namespace cbor {
2525

2626
/// Please refer to https://mongoc.org/libbson/current/api.html
2727
struct Reader {
28-
struct CBORValue {
29-
bson_value_t val_;
30-
};
31-
3228
struct CBORInputArray {
33-
CBORValue* val_;
29+
CborValue* val_;
3430
};
3531

3632
struct CBORInputObject {
37-
CBORValue* val_;
33+
CborValue* val_;
3834
};
3935

4036
struct CBORInputVar {
41-
CBORValue* val_;
37+
CborValue* val_;
4238
};
4339

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

4844
template <class T>
4945
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
50-
T::from_bson_obj(var);
46+
T::from_cbor_obj(var);
5147
});
5248

5349
rfl::Result<InputVarType> get_field(
5450
const std::string& _name, const InputObjectType& _obj) const noexcept {
55-
bson_t b;
56-
bson_iter_t iter;
57-
const auto doc = _obj.val_->val_.value.v_doc;
58-
if (bson_init_static(&b, doc.data, doc.data_len)) {
59-
if (bson_iter_init(&iter, &b)) {
60-
while (bson_iter_next(&iter)) {
61-
auto key = std::string(bson_iter_key(&iter));
62-
if (key == _name) {
63-
return to_input_var(&iter);
64-
}
51+
CborValue val;
52+
size_t length = 0;
53+
auto buffer = std::vector<char>();
54+
if (cbor_value_enter_container(_obj.val_, &val) != CborError) {
55+
while (!cbor_value_at_end(&val)) {
56+
if (!cbor_value_is_text_string(&val)) {
57+
return Error("Expected the key to be a string value.");
58+
}
59+
cbor_value_get_string_length(&val, &length);
60+
buffer.resize(length + 1);
61+
buffer[length] = '\0';
62+
cbor_copy_text_string(&val, buffer.data(), &length, &val);
63+
if (key == buffer.data()) {
64+
return to_input_var(&val);
6565
}
66+
cbor_value_advance(&val);
6667
}
6768
}
6869
return Error("No field named '" + _name + "' was found.");
@@ -74,9 +75,9 @@ struct Reader {
7475

7576
template <class T>
7677
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
77-
const auto btype = _var.val_->val_.value_type;
78-
const auto value = _var.val_->val_.value;
7978
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
79+
const auto btype = _var.val_->val_.value_type;
80+
8081
switch (btype) {
8182
case CBOR_TYPE_UTF8:
8283
return std::string(value.v_utf8.str, value.v_utf8.len);
@@ -89,10 +90,12 @@ struct Reader {
8990
"Could not cast to string. The type must be UTF8 or symbol.");
9091
}
9192
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
92-
if (btype != CBOR_TYPE_BOOL) {
93+
if (!cbor_value_is_boolean(_var->val_)) {
9394
return rfl::Error("Could not cast to boolean.");
9495
}
95-
return value.v_bool;
96+
bool result = false;
97+
cbor_value_get_boolean(_var->val_, &result);
98+
return result;
9699
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
97100
std::is_integral<std::remove_cvref_t<T>>()) {
98101
switch (btype) {
@@ -193,33 +196,22 @@ struct Reader {
193196
rfl::Result<T> use_custom_constructor(
194197
const InputVarType& _var) const noexcept {
195198
try {
196-
return T::from_bson_obj(_var);
199+
return T::from_cbor_obj(_var);
197200
} catch (std::exception& e) {
198201
return rfl::Error(e.what());
199202
}
200203
}
201204

202205
private:
203-
struct CBORValues {
204-
std::vector<rfl::Box<CBORValue>> vec_;
205-
~CBORValues() {
206-
for (auto& v : vec_) {
207-
bson_value_destroy(&(v->val_));
208-
}
209-
}
210-
};
211-
212-
private:
213-
InputVarType to_input_var(bson_iter_t* _iter) const noexcept {
214-
values_->vec_.emplace_back(rfl::Box<CBORValue>::make());
206+
InputVarType to_input_var(CborValur* _ptr) const noexcept {
207+
values_->emplace_back(rfl::Box<CborValue>::make(*_ptr));
215208
auto* last_value = values_->vec_.back().get();
216-
bson_value_copy(bson_iter_value(_iter), &last_value->val_);
217209
return InputVarType{last_value};
218210
}
219211

220212
private:
221213
/// Contains the values inside the object.
222-
rfl::Ref<CBORValues> values_;
214+
rfl::Box<std::vector<rfl::Box<CborValue>>> values_;
223215
};
224216

225217
} // namespace cbor

0 commit comments

Comments
 (0)