draconisplusplus/include/rfl/bson/Reader.hpp

209 lines
6.5 KiB
C++
Raw Normal View History

2024-06-05 19:04:53 -04:00
#ifndef RFL_BSON_READER_HPP_
#define RFL_BSON_READER_HPP_
#include <array>
2024-06-08 14:10:59 -04:00
#include <bson/bson.h>
2024-06-05 19:04:53 -04:00
#include <concepts>
#include <exception>
#include <map>
#include <memory>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include "../Box.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace bson {
/// Please refer to https://mongoc.org/libbson/current/api.html
struct Reader {
struct BSONValue {
bson_value_t val_;
};
struct BSONInputArray {
BSONValue* val_;
};
struct BSONInputObject {
BSONValue* val_;
};
struct BSONInputVar {
BSONValue* val_;
};
using InputArrayType = BSONInputArray;
using InputObjectType = BSONInputObject;
using InputVarType = BSONInputVar;
template <class T>
static constexpr bool has_custom_constructor =
2024-06-16 00:13:15 -04:00
(requires(InputVarType var) { T::from_bson_obj(var); });
2024-06-08 14:10:59 -04:00
2024-06-16 00:13:15 -04:00
rfl::Result<InputVarType> get_field(const std::string& _name, const InputObjectType& _obj)
const noexcept {
2024-06-08 15:53:06 -04:00
bson_t b;
2024-06-08 14:10:59 -04:00
bson_iter_t iter;
2024-06-08 15:53:06 -04:00
const auto doc = _obj.val_->val_.value.v_doc;
2024-06-08 14:10:59 -04:00
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));
2024-06-09 18:55:00 -04:00
if (key == _name) {
return to_input_var(&iter);
}
2024-06-08 14:10:59 -04:00
}
2024-06-05 19:04:53 -04:00
}
}
2024-06-08 14:10:59 -04:00
return Error("No field named '" + _name + "' was found.");
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
bool is_empty(const InputVarType& _var) const noexcept {
return _var.val_->val_.value_type == BSON_TYPE_NULL;
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
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>()) {
switch (btype) {
case BSON_TYPE_UTF8:
return std::string(value.v_utf8.str, value.v_utf8.len);
case BSON_TYPE_SYMBOL:
return std::string(value.v_symbol.symbol, value.v_symbol.len);
default:
2024-06-16 00:13:15 -04:00
return rfl::Error("Could not cast to string. The type must be UTF8 or symbol.");
2024-06-08 14:10:59 -04:00
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (btype != BSON_TYPE_BOOL) {
return rfl::Error("Could not cast to boolean.");
}
return value.v_bool;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
switch (btype) {
case BSON_TYPE_DOUBLE:
return static_cast<T>(value.v_double);
case BSON_TYPE_INT32:
return static_cast<T>(value.v_int32);
case BSON_TYPE_INT64:
return static_cast<T>(value.v_int64);
case BSON_TYPE_DATE_TIME:
return static_cast<T>(value.v_datetime);
default:
return rfl::Error(
2024-06-16 00:13:15 -04:00
"Could not cast to numeric value. The type must be double, "
"int32, int64 or date_time."
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
2024-06-16 00:13:15 -04:00
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
2024-06-08 14:10:59 -04:00
if (btype != BSON_TYPE_OID) {
return rfl::Error("Could not cast to OID.");
}
return value.v_oid;
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
2024-06-16 00:13:15 -04:00
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept {
2024-06-08 14:10:59 -04:00
const auto btype = _var.val_->val_.value_type;
if (btype != BSON_TYPE_ARRAY && btype != BSON_TYPE_DOCUMENT) {
return Error("Could not cast to an array.");
}
2024-06-16 00:13:15 -04:00
return InputArrayType { _var.val_ };
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
template <class ArrayReader>
2024-06-16 00:13:15 -04:00
std::optional<Error> read_array(const ArrayReader& _array_reader, const InputArrayType& _arr)
const noexcept {
2024-06-08 15:53:06 -04:00
bson_t b;
2024-06-08 14:10:59 -04:00
bson_iter_t iter;
2024-06-08 15:53:06 -04:00
const auto doc = _arr.val_->val_.value.v_doc;
2024-06-08 14:10:59 -04:00
if (bson_init_static(&b, doc.data, doc.data_len)) {
if (bson_iter_init(&iter, &b)) {
while (bson_iter_next(&iter)) {
const auto err = _array_reader.read(to_input_var(&iter));
2024-06-09 18:55:00 -04:00
if (err) {
return err;
}
2024-06-08 14:10:59 -04:00
}
}
}
return std::nullopt;
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
template <class ObjectReader>
2024-06-16 00:13:15 -04:00
std::optional<Error>
read_object(const ObjectReader& _object_reader, const InputObjectType& _obj) const noexcept {
2024-06-08 15:53:06 -04:00
bson_t b;
2024-06-08 14:10:59 -04:00
bson_iter_t iter;
2024-06-08 15:53:06 -04:00
const auto doc = _obj.val_->val_.value.v_doc;
2024-06-08 14:10:59 -04:00
if (bson_init_static(&b, doc.data, doc.data_len)) {
if (bson_iter_init(&iter, &b)) {
while (bson_iter_next(&iter)) {
const char* k = bson_iter_key(&iter);
_object_reader.read(std::string_view(k), to_input_var(&iter));
}
2024-06-05 19:04:53 -04:00
}
}
2024-06-08 14:10:59 -04:00
return std::nullopt;
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
2024-06-16 00:13:15 -04:00
rfl::Result<InputObjectType> to_object(const InputVarType& _var) const noexcept {
2024-06-08 14:10:59 -04:00
const auto btype = _var.val_->val_.value_type;
if (btype != BSON_TYPE_DOCUMENT) {
return Error("Could not cast to a document.");
2024-06-05 19:04:53 -04:00
}
2024-06-16 00:13:15 -04:00
return InputObjectType { _var.val_ };
2024-06-08 14:10:59 -04:00
}
template <class T>
2024-06-16 00:13:15 -04:00
rfl::Result<T> use_custom_constructor(const InputVarType& _var) const noexcept {
2024-06-08 14:10:59 -04:00
try {
return T::from_bson_obj(_var);
} catch (std::exception& e) { return rfl::Error(e.what()); }
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
private:
struct BSONValues {
std::vector<rfl::Box<BSONValue>> vec_;
~BSONValues() {
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<BSONValue>::make());
auto* last_value = values_->vec_.back().get();
bson_value_copy(bson_iter_value(_iter), &last_value->val_);
2024-06-16 00:13:15 -04:00
return InputVarType { last_value };
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
private:
/// Contains the values inside the object.
rfl::Ref<BSONValues> values_;
};
} // namespace bson
} // namespace rfl
#endif // JSON_PARSER_HPP_