1
1
#ifndef RFL_CBOR_READER_HPP_
2
2
#define RFL_CBOR_READER_HPP_
3
3
4
- #include < bson/bson .h>
4
+ #include < cbor .h>
5
5
6
6
#include < array>
7
7
#include < concepts>
@@ -25,20 +25,16 @@ namespace cbor {
25
25
26
26
// / Please refer to https://mongoc.org/libbson/current/api.html
27
27
struct Reader {
28
- struct CBORValue {
29
- bson_value_t val_;
30
- };
31
-
32
28
struct CBORInputArray {
33
- CBORValue * val_;
29
+ CborValue * val_;
34
30
};
35
31
36
32
struct CBORInputObject {
37
- CBORValue * val_;
33
+ CborValue * val_;
38
34
};
39
35
40
36
struct CBORInputVar {
41
- CBORValue * val_;
37
+ CborValue * val_;
42
38
};
43
39
44
40
using InputArrayType = CBORInputArray;
@@ -47,22 +43,27 @@ struct Reader {
47
43
48
44
template <class T >
49
45
static constexpr bool has_custom_constructor = (requires (InputVarType var) {
50
- T::from_bson_obj (var);
46
+ T::from_cbor_obj (var);
51
47
});
52
48
53
49
rfl::Result<InputVarType> get_field (
54
50
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);
65
65
}
66
+ cbor_value_advance (&val);
66
67
}
67
68
}
68
69
return Error (" No field named '" + _name + " ' was found." );
@@ -74,9 +75,9 @@ struct Reader {
74
75
75
76
template <class T >
76
77
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 ;
79
78
if constexpr (std::is_same<std::remove_cvref_t <T>, std::string>()) {
79
+ const auto btype = _var.val_ ->val_ .value_type ;
80
+
80
81
switch (btype) {
81
82
case CBOR_TYPE_UTF8:
82
83
return std::string (value.v_utf8 .str , value.v_utf8 .len );
@@ -89,10 +90,12 @@ struct Reader {
89
90
" Could not cast to string. The type must be UTF8 or symbol." );
90
91
}
91
92
} 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_ ) ) {
93
94
return rfl::Error (" Could not cast to boolean." );
94
95
}
95
- return value.v_bool ;
96
+ bool result = false ;
97
+ cbor_value_get_boolean (_var->val_ , &result);
98
+ return result;
96
99
} else if constexpr (std::is_floating_point<std::remove_cvref_t <T>>() ||
97
100
std::is_integral<std::remove_cvref_t <T>>()) {
98
101
switch (btype) {
@@ -193,33 +196,22 @@ struct Reader {
193
196
rfl::Result<T> use_custom_constructor (
194
197
const InputVarType& _var) const noexcept {
195
198
try {
196
- return T::from_bson_obj (_var);
199
+ return T::from_cbor_obj (_var);
197
200
} catch (std::exception& e) {
198
201
return rfl::Error (e.what ());
199
202
}
200
203
}
201
204
202
205
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));
215
208
auto * last_value = values_->vec_ .back ().get ();
216
- bson_value_copy (bson_iter_value (_iter), &last_value->val_ );
217
209
return InputVarType{last_value};
218
210
}
219
211
220
212
private:
221
213
// / Contains the values inside the object.
222
- rfl::Ref<CBORValues > values_;
214
+ rfl::Box<std::vector<rfl::Box<CborValue>> > values_;
223
215
};
224
216
225
217
} // namespace cbor
0 commit comments