Simplify td_api_json more.

GitOrigin-RevId: 4df61a045e381a4577321c87a2b393747992127e
This commit is contained in:
levlam 2019-12-08 10:44:41 +03:00
parent 8c495d99e6
commit 95bee16523
4 changed files with 50 additions and 32 deletions

View File

@ -26,7 +26,7 @@ void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_he
sb << "void to_json(JsonValueScope &jv, " sb << "void to_json(JsonValueScope &jv, "
<< "const td_api::" << tl::simple::gen_cpp_name(constructor->name) << " &object)"; << "const td_api::" << tl::simple::gen_cpp_name(constructor->name) << " &object)";
if (is_header) { if (is_header) {
sb << ";\n"; sb << ";\n\n";
return; return;
} }
sb << " {\n"; sb << " {\n";
@ -54,7 +54,7 @@ void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_he
sb << " }\n"; sb << " }\n";
} }
} }
sb << "}\n"; sb << "}\n\n";
} }
void gen_to_json(StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode) { void gen_to_json(StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode) {
@ -66,13 +66,13 @@ void gen_to_json(StringBuilder &sb, const tl::simple::Schema &schema, bool is_he
auto type_name = tl::simple::gen_cpp_name(custom_type->name); auto type_name = tl::simple::gen_cpp_name(custom_type->name);
sb << "void to_json(JsonValueScope &jv, const td_api::" << type_name << " &object)"; sb << "void to_json(JsonValueScope &jv, const td_api::" << type_name << " &object)";
if (is_header) { if (is_header) {
sb << ";\n"; sb << ";\n\n";
} else { } else {
sb << " {\n" sb << " {\n"
<< " td_api::downcast_call(const_cast<td_api::" << type_name << " td_api::downcast_call(const_cast<td_api::" << type_name
<< " &>(object), [&jv](const auto &object) { " << " &>(object), [&jv](const auto &object) { "
"to_json(jv, object); });\n" "to_json(jv, object); });\n"
<< "}\n"; << "}\n\n";
} }
} }
for (auto *constructor : custom_type->constructors) { for (auto *constructor : custom_type->constructors) {
@ -91,23 +91,16 @@ template <class T>
void gen_from_json_constructor(StringBuilder &sb, const T *constructor, bool is_header) { void gen_from_json_constructor(StringBuilder &sb, const T *constructor, bool is_header) {
sb << "Status from_json(td_api::" << tl::simple::gen_cpp_name(constructor->name) << " &to, JsonObject &from)"; sb << "Status from_json(td_api::" << tl::simple::gen_cpp_name(constructor->name) << " &to, JsonObject &from)";
if (is_header) { if (is_header) {
sb << ";\n"; sb << ";\n\n";
} else { } else {
sb << " {\n"; sb << " {\n";
for (auto &arg : constructor->args) { for (auto &arg : constructor->args) {
sb << " {\n"; sb << " TRY_STATUS(from_json" << (arg.type->type == tl::simple::Type::Bytes ? "_bytes" : "") << "(to."
sb << " auto value = get_json_object_field_force(from, \"" << tl::simple::gen_cpp_name(arg.name) << "\");\n"; << tl::simple::gen_cpp_field_name(arg.name) << ", get_json_object_field_force(from, \""
sb << " if (value.type() != td::JsonValue::Type::Null) {\n"; << tl::simple::gen_cpp_name(arg.name) << "\")));\n";
if (arg.type->type == tl::simple::Type::Bytes) {
sb << " TRY_STATUS(from_json_bytes(to." << tl::simple::gen_cpp_field_name(arg.name) << ", value));\n";
} else {
sb << " TRY_STATUS(from_json(to." << tl::simple::gen_cpp_field_name(arg.name) << ", value));\n";
}
sb << " }\n";
sb << " }\n";
} }
sb << " return Status::OK();\n"; sb << " return Status::OK();\n";
sb << "}\n"; sb << "}\n\n";
} }
} }
@ -132,7 +125,7 @@ using Vec = std::vector<std::pair<int32, std::string>>;
void gen_tl_constructor_from_string(StringBuilder &sb, Slice name, const Vec &vec, bool is_header) { void gen_tl_constructor_from_string(StringBuilder &sb, Slice name, const Vec &vec, bool is_header) {
sb << "Result<int32> tl_constructor_from_string(td_api::" << name << " *object, const std::string &str)"; sb << "Result<int32> tl_constructor_from_string(td_api::" << name << " *object, const std::string &str)";
if (is_header) { if (is_header) {
sb << ";\n"; sb << ";\n\n";
return; return;
} }
sb << " {\n"; sb << " {\n";
@ -153,7 +146,7 @@ void gen_tl_constructor_from_string(StringBuilder &sb, Slice name, const Vec &ve
<< " return Status::Error(PSLICE() << \"Unknown class \\\"\" << str << \"\\\"\");\n" << " return Status::Error(PSLICE() << \"Unknown class \\\"\" << str << \"\\\"\");\n"
<< " }\n" << " }\n"
<< " return it->second;\n"; << " return it->second;\n";
sb << "}\n"; sb << "}\n\n";
} }
void gen_tl_constructor_from_string(StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode) { void gen_tl_constructor_from_string(StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode) {
@ -221,7 +214,7 @@ void gen_json_converter_file(const tl::simple::Schema &schema, const std::string
sb << "#include <unordered_map>\n\n"; sb << "#include <unordered_map>\n\n";
} }
sb << "namespace td {\n"; sb << "namespace td {\n";
sb << "namespace td_api{\n"; sb << "namespace td_api{\n\n";
gen_tl_constructor_from_string(sb, schema, is_header, mode); gen_tl_constructor_from_string(sb, schema, is_header, mode);
gen_from_json(sb, schema, is_header, mode); gen_from_json(sb, schema, is_header, mode);
gen_to_json(sb, schema, is_header, mode); gen_to_json(sb, schema, is_header, mode);

View File

@ -45,7 +45,7 @@ static std::pair<td_api::object_ptr<td_api::Function>, string> to_request(Slice
} }
td_api::object_ptr<td_api::Function> func; td_api::object_ptr<td_api::Function> func;
auto status = from_json(func, json_value); auto status = from_json(func, std::move(json_value));
if (status.is_error()) { if (status.is_error()) {
return {get_return_error_function(PSLICE() return {get_return_error_function(PSLICE()
<< "Failed to parse JSON object as TDLib request: " << status.error().message()), << "Failed to parse JSON object as TDLib request: " << status.error().message()),

View File

@ -76,8 +76,11 @@ void to_json(JsonValueScope &jv, const std::vector<T> &v) {
} }
} }
inline Status from_json(int32 &to, JsonValue &from) { inline Status from_json(int32 &to, JsonValue from) {
if (from.type() != JsonValue::Type::Number && from.type() != JsonValue::Type::String) { if (from.type() != JsonValue::Type::Number && from.type() != JsonValue::Type::String) {
if (from.type() == JsonValue::Type::Null) {
return Status::OK();
}
return Status::Error(PSLICE() << "Expected Number, got " << from.type()); return Status::Error(PSLICE() << "Expected Number, got " << from.type());
} }
Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number(); Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number();
@ -85,10 +88,13 @@ inline Status from_json(int32 &to, JsonValue &from) {
return Status::OK(); return Status::OK();
} }
inline Status from_json(bool &to, JsonValue &from) { inline Status from_json(bool &to, JsonValue from) {
if (from.type() != JsonValue::Type::Boolean) { if (from.type() != JsonValue::Type::Boolean) {
if (from.type() == JsonValue::Type::Null) {
return Status::OK();
}
int32 x = 0; int32 x = 0;
auto status = from_json(x, from); auto status = from_json(x, std::move(from));
if (status.is_ok()) { if (status.is_ok()) {
to = x != 0; to = x != 0;
return Status::OK(); return Status::OK();
@ -99,8 +105,11 @@ inline Status from_json(bool &to, JsonValue &from) {
return Status::OK(); return Status::OK();
} }
inline Status from_json(int64 &to, JsonValue &from) { inline Status from_json(int64 &to, JsonValue from) {
if (from.type() != JsonValue::Type::Number && from.type() != JsonValue::Type::String) { if (from.type() != JsonValue::Type::Number && from.type() != JsonValue::Type::String) {
if (from.type() == JsonValue::Type::Null) {
return Status::OK();
}
return Status::Error(PSLICE() << "Expected String or Number, got " << from.type()); return Status::Error(PSLICE() << "Expected String or Number, got " << from.type());
} }
Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number(); Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number();
@ -108,24 +117,33 @@ inline Status from_json(int64 &to, JsonValue &from) {
return Status::OK(); return Status::OK();
} }
inline Status from_json(double &to, JsonValue &from) { inline Status from_json(double &to, JsonValue from) {
if (from.type() != JsonValue::Type::Number) { if (from.type() != JsonValue::Type::Number) {
if (from.type() == JsonValue::Type::Null) {
return Status::OK();
}
return Status::Error(PSLICE() << "Expected Number, got " << from.type()); return Status::Error(PSLICE() << "Expected Number, got " << from.type());
} }
to = to_double(from.get_number()); to = to_double(from.get_number());
return Status::OK(); return Status::OK();
} }
inline Status from_json(string &to, JsonValue &from) { inline Status from_json(string &to, JsonValue from) {
if (from.type() != JsonValue::Type::String) { if (from.type() != JsonValue::Type::String) {
if (from.type() == JsonValue::Type::Null) {
return Status::OK();
}
return Status::Error(PSLICE() << "Expected String, got " << from.type()); return Status::Error(PSLICE() << "Expected String, got " << from.type());
} }
to = from.get_string().str(); to = from.get_string().str();
return Status::OK(); return Status::OK();
} }
inline Status from_json_bytes(string &to, JsonValue &from) { inline Status from_json_bytes(string &to, JsonValue from) {
if (from.type() != JsonValue::Type::String) { if (from.type() != JsonValue::Type::String) {
if (from.type() == JsonValue::Type::Null) {
return Status::OK();
}
return Status::Error(PSLICE() << "Expected String, got " << from.type()); return Status::Error(PSLICE() << "Expected String, got " << from.type());
} }
TRY_RESULT_ASSIGN(to, base64_decode(from.get_string())); TRY_RESULT_ASSIGN(to, base64_decode(from.get_string()));
@ -133,14 +151,17 @@ inline Status from_json_bytes(string &to, JsonValue &from) {
} }
template <class T> template <class T>
Status from_json(std::vector<T> &to, JsonValue &from) { Status from_json(std::vector<T> &to, JsonValue from) {
if (from.type() != JsonValue::Type::Array) { if (from.type() != JsonValue::Type::Array) {
if (from.type() == JsonValue::Type::Null) {
return Status::OK();
}
return Status::Error(PSLICE() << "Expected Array, got " << from.type()); return Status::Error(PSLICE() << "Expected Array, got " << from.type());
} }
to = std::vector<T>(from.get_array().size()); to = std::vector<T>(from.get_array().size());
size_t i = 0; size_t i = 0;
for (auto &value : from.get_array()) { for (auto &value : from.get_array()) {
TRY_STATUS(from_json(to[i], value)); TRY_STATUS(from_json(to[i], std::move(value)));
i++; i++;
} }
return Status::OK(); return Status::OK();
@ -162,7 +183,7 @@ class DowncastHelper : public T {
}; };
template <class T> template <class T>
std::enable_if_t<!std::is_constructible<T>::value, Status> from_json(tl_object_ptr<T> &to, JsonValue &from) { std::enable_if_t<!std::is_constructible<T>::value, Status> from_json(tl_object_ptr<T> &to, JsonValue from) {
if (from.type() != JsonValue::Type::Object) { if (from.type() != JsonValue::Type::Object) {
if (from.type() == JsonValue::Type::Null) { if (from.type() == JsonValue::Type::Null) {
to = nullptr; to = nullptr;
@ -198,7 +219,7 @@ std::enable_if_t<!std::is_constructible<T>::value, Status> from_json(tl_object_p
} }
template <class T> template <class T>
std::enable_if_t<std::is_constructible<T>::value, Status> from_json(tl_object_ptr<T> &to, JsonValue &from) { std::enable_if_t<std::is_constructible<T>::value, Status> from_json(tl_object_ptr<T> &to, JsonValue from) {
if (from.type() != JsonValue::Type::Object) { if (from.type() != JsonValue::Type::Object) {
if (from.type() == JsonValue::Type::Null) { if (from.type() == JsonValue::Type::Null) {
to = nullptr; to = nullptr;

View File

@ -21,7 +21,6 @@
namespace td { namespace td {
namespace tl { namespace tl {
namespace simple { namespace simple {
// TL type is
std::string gen_cpp_name(std::string name) { std::string gen_cpp_name(std::string name) {
for (std::size_t i = 0; i < name.size(); i++) { for (std::size_t i = 0; i < name.size(); i++) {
@ -132,9 +131,11 @@ class Schema {
void mark_result(const Type *type) { void mark_result(const Type *type) {
do_mark(type, true); do_mark(type, true);
} }
void mark_query(const Type *type) { void mark_query(const Type *type) {
do_mark(type, false); do_mark(type, false);
} }
void do_mark(const Type *type, bool is_result) { void do_mark(const Type *type, bool is_result) {
if (type->type == Type::Vector) { if (type->type == Type::Vector) {
return do_mark(type->vector_value_type, is_result); return do_mark(type->vector_value_type, is_result);
@ -190,6 +191,7 @@ class Schema {
} }
return type; return type;
} }
const CustomType *get_custom_type(const tl_type *from_type) { const CustomType *get_custom_type(const tl_type *from_type) {
auto *type = get_type(from_type); auto *type = get_type(from_type);
assert(type->type == Type::Custom); assert(type->type == Type::Custom);
@ -213,6 +215,7 @@ class Schema {
} }
return constructor; return constructor;
} }
const Function *get_function(const tl_combinator *from) { const Function *get_function(const tl_combinator *from) {
auto &function = function_by_id[from->id]; auto &function = function_by_id[from->id];
if (!function) { if (!function) {
@ -230,6 +233,7 @@ class Schema {
} }
return function; return function;
} }
const Type *get_type(const tl_tree *tree) { const Type *get_type(const tl_tree *tree) {
assert(tree->get_type() == NODE_TYPE_TYPE); assert(tree->get_type() == NODE_TYPE_TYPE);
auto *type_tree = static_cast<const tl_tree_type *>(tree); auto *type_tree = static_cast<const tl_tree_type *>(tree);