geode/loader/include/Geode/utils/JsonValidation.hpp

303 lines
8.9 KiB
C++
Raw Normal View History

#pragma once
#include <matjson.hpp>
2022-12-04 11:39:40 -05:00
#include "../loader/Log.hpp"
2022-10-30 14:59:20 -04:00
#include <set>
2022-10-30 14:59:20 -04:00
#include <variant>
namespace geode {
struct JsonChecker;
template <typename T, typename = void>
struct is_iterable : std::false_type {};
template <typename T>
2022-10-30 14:59:20 -04:00
struct is_iterable<
2022-12-04 11:39:40 -05:00
T, std::void_t<decltype(std::begin(std::declval<T>())), decltype(std::end(std::declval<T>()))>> :
2022-10-30 14:59:20 -04:00
std::true_type {};
template <typename T>
constexpr bool is_iterable_v = is_iterable<T>::value;
namespace {
using value_t = matjson::Type;
2022-10-30 14:59:20 -04:00
constexpr char const* jsonValueTypeToString(value_t type) {
switch (type) {
default:
case value_t::Null: return "null";
case value_t::Object: return "object";
case value_t::Array: return "array";
case value_t::String: return "string";
case value_t::Bool: return "boolean";
case value_t::Number: return "number";
}
}
2022-10-30 14:59:20 -04:00
template <class T>
constexpr value_t getJsonType() {
if constexpr (std::is_same_v<T, bool>) {
return value_t::Bool;
}
else if constexpr (std::is_floating_point_v<T>) {
return value_t::Number;
}
else if constexpr (std::is_unsigned_v<T>) {
return value_t::Number;
}
else if constexpr (std::is_integral_v<T>) {
return value_t::Number;
}
2022-10-30 14:59:20 -04:00
else if constexpr (std::is_constructible_v<T, std::string>) {
return value_t::String;
}
else if constexpr (is_iterable_v<T>) {
return value_t::Array;
}
return value_t::Null;
}
bool jsonConvertibleTo(value_t value, value_t to) {
2022-10-30 14:59:20 -04:00
// if we don't know the type we're passing into,
2022-09-21 07:50:23 -04:00
// everything's valid
if (to == value_t::Null) return true;
if (value == value_t::Number) {
return to == value_t::Number;
}
return value == to;
}
}
2022-10-30 14:59:20 -04:00
template <class T>
2023-02-08 10:25:07 -05:00
using JsonValueValidator = utils::MiniFunction<bool(T const&)>;
struct JsonMaybeObject;
2022-09-24 11:46:47 -04:00
struct JsonMaybeValue;
struct GEODE_DLL JsonMaybeSomething {
protected:
2023-02-08 10:25:07 -05:00
JsonChecker& m_checker;
matjson::Value& m_json;
std::string m_hierarchy;
bool m_hasValue;
2023-02-08 10:25:07 -05:00
friend struct JsonMaybeObject;
friend struct JsonMaybeValue;
2022-09-24 11:46:47 -04:00
void setError(std::string const& error);
2022-10-30 14:59:20 -04:00
public:
matjson::Value& json();
JsonMaybeSomething(
JsonChecker& checker, matjson::Value& json, std::string const& hierarchy, bool hasValue
2022-10-23 09:22:27 -04:00
);
bool isError() const;
std::string getError() const;
operator bool() const;
};
struct GEODE_DLL JsonMaybeValue : public JsonMaybeSomething {
bool m_inferType = true;
JsonMaybeValue(
JsonChecker& checker, matjson::Value& json, std::string const& hierarchy, bool hasValue
2022-10-23 09:22:27 -04:00
);
2022-09-24 11:46:47 -04:00
JsonMaybeSomething& self();
template <matjson::Type T>
2023-02-08 10:25:07 -05:00
JsonMaybeValue& as() {
if (this->isError()) return *this;
2022-09-24 11:46:47 -04:00
if (!jsonConvertibleTo(self().m_json.type(), T)) {
this->setError(
self().m_hierarchy + ": Invalid type \"" + jsonValueTypeToString(self().m_json.type()) +
2022-10-30 14:59:20 -04:00
"\", expected \"" + jsonValueTypeToString(T) + "\""
);
}
m_inferType = false;
return *this;
}
JsonMaybeValue& array();
template <matjson::Type... T>
2023-02-08 10:25:07 -05:00
JsonMaybeValue& asOneOf() {
if (this->isError()) return *this;
2022-09-24 11:46:47 -04:00
bool isOneOf = (... || jsonConvertibleTo(self().m_json.type(), T));
if (!isOneOf) {
this->setError(
self().m_hierarchy + ": Invalid type \"" + jsonValueTypeToString(self().m_json.type()) +
2022-10-30 14:59:20 -04:00
"\", expected one of \"" + (jsonValueTypeToString(T), ...) + "\""
);
}
m_inferType = false;
return *this;
}
template <matjson::Type T>
2023-02-08 10:25:07 -05:00
JsonMaybeValue& is() {
if (this->isError()) return *this;
2022-09-24 11:46:47 -04:00
self().m_hasValue = jsonConvertibleTo(self().m_json.type(), T);
m_inferType = false;
return *this;
}
2022-10-30 14:59:20 -04:00
template <class T>
2023-02-08 10:25:07 -05:00
JsonMaybeValue& validate(JsonValueValidator<T> validator) {
if (this->isError()) return *this;
try {
if (!validator(self().m_json.template as<T>())) {
2022-09-24 11:46:47 -04:00
this->setError(self().m_hierarchy + ": Invalid value format");
}
2022-10-30 14:59:20 -04:00
}
catch (...) {
this->setError(
2022-09-24 11:46:47 -04:00
self().m_hierarchy + ": Invalid type \"" +
std::string(jsonValueTypeToString(self().m_json.type())) + "\""
);
}
return *this;
}
2022-12-04 11:39:40 -05:00
template <class T>
2023-02-08 10:25:07 -05:00
JsonMaybeValue& inferType() {
if (this->isError() || !m_inferType) return *this;
return this->as<getJsonType<T>()>();
}
2022-10-30 14:59:20 -04:00
template <class T>
2023-02-08 10:25:07 -05:00
JsonMaybeValue& intoRaw(T& target) {
if (this->isError()) return *this;
2022-09-24 11:46:47 -04:00
target = self().m_json;
return *this;
}
2022-10-30 14:59:20 -04:00
template <class T>
2023-02-08 10:25:07 -05:00
JsonMaybeValue& into(T& target) {
return this->intoAs<T, T>(target);
}
2022-10-30 14:59:20 -04:00
template <class T>
2023-02-08 10:25:07 -05:00
JsonMaybeValue& into(std::optional<T>& target) {
return this->intoAs<T, std::optional<T>>(target);
}
2022-10-30 14:59:20 -04:00
template <class A, class T>
2023-02-08 10:25:07 -05:00
JsonMaybeValue& intoAs(T& target) {
this->inferType<A>();
if (this->isError()) return *this;
try {
target = self().m_json.template as<A>();
2022-10-30 14:59:20 -04:00
}
catch (...) {
this->setError(
2022-10-30 14:59:20 -04:00
self().m_hierarchy + ": Invalid type \"" +
jsonValueTypeToString(self().m_json.type()) + "\""
);
}
return *this;
}
2022-10-30 14:59:20 -04:00
template <class T>
T get() {
this->inferType<T>();
if (this->isError()) return T();
try {
constexpr auto type = getJsonType<T>();
if constexpr (type == value_t::Number) {
return self().m_json.as_double();
} else if constexpr (type == value_t::Bool) {
return self().m_json.as_bool();
} else if constexpr (type == value_t::String) {
return self().m_json.as_string();
}
2022-10-30 14:59:20 -04:00
}
catch (...) {
this->setError(
2022-10-30 14:59:20 -04:00
self().m_hierarchy + ": Invalid type to get \"" +
std::string(jsonValueTypeToString(self().m_json.type())) + "\""
);
}
return T();
}
JsonMaybeObject obj();
2022-10-30 14:59:20 -04:00
template <class T>
struct Iterator {
std::vector<T> m_values;
using iterator = typename std::vector<T>::iterator;
using const_iterator = typename std::vector<T>::const_iterator;
iterator begin() {
return m_values.begin();
}
2022-10-30 14:59:20 -04:00
iterator end() {
return m_values.end();
}
const_iterator begin() const {
return m_values.begin();
}
2022-10-30 14:59:20 -04:00
const_iterator end() const {
return m_values.end();
}
};
JsonMaybeValue at(size_t i);
Iterator<JsonMaybeValue> iterate();
Iterator<std::pair<std::string, JsonMaybeValue>> items();
};
struct GEODE_DLL JsonMaybeObject : JsonMaybeSomething {
std::set<std::string> m_knownKeys;
JsonMaybeObject(
JsonChecker& checker, matjson::Value& json, std::string const& hierarchy, bool hasValue
2022-10-23 09:22:27 -04:00
);
2022-09-24 11:46:47 -04:00
JsonMaybeSomething& self();
void addKnownKey(std::string const& key);
matjson::Value& json();
JsonMaybeValue emptyValue();
JsonMaybeValue has(std::string const& key);
JsonMaybeValue needs(std::string const& key);
void checkUnknownKeys();
};
2022-10-30 14:59:20 -04:00
struct GEODE_DLL JsonChecker {
std::variant<std::monostate, std::string> m_result;
matjson::Value& m_json;
JsonChecker(matjson::Value& json);
bool isError() const;
std::string getError() const;
JsonMaybeValue root(std::string const& hierarchy);
};
2022-10-30 14:59:20 -04:00
}