diff --git a/tdtl/td/tl/tl_simple.h b/tdtl/td/tl/tl_simple.h index dd5a1e53..06d6ea13 100644 --- a/tdtl/td/tl/tl_simple.h +++ b/tdtl/td/tl/tl_simple.h @@ -81,12 +81,16 @@ struct Type { struct Arg { const Type *type; std::string name; + int var_num = -1; + int exist_var_num = -1; + int exist_var_bit = -1; }; struct Constructor { std::string name; std::int32_t id; std::vector args; + int var_count = 0; const CustomType *type; }; @@ -100,6 +104,7 @@ struct CustomType { struct Function { std::string name; + int var_count = 0; std::int32_t id; std::vector args; const Type *type; @@ -248,11 +253,15 @@ class Schema { constructor = constructors_.back().get(); constructor->id = from->id; constructor->name = from->name; + constructor->var_count = from->var_count; constructor->type = get_custom_type(config_->get_type(from->type_id)); for (auto &from_arg : from->args) { Arg arg; arg.name = from_arg.name; arg.type = get_type(from_arg.type); + arg.var_num = from_arg.var_num; + arg.exist_var_num = from_arg.exist_var_num; + arg.exist_var_bit = from_arg.exist_var_bit; constructor->args.push_back(std::move(arg)); } } @@ -266,11 +275,15 @@ class Schema { function = functions_.back().get(); function->id = from->id; function->name = from->name; + function->var_count = from->var_count; function->type = get_type(config_->get_type(from->type_id)); for (auto &from_arg : from->args) { Arg arg; arg.name = from_arg.name; arg.type = get_type(from_arg.type); + arg.var_num = from_arg.var_num; + arg.exist_var_num = from_arg.exist_var_num; + arg.exist_var_bit = from_arg.exist_var_bit; function->args.push_back(std::move(arg)); } } diff --git a/tl/generate/tl_json_converter.cpp b/tl/generate/tl_json_converter.cpp index a0213ef5..d1a0488f 100644 --- a/tl/generate/tl_json_converter.cpp +++ b/tl/generate/tl_json_converter.cpp @@ -48,13 +48,30 @@ void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_he sb << " {\n"; sb << " auto jo = jv.enter_object();\n"; sb << " jo(\"@type\", \"" << constructor->name << "\");\n"; + std::vector var_names(constructor->var_count); + for (auto &arg : constructor->args) { + if (arg.var_num >= 0) { + CHECK(arg.var_num < (int)var_names.size()); + var_names[arg.var_num] = tl::simple::gen_cpp_field_name(arg.name); + } + } for (auto &arg : constructor->args) { auto field_name = tl::simple::gen_cpp_field_name(arg.name); - // TODO: or as null - bool is_custom = arg.type->type == tl::simple::Type::Custom; + bool is_optional = arg.type->type == tl::simple::Type::Custom || arg.exist_var_num >= 0; - if (is_custom) { - sb << " if (object." << field_name << ") {\n "; + if (is_optional) { + sb << " if ("; + if (arg.type->type == tl::simple::Type::Custom) { + sb << "object." << field_name; + if (arg.exist_var_num >= 0) { + sb << " && "; + } + } + if (arg.exist_var_num >= 0) { + CHECK(arg.exist_var_num < (int)var_names.size()); + sb << "(object." << var_names[arg.exist_var_num] << " & " << (1 << arg.exist_var_bit) << ")"; + } + sb << ") {\n "; } auto object = PSTRING() << "object." << tl::simple::gen_cpp_field_name(arg.name); if (arg.type->type == tl::simple::Type::Bytes || arg.type->type == tl::simple::Type::SecureBytes) { @@ -72,7 +89,7 @@ void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_he object = PSTRING() << "JsonVectorInt64{" << object << "}"; } sb << " jo(\"" << arg.name << "\", ToJson(" << object << "));\n"; - if (is_custom) { + if (is_optional) { sb << " }\n"; } }