mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
Support optional fields in tl json generator
This commit is contained in:
parent
c955a5333d
commit
392cf64758
2 changed files with 35 additions and 5 deletions
|
@ -81,12 +81,16 @@ struct Type {
|
||||||
struct Arg {
|
struct Arg {
|
||||||
const Type *type;
|
const Type *type;
|
||||||
std::string name;
|
std::string name;
|
||||||
|
int var_num = -1;
|
||||||
|
int exist_var_num = -1;
|
||||||
|
int exist_var_bit = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Constructor {
|
struct Constructor {
|
||||||
std::string name;
|
std::string name;
|
||||||
std::int32_t id;
|
std::int32_t id;
|
||||||
std::vector<Arg> args;
|
std::vector<Arg> args;
|
||||||
|
int var_count = 0;
|
||||||
const CustomType *type;
|
const CustomType *type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -100,6 +104,7 @@ struct CustomType {
|
||||||
|
|
||||||
struct Function {
|
struct Function {
|
||||||
std::string name;
|
std::string name;
|
||||||
|
int var_count = 0;
|
||||||
std::int32_t id;
|
std::int32_t id;
|
||||||
std::vector<Arg> args;
|
std::vector<Arg> args;
|
||||||
const Type *type;
|
const Type *type;
|
||||||
|
@ -248,11 +253,15 @@ class Schema {
|
||||||
constructor = constructors_.back().get();
|
constructor = constructors_.back().get();
|
||||||
constructor->id = from->id;
|
constructor->id = from->id;
|
||||||
constructor->name = from->name;
|
constructor->name = from->name;
|
||||||
|
constructor->var_count = from->var_count;
|
||||||
constructor->type = get_custom_type(config_->get_type(from->type_id));
|
constructor->type = get_custom_type(config_->get_type(from->type_id));
|
||||||
for (auto &from_arg : from->args) {
|
for (auto &from_arg : from->args) {
|
||||||
Arg arg;
|
Arg arg;
|
||||||
arg.name = from_arg.name;
|
arg.name = from_arg.name;
|
||||||
arg.type = get_type(from_arg.type);
|
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));
|
constructor->args.push_back(std::move(arg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -266,11 +275,15 @@ class Schema {
|
||||||
function = functions_.back().get();
|
function = functions_.back().get();
|
||||||
function->id = from->id;
|
function->id = from->id;
|
||||||
function->name = from->name;
|
function->name = from->name;
|
||||||
|
function->var_count = from->var_count;
|
||||||
function->type = get_type(config_->get_type(from->type_id));
|
function->type = get_type(config_->get_type(from->type_id));
|
||||||
for (auto &from_arg : from->args) {
|
for (auto &from_arg : from->args) {
|
||||||
Arg arg;
|
Arg arg;
|
||||||
arg.name = from_arg.name;
|
arg.name = from_arg.name;
|
||||||
arg.type = get_type(from_arg.type);
|
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));
|
function->args.push_back(std::move(arg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,13 +48,30 @@ void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_he
|
||||||
sb << " {\n";
|
sb << " {\n";
|
||||||
sb << " auto jo = jv.enter_object();\n";
|
sb << " auto jo = jv.enter_object();\n";
|
||||||
sb << " jo(\"@type\", \"" << constructor->name << "\");\n";
|
sb << " jo(\"@type\", \"" << constructor->name << "\");\n";
|
||||||
|
std::vector<std::string> 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) {
|
for (auto &arg : constructor->args) {
|
||||||
auto field_name = tl::simple::gen_cpp_field_name(arg.name);
|
auto field_name = tl::simple::gen_cpp_field_name(arg.name);
|
||||||
// TODO: or as null
|
bool is_optional = arg.type->type == tl::simple::Type::Custom || arg.exist_var_num >= 0;
|
||||||
bool is_custom = arg.type->type == tl::simple::Type::Custom;
|
|
||||||
|
|
||||||
if (is_custom) {
|
if (is_optional) {
|
||||||
sb << " if (object." << field_name << ") {\n ";
|
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);
|
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) {
|
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 << "}";
|
object = PSTRING() << "JsonVectorInt64{" << object << "}";
|
||||||
}
|
}
|
||||||
sb << " jo(\"" << arg.name << "\", ToJson(" << object << "));\n";
|
sb << " jo(\"" << arg.name << "\", ToJson(" << object << "));\n";
|
||||||
if (is_custom) {
|
if (is_optional) {
|
||||||
sb << " }\n";
|
sb << " }\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue