finalize entity functions
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
f0e6dec7de
commit
c489e9e8ae
18 changed files with 523 additions and 181 deletions
|
|
@ -152,10 +152,9 @@ auto IkarusValueData::from_json(nlohmann::json const & json)
|
|||
return cppbase::ok(value);
|
||||
}
|
||||
|
||||
auto IkarusValueData::to_json(
|
||||
nlohmann::json & json,
|
||||
IkarusValueData const & value
|
||||
) -> void {
|
||||
auto IkarusValueData::to_json(IkarusValueData const & value) -> nlohmann::json {
|
||||
nlohmann::json json = nlohmann::json::object();
|
||||
|
||||
std::visit(
|
||||
cppbase::overloaded{
|
||||
[&](IkarusValueDataPrimitive const & primitive) {
|
||||
|
|
@ -184,9 +183,7 @@ auto IkarusValueData::to_json(
|
|||
json["type"] = IkarusValueDataType_List;
|
||||
json["data"] = list.values |
|
||||
std::views::transform([](auto const & data) {
|
||||
nlohmann::json j;
|
||||
IkarusValueData::to_json(j, *data);
|
||||
return j;
|
||||
return IkarusValueData::to_json(*data);
|
||||
}) |
|
||||
std::ranges::to<std::vector<nlohmann::json>>();
|
||||
},
|
||||
|
|
@ -195,8 +192,8 @@ auto IkarusValueData::to_json(
|
|||
json["data"] =
|
||||
map.values | std::views::transform([](auto const & pair) {
|
||||
nlohmann::json j;
|
||||
IkarusValueData::to_json(j["key"], *pair.first);
|
||||
IkarusValueData::to_json(j["value"], *pair.second);
|
||||
j["key"] = IkarusValueData::to_json(*pair.first);
|
||||
j["value"] = IkarusValueData::to_json(*pair.second);
|
||||
return j;
|
||||
}) |
|
||||
std::ranges::to<std::vector<nlohmann::json>>();
|
||||
|
|
@ -205,13 +202,13 @@ auto IkarusValueData::to_json(
|
|||
json["type"] = IkarusValueDataType_Tuple;
|
||||
json["data"] = tuple.values |
|
||||
std::views::transform([](auto const & data) {
|
||||
nlohmann::json j;
|
||||
IkarusValueData::to_json(j, *data);
|
||||
return j;
|
||||
return IkarusValueData::to_json(*data);
|
||||
}) |
|
||||
std::ranges::to<std::vector<nlohmann::json>>();
|
||||
}
|
||||
},
|
||||
value.variant
|
||||
);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,8 +55,7 @@ struct IkarusValueData {
|
|||
|
||||
static auto from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValueData, IkarusValueDataParseError>;
|
||||
static auto to_json(nlohmann::json & json, IkarusValueData const & value)
|
||||
-> void;
|
||||
static auto to_json(IkarusValueData const & value) -> nlohmann::json;
|
||||
|
||||
IkarusValueDataVariant variant;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
#include <variant>
|
||||
|
||||
#include <cppbase/format.hpp>
|
||||
|
||||
struct IkarusJsonMissingKeyError {};
|
||||
|
||||
struct IkarusJsonInvalidTypeError {};
|
||||
|
|
@ -34,3 +36,82 @@ using IkarusValueParseError = std::variant<
|
|||
|
||||
using IkarusValuesParseError =
|
||||
std::variant<IkarusJsonMissingKeyError, IkarusValueParseError>;
|
||||
|
||||
template<>
|
||||
struct fmt::formatter<IkarusJsonMissingKeyError> : formatter<string_view> {
|
||||
constexpr static auto format(
|
||||
[[maybe_unused]] IkarusJsonMissingKeyError const & error,
|
||||
fmt::format_context & ctx
|
||||
) {
|
||||
return fmt::format_to(ctx.out(), "missing JSON key");
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct fmt::formatter<IkarusJsonInvalidTypeError> : formatter<string_view> {
|
||||
constexpr static auto format(
|
||||
[[maybe_unused]] IkarusJsonInvalidTypeError const & error,
|
||||
fmt::format_context & ctx
|
||||
) {
|
||||
return fmt::format_to(ctx.out(), "invalid JSON type");
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct fmt::formatter<IkarusJsonEnumOutOfBoundsError> : formatter<string_view> {
|
||||
constexpr static auto format(
|
||||
[[maybe_unused]] IkarusJsonEnumOutOfBoundsError const & error,
|
||||
fmt::format_context & ctx
|
||||
) {
|
||||
return fmt::format_to(ctx.out(), "JSON enum is out of bounds");
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct fmt::formatter<IkarusJsonUnknownError> : formatter<string_view> {
|
||||
constexpr static auto format(
|
||||
[[maybe_unused]] IkarusJsonUnknownError const & error,
|
||||
fmt::format_context & ctx
|
||||
) {
|
||||
return fmt::format_to(ctx.out(), "unknown JSON error");
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct fmt::formatter<IkarusValueSchemaParseError> : formatter<string_view> {
|
||||
constexpr static auto format(
|
||||
[[maybe_unused]] IkarusValueSchemaParseError const & error,
|
||||
fmt::format_context & ctx
|
||||
) {
|
||||
return fmt::format_to(
|
||||
ctx.out(),
|
||||
"failed to parse value schema: {}",
|
||||
error.error
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct fmt::formatter<IkarusValueDataParseError> : formatter<string_view> {
|
||||
constexpr static auto format(
|
||||
[[maybe_unused]] IkarusValueDataParseError const & error,
|
||||
fmt::format_context & ctx
|
||||
) {
|
||||
return fmt::format_to(
|
||||
ctx.out(),
|
||||
"failed to parse value data: {}",
|
||||
error.error
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct fmt::formatter<IkarusValueParseErrorDataSchemaMismatch> :
|
||||
formatter<string_view> {
|
||||
constexpr static auto format(
|
||||
[[maybe_unused]] IkarusValueParseErrorDataSchemaMismatch const & error,
|
||||
fmt::format_context & ctx
|
||||
) {
|
||||
return fmt::format_to(ctx.out(), "value data and schema mismatched");
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -85,10 +85,10 @@ auto IkarusValueSchema::from_json(nlohmann::json const & json)
|
|||
return cppbase::ok(std::move(schema));
|
||||
}
|
||||
|
||||
auto IkarusValueSchema::to_json(
|
||||
nlohmann::json & json,
|
||||
IkarusValueSchema const & schema
|
||||
) -> void {
|
||||
auto IkarusValueSchema::to_json(IkarusValueSchema const & schema)
|
||||
-> nlohmann::json {
|
||||
nlohmann::json json = nlohmann::json::object();
|
||||
|
||||
std::visit(
|
||||
cppbase::overloaded{
|
||||
[&json](IkarusValueSchemaPrimitive const & schema) {
|
||||
|
|
@ -97,18 +97,14 @@ auto IkarusValueSchema::to_json(
|
|||
},
|
||||
[&json](IkarusValueSchemaList const & schema) {
|
||||
json["type"] = IkarusValueSchemaType_List;
|
||||
IkarusValueSchema::to_json(json["schema"], *schema.sub_schema);
|
||||
json["schema"] = IkarusValueSchema::to_json(*schema.sub_schema);
|
||||
},
|
||||
[&json](IkarusValueSchemaMap const & schema) {
|
||||
json["type"] = IkarusValueSchemaType_Map;
|
||||
IkarusValueSchema::to_json(
|
||||
json["key_schema"],
|
||||
*schema.key_schema
|
||||
);
|
||||
IkarusValueSchema::to_json(
|
||||
json["value_schema"],
|
||||
*schema.value_schema
|
||||
);
|
||||
json["key_schema"] =
|
||||
IkarusValueSchema::to_json(*schema.key_schema);
|
||||
json["value_schema"] =
|
||||
IkarusValueSchema::to_json(*schema.value_schema);
|
||||
},
|
||||
[&json](IkarusValueSchemaTuple const & schema) {
|
||||
json["type"] = IkarusValueSchemaType_Tuple;
|
||||
|
|
@ -117,9 +113,9 @@ auto IkarusValueSchema::to_json(
|
|||
sub_schemas.reserve(schema.sub_schemas.size());
|
||||
|
||||
for (auto const & sub_schema : schema.sub_schemas) {
|
||||
nlohmann::json sub_schema_json{};
|
||||
IkarusValueSchema::to_json(sub_schema_json, *sub_schema);
|
||||
sub_schemas.push_back(sub_schema_json);
|
||||
sub_schemas.push_back(
|
||||
IkarusValueSchema::to_json(*sub_schema)
|
||||
);
|
||||
}
|
||||
|
||||
json["schemas"] = sub_schemas;
|
||||
|
|
@ -127,6 +123,8 @@ auto IkarusValueSchema::to_json(
|
|||
},
|
||||
schema.variant
|
||||
);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
auto IkarusValueSchema::validate(IkarusValueData const & data) const -> bool {
|
||||
|
|
|
|||
|
|
@ -42,8 +42,7 @@ struct IkarusValueSchema {
|
|||
|
||||
static auto from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValueSchema, IkarusValueSchemaParseError>;
|
||||
static auto to_json(nlohmann::json & json, IkarusValueSchema const & value)
|
||||
-> void;
|
||||
static auto to_json(IkarusValueSchema const & value) -> nlohmann::json;
|
||||
|
||||
auto validate(IkarusValueData const & data) const -> bool;
|
||||
|
||||
|
|
|
|||
|
|
@ -25,34 +25,11 @@ auto IkarusValue::from_json(nlohmann::json const & json)
|
|||
return cppbase::ok(std::move(value));
|
||||
}
|
||||
|
||||
auto IkarusValue::to_json(nlohmann::json & json, IkarusValue const & value)
|
||||
-> void {
|
||||
IkarusValueSchema::to_json(json["schema"], value.schema);
|
||||
IkarusValueData::to_json(json["data"], value.data);
|
||||
}
|
||||
|
||||
auto IkarusValues::from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValues, IkarusValuesParseError> {
|
||||
IkarusValues values{};
|
||||
|
||||
for (auto const & json_entry : json) {
|
||||
VTRY(auto name_json, get_key(json_entry, "name"));
|
||||
|
||||
if (!name_json->is_string()) {
|
||||
return cppbase::err(IkarusJsonInvalidTypeError{});
|
||||
}
|
||||
|
||||
VTRY(auto value_json, get_key(json_entry, "value"));
|
||||
|
||||
VTRY(auto value, IkarusValue::from_json(*value_json));
|
||||
|
||||
values.values.emplace_back(
|
||||
std::make_pair(
|
||||
std::move(name_json->get<std::string_view>()),
|
||||
std::move(value)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return cppbase::ok(std::move(values));
|
||||
auto IkarusValue::to_json(IkarusValue const & value) -> nlohmann::json {
|
||||
nlohmann::json json = nlohmann::json::object();
|
||||
|
||||
json["schema"] = IkarusValueSchema::to_json(value.schema);
|
||||
json["data"] = IkarusValueData::to_json(value.data);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,15 +14,5 @@ struct IkarusValue {
|
|||
|
||||
static auto from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValue, IkarusValueParseError>;
|
||||
static auto to_json(nlohmann::json & json, IkarusValue const & value)
|
||||
-> void;
|
||||
};
|
||||
|
||||
struct IkarusValues {
|
||||
static auto from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValues, IkarusValuesParseError>;
|
||||
static auto to_json(nlohmann::json & json, IkarusValues const & values)
|
||||
-> void;
|
||||
|
||||
std::vector<std::pair<std::string, IkarusValue>> values;
|
||||
static auto to_json(IkarusValue const & value) -> nlohmann::json;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue