mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
Nameroverloads (#7164)
* Define and use Namer overloads * more * NamespacedType needs a public string-like overload * Move Rust FieldOffsetName to Namer LegacyRustFieldOffsetName Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
@@ -160,7 +160,7 @@ class GoGenerator : public BaseGenerator {
|
||||
void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
code += "type " + namer_.Type(struct_def.name) + " struct {\n\t";
|
||||
code += "type " + namer_.Type(struct_def) + " struct {\n\t";
|
||||
|
||||
// _ is reserved in flatbuffers field names, so no chance of name conflict:
|
||||
code += "_tab ";
|
||||
@@ -171,7 +171,7 @@ class GoGenerator : public BaseGenerator {
|
||||
// Construct the name of the type for this enum.
|
||||
std::string GetEnumTypeName(const EnumDef &enum_def) {
|
||||
return WrapInNameSpaceAndTrack(enum_def.defined_namespace,
|
||||
namer_.Type(enum_def.name));
|
||||
namer_.Type(enum_def));
|
||||
}
|
||||
|
||||
// Create a type for the enum values.
|
||||
@@ -192,7 +192,7 @@ class GoGenerator : public BaseGenerator {
|
||||
size_t max_name_length, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "\t";
|
||||
code += namer_.EnumVariant(enum_def.name, ev.name);
|
||||
code += namer_.EnumVariant(enum_def, ev);
|
||||
code += " ";
|
||||
code += std::string(max_name_length - ev.name.length(), ' ');
|
||||
code += GetEnumTypeName(enum_def);
|
||||
@@ -219,7 +219,7 @@ class GoGenerator : public BaseGenerator {
|
||||
size_t max_name_length, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "\t";
|
||||
code += namer_.EnumVariant(enum_def.name, ev.name);
|
||||
code += namer_.EnumVariant(enum_def, ev);
|
||||
code += ": ";
|
||||
code += std::string(max_name_length - ev.name.length(), ' ');
|
||||
code += "\"";
|
||||
@@ -236,7 +236,7 @@ class GoGenerator : public BaseGenerator {
|
||||
// Generate String() method on enum type.
|
||||
void EnumStringer(const EnumDef &enum_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
const std::string enum_type = namer_.Type(enum_def.name);
|
||||
const std::string enum_type = namer_.Type(enum_def);
|
||||
code += "func (v " + enum_type + ") String() string {\n";
|
||||
code += "\tif s, ok := EnumNames" + enum_type + "[v]; ok {\n";
|
||||
code += "\t\treturn s\n";
|
||||
@@ -250,7 +250,7 @@ class GoGenerator : public BaseGenerator {
|
||||
void BeginEnumValues(const EnumDef &enum_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "var EnumValues";
|
||||
code += namer_.Type(enum_def.name);
|
||||
code += namer_.Type(enum_def);
|
||||
code += " = map[string]" + GetEnumTypeName(enum_def) + "{\n";
|
||||
}
|
||||
|
||||
@@ -262,7 +262,7 @@ class GoGenerator : public BaseGenerator {
|
||||
code += ev.name;
|
||||
code += "\": ";
|
||||
code += std::string(max_name_length - ev.name.length(), ' ');
|
||||
code += namer_.EnumVariant(enum_def.name, ev.name);
|
||||
code += namer_.EnumVariant(enum_def, ev);
|
||||
code += ",\n";
|
||||
}
|
||||
|
||||
@@ -277,7 +277,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
const std::string size_prefix[] = { "", "SizePrefixed" };
|
||||
const std::string struct_type = namer_.Type(struct_def.name);
|
||||
const std::string struct_type = namer_.Type(struct_def);
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
code += "func Get" + size_prefix[i] + "RootAs" + struct_type;
|
||||
@@ -336,7 +336,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name) + "Length(";
|
||||
code += " " + namer_.Function(field) + "Length(";
|
||||
code += ") int " + OffsetPrefix(field);
|
||||
code += "\t\treturn rcv._tab.VectorLen(o)\n\t}\n";
|
||||
code += "\treturn 0\n}\n\n";
|
||||
@@ -348,7 +348,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name) + "Bytes(";
|
||||
code += " " + namer_.Function(field) + "Bytes(";
|
||||
code += ") []byte " + OffsetPrefix(field);
|
||||
code += "\t\treturn rcv._tab.ByteVector(o + rcv._tab.Pos)\n\t}\n";
|
||||
code += "\treturn nil\n}\n\n";
|
||||
@@ -360,7 +360,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string &code = *code_ptr;
|
||||
std::string getter = GenGetter(field.value.type);
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name);
|
||||
code += " " + namer_.Function(field);
|
||||
code += "() " + TypeName(field) + " {\n";
|
||||
code += "\treturn " +
|
||||
CastToEnum(field.value.type,
|
||||
@@ -375,7 +375,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string &code = *code_ptr;
|
||||
std::string getter = GenGetter(field.value.type);
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name);
|
||||
code += " " + namer_.Function(field);
|
||||
code += "() " + TypeName(field) + " ";
|
||||
code += OffsetPrefix(field);
|
||||
if (field.IsScalarOptional()) {
|
||||
@@ -398,7 +398,7 @@ class GoGenerator : public BaseGenerator {
|
||||
const FieldDef &field, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name);
|
||||
code += " " + namer_.Function(field);
|
||||
code += "(obj *" + TypeName(field);
|
||||
code += ") *" + TypeName(field);
|
||||
code += " {\n";
|
||||
@@ -417,7 +417,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name);
|
||||
code += " " + namer_.Function(field);
|
||||
code += "(obj *";
|
||||
code += TypeName(field);
|
||||
code += ") *" + TypeName(field) + " " + OffsetPrefix(field);
|
||||
@@ -439,7 +439,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name);
|
||||
code += " " + namer_.Function(field);
|
||||
code += "() " + TypeName(field) + " ";
|
||||
code += OffsetPrefix(field) + "\t\treturn " + GenGetter(field.value.type);
|
||||
code += "(o + rcv._tab.Pos)\n\t}\n\treturn nil\n";
|
||||
@@ -451,7 +451,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name) + "(";
|
||||
code += " " + namer_.Function(field) + "(";
|
||||
code += "obj " + GenTypePointer(field.value.type) + ") bool ";
|
||||
code += OffsetPrefix(field);
|
||||
code += "\t\t" + GenGetter(field.value.type);
|
||||
@@ -467,7 +467,7 @@ class GoGenerator : public BaseGenerator {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name);
|
||||
code += " " + namer_.Function(field);
|
||||
code += "(obj *" + TypeName(field);
|
||||
code += ", j int) bool " + OffsetPrefix(field);
|
||||
code += "\t\tx := rcv._tab.Vector(o)\n";
|
||||
@@ -490,7 +490,7 @@ class GoGenerator : public BaseGenerator {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " " + namer_.Function(field.name);
|
||||
code += " " + namer_.Function(field);
|
||||
code += "(j int) " + TypeName(field) + " ";
|
||||
code += OffsetPrefix(field);
|
||||
code += "\t\ta := rcv._tab.Vector(o)\n";
|
||||
@@ -538,7 +538,7 @@ class GoGenerator : public BaseGenerator {
|
||||
} else {
|
||||
std::string &code = *code_ptr;
|
||||
code += std::string(", ") + nameprefix;
|
||||
code += namer_.Variable(field.name);
|
||||
code += namer_.Variable(field);
|
||||
code += " " + TypeName(field);
|
||||
}
|
||||
}
|
||||
@@ -568,7 +568,7 @@ class GoGenerator : public BaseGenerator {
|
||||
} else {
|
||||
code += "\tbuilder.Prepend" + GenMethod(field) + "(";
|
||||
code += CastToBaseType(field.value.type,
|
||||
nameprefix + namer_.Variable(field.name)) +
|
||||
nameprefix + namer_.Variable(field)) +
|
||||
")\n";
|
||||
}
|
||||
}
|
||||
@@ -583,7 +583,7 @@ class GoGenerator : public BaseGenerator {
|
||||
// Get the value of a table's starting offset.
|
||||
void GetStartOfTable(const StructDef &struct_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func " + namer_.Type(struct_def.name) + "Start";
|
||||
code += "func " + namer_.Type(struct_def) + "Start";
|
||||
code += "(builder *flatbuffers.Builder) {\n";
|
||||
code += "\tbuilder.StartObject(";
|
||||
code += NumToString(struct_def.fields.vec.size());
|
||||
@@ -594,9 +594,8 @@ class GoGenerator : public BaseGenerator {
|
||||
void BuildFieldOfTable(const StructDef &struct_def, const FieldDef &field,
|
||||
const size_t offset, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
const std::string field_var = namer_.Variable(field.name);
|
||||
code += "func " + namer_.Type(struct_def.name) + "Add" +
|
||||
namer_.Function(field.name);
|
||||
const std::string field_var = namer_.Variable(field);
|
||||
code += "func " + namer_.Type(struct_def) + "Add" + namer_.Function(field);
|
||||
code += "(builder *flatbuffers.Builder, ";
|
||||
code += field_var + " ";
|
||||
if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) {
|
||||
@@ -632,8 +631,8 @@ class GoGenerator : public BaseGenerator {
|
||||
void BuildVectorOfTable(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func " + namer_.Type(struct_def.name) + "Start";
|
||||
code += namer_.Function(field.name);
|
||||
code += "func " + namer_.Type(struct_def) + "Start";
|
||||
code += namer_.Function(field);
|
||||
code += "Vector(builder *flatbuffers.Builder, numElems int) ";
|
||||
code += "flatbuffers.UOffsetT {\n\treturn builder.StartVector(";
|
||||
auto vector_type = field.value.type.VectorType();
|
||||
@@ -647,7 +646,7 @@ class GoGenerator : public BaseGenerator {
|
||||
// Get the offset of the end of a table.
|
||||
void GetEndOffsetOnTable(const StructDef &struct_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func " + namer_.Type(struct_def.name) + "End";
|
||||
code += "func " + namer_.Type(struct_def) + "End";
|
||||
code += "(builder *flatbuffers.Builder) flatbuffers.UOffsetT ";
|
||||
code += "{\n\treturn builder.EndObject()\n}\n";
|
||||
}
|
||||
@@ -655,7 +654,7 @@ class GoGenerator : public BaseGenerator {
|
||||
// Generate the receiver for function signatures.
|
||||
void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "func (rcv *" + namer_.Type(struct_def.name) + ")";
|
||||
code += "func (rcv *" + namer_.Type(struct_def) + ")";
|
||||
}
|
||||
|
||||
// Generate a struct field getter, conditioned on its child type(s).
|
||||
@@ -708,7 +707,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string setter =
|
||||
"rcv._tab.Mutate" + namer_.Method(GenTypeBasic(field.value.type));
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " Mutate" + namer_.Function(field.name);
|
||||
code += " Mutate" + namer_.Function(field);
|
||||
code += "(n " + GenTypeGet(field.value.type) + ") bool {\n\treturn " + setter;
|
||||
code += "(rcv._tab.Pos+flatbuffers.UOffsetT(";
|
||||
code += NumToString(field.value.offset) + "), ";
|
||||
@@ -722,7 +721,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string setter = "rcv._tab.Mutate" +
|
||||
namer_.Method(GenTypeBasic(field.value.type)) + "Slot";
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " Mutate" + namer_.Function(field.name);
|
||||
code += " Mutate" + namer_.Function(field);
|
||||
code += "(n " + GenTypeGet(field.value.type) + ") bool {\n\treturn ";
|
||||
code += setter + "(" + NumToString(field.value.offset) + ", ";
|
||||
code += CastToBaseType(field.value.type, "n") + ")\n";
|
||||
@@ -738,7 +737,7 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string setter =
|
||||
"rcv._tab.Mutate" + namer_.Method(GenTypeBasic(vectortype));
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += " Mutate" + namer_.Function(field.name);
|
||||
code += " Mutate" + namer_.Function(field);
|
||||
code += "(j int, n " + TypeName(field) + ") bool ";
|
||||
code += OffsetPrefix(field);
|
||||
code += "\t\ta := rcv._tab.Vector(o)\n";
|
||||
@@ -841,7 +840,7 @@ class GoGenerator : public BaseGenerator {
|
||||
field.value.type.enum_def != nullptr &&
|
||||
field.value.type.enum_def->is_union)
|
||||
continue;
|
||||
code += "\t" + namer_.Field(field.name) + " ";
|
||||
code += "\t" + namer_.Field(field) + " ";
|
||||
if (field.IsScalarOptional()) {
|
||||
code += "*";
|
||||
}
|
||||
@@ -861,7 +860,7 @@ class GoGenerator : public BaseGenerator {
|
||||
void GenNativeUnion(const EnumDef &enum_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "type " + NativeName(enum_def) + " struct {\n";
|
||||
code += "\tType " + namer_.Type(enum_def.name) + "\n";
|
||||
code += "\tType " + namer_.Type(enum_def) + "\n";
|
||||
code += "\tValue interface{}\n";
|
||||
code += "}\n\n";
|
||||
}
|
||||
@@ -877,7 +876,7 @@ class GoGenerator : public BaseGenerator {
|
||||
++it2) {
|
||||
const EnumVal &ev = **it2;
|
||||
if (ev.IsZero()) continue;
|
||||
code += "\tcase " + namer_.EnumVariant(enum_def.name, ev.name) + ":\n";
|
||||
code += "\tcase " + namer_.EnumVariant(enum_def, ev) + ":\n";
|
||||
code += "\t\treturn t.Value.(" + NativeType(ev.union_type) +
|
||||
").Pack(builder)\n";
|
||||
}
|
||||
@@ -889,7 +888,7 @@ class GoGenerator : public BaseGenerator {
|
||||
void GenNativeUnionUnPack(const EnumDef &enum_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
code += "func (rcv " + namer_.Type(enum_def.name) +
|
||||
code += "func (rcv " + namer_.Type(enum_def) +
|
||||
") UnPack(table flatbuffers.Table) *" + NativeName(enum_def) +
|
||||
" {\n";
|
||||
code += "\tswitch rcv {\n";
|
||||
@@ -898,13 +897,13 @@ class GoGenerator : public BaseGenerator {
|
||||
++it2) {
|
||||
const EnumVal &ev = **it2;
|
||||
if (ev.IsZero()) continue;
|
||||
code += "\tcase " + namer_.EnumVariant(enum_def.name, ev.name) + ":\n";
|
||||
code += "\tcase " + namer_.EnumVariant(enum_def, ev) + ":\n";
|
||||
code += "\t\tx := " + ev.union_type.struct_def->name + "{_tab: table}\n";
|
||||
|
||||
code += "\t\treturn &" +
|
||||
WrapInNameSpaceAndTrack(enum_def.defined_namespace,
|
||||
NativeName(enum_def)) +
|
||||
"{ Type: " + namer_.EnumVariant(enum_def.name, ev.name) +
|
||||
"{ Type: " + namer_.EnumVariant(enum_def, ev) +
|
||||
", Value: x.UnPack() }\n";
|
||||
}
|
||||
code += "\t}\n";
|
||||
@@ -914,7 +913,7 @@ class GoGenerator : public BaseGenerator {
|
||||
|
||||
void GenNativeTablePack(const StructDef &struct_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
const std::string struct_type = namer_.Type(struct_def.name);
|
||||
const std::string struct_type = namer_.Type(struct_def);
|
||||
|
||||
code += "func (t *" + NativeName(struct_def) +
|
||||
") Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {\n";
|
||||
@@ -925,8 +924,8 @@ class GoGenerator : public BaseGenerator {
|
||||
if (field.deprecated) continue;
|
||||
if (IsScalar(field.value.type.base_type)) continue;
|
||||
|
||||
const std::string field_field = namer_.Field(field.name);
|
||||
const std::string field_var = namer_.Variable(field.name);
|
||||
const std::string field_field = namer_.Field(field);
|
||||
const std::string field_var = namer_.Variable(field);
|
||||
const std::string offset = field_var + "Offset";
|
||||
|
||||
if (IsString(field.value.type)) {
|
||||
@@ -962,7 +961,7 @@ class GoGenerator : public BaseGenerator {
|
||||
"[j].Pack(builder)\n";
|
||||
code += "\t\t}\n";
|
||||
}
|
||||
code += "\t\t" + struct_type + "Start" + namer_.Function(field.name) +
|
||||
code += "\t\t" + struct_type + "Start" + namer_.Function(field) +
|
||||
"Vector(builder, " + length + ")\n";
|
||||
code += "\t\tfor j := " + length + " - 1; j >= 0; j-- {\n";
|
||||
if (IsScalar(field.value.type.element)) {
|
||||
@@ -996,9 +995,9 @@ class GoGenerator : public BaseGenerator {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
const FieldDef &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
const std::string field_field = namer_.Field(field.name);
|
||||
const std::string field_fn = namer_.Function(field.name);
|
||||
const std::string offset = namer_.Variable(field.name) + "Offset";
|
||||
const std::string field_field = namer_.Field(field);
|
||||
const std::string field_fn = namer_.Function(field);
|
||||
const std::string offset = namer_.Variable(field) + "Offset";
|
||||
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
std::string prefix;
|
||||
@@ -1037,7 +1036,7 @@ class GoGenerator : public BaseGenerator {
|
||||
void GenNativeTableUnPack(const StructDef &struct_def,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
const std::string struct_type = namer_.Type(struct_def.name);
|
||||
const std::string struct_type = namer_.Type(struct_def);
|
||||
|
||||
code += "func (rcv *" + struct_type + ") UnPackTo(t *" +
|
||||
NativeName(struct_def) + ") {\n";
|
||||
@@ -1045,8 +1044,8 @@ class GoGenerator : public BaseGenerator {
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
const FieldDef &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
const std::string field_field = namer_.Field(field.name);
|
||||
const std::string field_var = namer_.Variable(field.name);
|
||||
const std::string field_field = namer_.Field(field);
|
||||
const std::string field_var = namer_.Variable(field);
|
||||
const std::string length = field_var + "Length";
|
||||
if (IsScalar(field.value.type.base_type)) {
|
||||
if (field.value.type.enum_def != nullptr &&
|
||||
@@ -1089,8 +1088,8 @@ class GoGenerator : public BaseGenerator {
|
||||
} else if (field.value.type.base_type == BASE_TYPE_UNION) {
|
||||
const std::string field_table = field_var + "Table";
|
||||
code += "\t" + field_table + " := flatbuffers.Table{}\n";
|
||||
code += "\tif rcv." + namer_.Method(field.name) + "(&" + field_table +
|
||||
") {\n";
|
||||
code +=
|
||||
"\tif rcv." + namer_.Method(field) + "(&" + field_table + ") {\n";
|
||||
code += "\t\tt." + field_field + " = rcv." +
|
||||
namer_.Method(field.name + UnionTypeFieldSuffix()) +
|
||||
"().UnPack(" + field_table + ")\n";
|
||||
@@ -1116,7 +1115,7 @@ class GoGenerator : public BaseGenerator {
|
||||
code += "func (t *" + NativeName(struct_def) +
|
||||
") Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {\n";
|
||||
code += "\tif t == nil { return 0 }\n";
|
||||
code += "\treturn Create" + namer_.Type(struct_def.name) + "(builder";
|
||||
code += "\treturn Create" + namer_.Type(struct_def) + "(builder";
|
||||
StructPackArgs(struct_def, "", code_ptr);
|
||||
code += ")\n";
|
||||
code += "}\n";
|
||||
@@ -1130,10 +1129,10 @@ class GoGenerator : public BaseGenerator {
|
||||
const FieldDef &field = **it;
|
||||
if (field.value.type.base_type == BASE_TYPE_STRUCT) {
|
||||
StructPackArgs(*field.value.type.struct_def,
|
||||
(nameprefix + namer_.Field(field.name) + ".").c_str(),
|
||||
(nameprefix + namer_.Field(field) + ".").c_str(),
|
||||
code_ptr);
|
||||
} else {
|
||||
code += std::string(", t.") + nameprefix + namer_.Field(field.name);
|
||||
code += std::string(", t.") + nameprefix + namer_.Field(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1142,22 +1141,22 @@ class GoGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
|
||||
code += "func (rcv *" + namer_.Type(struct_def.name) + ") UnPackTo(t *" +
|
||||
code += "func (rcv *" + namer_.Type(struct_def) + ") UnPackTo(t *" +
|
||||
NativeName(struct_def) + ") {\n";
|
||||
for (auto it = struct_def.fields.vec.begin();
|
||||
it != struct_def.fields.vec.end(); ++it) {
|
||||
const FieldDef &field = **it;
|
||||
if (field.value.type.base_type == BASE_TYPE_STRUCT) {
|
||||
code += "\tt." + namer_.Field(field.name) + " = rcv." +
|
||||
namer_.Method(field.name) + "(nil).UnPack()\n";
|
||||
code += "\tt." + namer_.Field(field) + " = rcv." +
|
||||
namer_.Method(field) + "(nil).UnPack()\n";
|
||||
} else {
|
||||
code += "\tt." + namer_.Field(field.name) + " = rcv." +
|
||||
namer_.Method(field.name) + "()\n";
|
||||
code += "\tt." + namer_.Field(field) + " = rcv." +
|
||||
namer_.Method(field) + "()\n";
|
||||
}
|
||||
}
|
||||
code += "}\n\n";
|
||||
|
||||
code += "func (rcv *" + namer_.Type(struct_def.name) + ") UnPack() *" +
|
||||
code += "func (rcv *" + namer_.Type(struct_def) + ") UnPack() *" +
|
||||
NativeName(struct_def) + " {\n";
|
||||
code += "\tif rcv == nil { return nil }\n";
|
||||
code += "\tt := &" + NativeName(struct_def) + "{}\n";
|
||||
@@ -1285,11 +1284,11 @@ class GoGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
std::string NativeName(const StructDef &struct_def) const {
|
||||
return namer_.ObjectType(struct_def.name);
|
||||
return namer_.ObjectType(struct_def);
|
||||
}
|
||||
|
||||
std::string NativeName(const EnumDef &enum_def) const {
|
||||
return namer_.ObjectType(enum_def.name);
|
||||
return namer_.ObjectType(enum_def);
|
||||
}
|
||||
|
||||
std::string NativeType(const Type &type) {
|
||||
@@ -1366,20 +1365,19 @@ class GoGenerator : public BaseGenerator {
|
||||
while (code.length() > 2 && code.substr(code.length() - 2) == "\n\n") {
|
||||
code.pop_back();
|
||||
}
|
||||
std::string filename = namer_.Directories(ns.components) +
|
||||
namer_.File(def.name, SkipFile::Suffix);
|
||||
std::string filename =
|
||||
namer_.Directories(ns) + namer_.File(def, SkipFile::Suffix);
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
|
||||
// Create the full name of the imported namespace (format: A__B__C).
|
||||
std::string NamespaceImportName(const Namespace *ns) const {
|
||||
return namer_.Namespace(ns->components);
|
||||
return namer_.Namespace(*ns);
|
||||
}
|
||||
|
||||
// Create the full path for the imported namespace (format: A/B/C).
|
||||
std::string NamespaceImportPath(const Namespace *ns) const {
|
||||
return namer_.Directories(ns->components,
|
||||
SkipDir::OutputPathAndTrailingPathSeparator);
|
||||
return namer_.Directories(*ns, SkipDir::OutputPathAndTrailingPathSeparator);
|
||||
}
|
||||
|
||||
// Ensure that a type is prefixed with its go package import name if it is
|
||||
|
||||
@@ -88,7 +88,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
// Begin a class declaration.
|
||||
void BeginClass(const StructDef &struct_def, std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
code += "class " + namer_.Type(struct_def.name) + "(object):\n";
|
||||
code += "class " + namer_.Type(struct_def) + "(object):\n";
|
||||
code += Indent + "__slots__ = ['_tab']";
|
||||
code += "\n\n";
|
||||
}
|
||||
@@ -96,7 +96,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
// Begin enum code with a class declaration.
|
||||
void BeginEnum(const EnumDef &enum_def, std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
code += "class " + namer_.Type(enum_def.name) + "(object):\n";
|
||||
code += "class " + namer_.Type(enum_def) + "(object):\n";
|
||||
}
|
||||
|
||||
// Starts a new line and then indents.
|
||||
@@ -109,7 +109,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
code += Indent;
|
||||
code += namer_.Variant(ev.name);
|
||||
code += namer_.Variant(ev);
|
||||
code += " = ";
|
||||
code += enum_def.ToString(ev) + "\n";
|
||||
}
|
||||
@@ -118,7 +118,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
void NewRootTypeFromBuffer(const StructDef &struct_def,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const std::string struct_type = namer_.Type(struct_def.name);
|
||||
const std::string struct_type = namer_.Type(struct_def);
|
||||
|
||||
code += Indent + "@classmethod\n";
|
||||
code += Indent + "def GetRootAs";
|
||||
@@ -158,7 +158,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &code = *code_ptr;
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name) + "Length(self";
|
||||
code += namer_.Method(field) + "Length(self";
|
||||
code += "):" + OffsetPrefix(field);
|
||||
code += Indent + Indent + Indent + "return self._tab.VectorLen(o)\n";
|
||||
code += Indent + Indent + "return 0\n\n";
|
||||
@@ -170,7 +170,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &code = *code_ptr;
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name) + "IsNone(self";
|
||||
code += namer_.Method(field) + "IsNone(self";
|
||||
code += "):";
|
||||
code += GenIndents(2) +
|
||||
"o = flatbuffers.number_types.UOffsetTFlags.py_type" +
|
||||
@@ -186,7 +186,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &code = *code_ptr;
|
||||
std::string getter = GenGetter(field.value.type);
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name);
|
||||
code += namer_.Method(field);
|
||||
code += "(self): return " + getter;
|
||||
code += "self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(";
|
||||
code += NumToString(field.value.offset) + "))\n";
|
||||
@@ -198,7 +198,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &code = *code_ptr;
|
||||
std::string getter = GenGetter(field.value.type);
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name);
|
||||
code += namer_.Method(field);
|
||||
code += "(self):";
|
||||
code += OffsetPrefix(field);
|
||||
getter += "o + self._tab.Pos)";
|
||||
@@ -223,7 +223,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name);
|
||||
code += namer_.Method(field);
|
||||
code += "(self, obj):\n";
|
||||
code += Indent + Indent + "obj.Init(self._tab.Bytes, self._tab.Pos + ";
|
||||
code += NumToString(field.value.offset) + ")";
|
||||
@@ -236,7 +236,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &code = *code_ptr;
|
||||
const auto vec_type = field.value.type.VectorType();
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name);
|
||||
code += namer_.Method(field);
|
||||
if (IsStruct(vec_type)) {
|
||||
code += "(self, obj, i):\n";
|
||||
code += Indent + Indent + "obj.Init(self._tab.Bytes, self._tab.Pos + ";
|
||||
@@ -260,7 +260,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name);
|
||||
code += namer_.Method(field);
|
||||
code += "(self):";
|
||||
code += OffsetPrefix(field);
|
||||
if (field.value.type.struct_def->fixed) {
|
||||
@@ -285,7 +285,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name);
|
||||
code += namer_.Method(field);
|
||||
code += "(self):";
|
||||
code += OffsetPrefix(field);
|
||||
code += Indent + Indent + Indent + "return " + GenGetter(field.value.type);
|
||||
@@ -298,7 +298,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name) + "(self):";
|
||||
code += namer_.Method(field) + "(self):";
|
||||
code += OffsetPrefix(field);
|
||||
|
||||
// TODO(rw): this works and is not the good way to it:
|
||||
@@ -321,21 +321,13 @@ class PythonGenerator : public BaseGenerator {
|
||||
// module.
|
||||
std::string GenPackageReference(const Type &type) const {
|
||||
if (type.struct_def) {
|
||||
return GenPackageReference(*type.struct_def);
|
||||
return namer_.NamespacedType(*type.struct_def);
|
||||
} else if (type.enum_def) {
|
||||
return GenPackageReference(*type.enum_def);
|
||||
return namer_.NamespacedType(*type.enum_def);
|
||||
} else {
|
||||
return "." + GenTypeGet(type);
|
||||
}
|
||||
}
|
||||
std::string GenPackageReference(const EnumDef &enum_def) const {
|
||||
return namer_.NamespacedType(enum_def.defined_namespace->components,
|
||||
enum_def.name);
|
||||
}
|
||||
std::string GenPackageReference(const StructDef &struct_def) const {
|
||||
return namer_.NamespacedType(struct_def.defined_namespace->components,
|
||||
struct_def.name);
|
||||
}
|
||||
|
||||
// Get the value of a vector's struct member.
|
||||
void GetMemberOfVectorOfStruct(const StructDef &struct_def,
|
||||
@@ -345,7 +337,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name);
|
||||
code += namer_.Method(field);
|
||||
code += "(self, j):" + OffsetPrefix(field);
|
||||
code += Indent + Indent + Indent + "x = self._tab.Vector(o)\n";
|
||||
code += Indent + Indent + Indent;
|
||||
@@ -374,7 +366,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto vectortype = field.value.type.VectorType();
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name);
|
||||
code += namer_.Method(field);
|
||||
code += "(self, j):";
|
||||
code += OffsetPrefix(field);
|
||||
code += Indent + Indent + Indent + "a = self._tab.Vector(o)\n";
|
||||
@@ -403,7 +395,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
if (!(IsScalar(vectortype.base_type))) { return; }
|
||||
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name) + "AsNumpy(self):";
|
||||
code += namer_.Method(field) + "AsNumpy(self):";
|
||||
code += OffsetPrefix(field);
|
||||
|
||||
code += Indent + Indent + Indent;
|
||||
@@ -445,7 +437,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
auto &code = *code_ptr;
|
||||
GenReceiver(struct_def, code_ptr);
|
||||
code += namer_.Method(field.name) + "NestedRoot(self):";
|
||||
code += namer_.Method(field) + "NestedRoot(self):";
|
||||
|
||||
code += OffsetPrefix(field);
|
||||
|
||||
@@ -464,7 +456,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &code = *code_ptr;
|
||||
|
||||
code += "\n";
|
||||
code += "def Create" + namer_.Type(struct_def.name);
|
||||
code += "def Create" + namer_.Type(struct_def);
|
||||
code += "(builder";
|
||||
}
|
||||
|
||||
@@ -487,14 +479,14 @@ class PythonGenerator : public BaseGenerator {
|
||||
// a nested struct, prefix the name with the field name.
|
||||
auto subprefix = nameprefix;
|
||||
if (has_field_name) {
|
||||
subprefix += namer_.Field(field.name) + fieldname_suffix;
|
||||
subprefix += namer_.Field(field) + fieldname_suffix;
|
||||
}
|
||||
StructBuilderArgs(*field.value.type.struct_def, subprefix, namesuffix,
|
||||
has_field_name, fieldname_suffix, code_ptr);
|
||||
} else {
|
||||
auto &code = *code_ptr;
|
||||
code += std::string(", ") + nameprefix;
|
||||
if (has_field_name) { code += namer_.Field(field.name); }
|
||||
if (has_field_name) { code += namer_.Field(field); }
|
||||
code += namesuffix;
|
||||
}
|
||||
}
|
||||
@@ -526,10 +518,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
code +=
|
||||
indent + " builder.Pad(" + NumToString(field.padding) + ")\n";
|
||||
if (IsStruct(field_type)) {
|
||||
StructBuilderBody(
|
||||
*field_type.struct_def,
|
||||
(nameprefix + (namer_.Field(field.name) + "_")).c_str(), code_ptr,
|
||||
index, in_array);
|
||||
StructBuilderBody(*field_type.struct_def,
|
||||
(nameprefix + (namer_.Field(field) + "_")).c_str(),
|
||||
code_ptr, index, in_array);
|
||||
} else {
|
||||
const auto index_var = "_idx" + NumToString(index);
|
||||
if (IsArray(field_type)) {
|
||||
@@ -539,14 +530,13 @@ class PythonGenerator : public BaseGenerator {
|
||||
in_array = true;
|
||||
}
|
||||
if (IsStruct(type)) {
|
||||
StructBuilderBody(
|
||||
*field_type.struct_def,
|
||||
(nameprefix + (namer_.Field(field.name) + "_")).c_str(), code_ptr,
|
||||
index + 1, in_array);
|
||||
StructBuilderBody(*field_type.struct_def,
|
||||
(nameprefix + (namer_.Field(field) + "_")).c_str(),
|
||||
code_ptr, index + 1, in_array);
|
||||
} else {
|
||||
code += IsArray(field_type) ? " " : "";
|
||||
code += indent + " builder.Prepend" + GenMethod(field) + "(";
|
||||
code += nameprefix + namer_.Variable(field.name);
|
||||
code += nameprefix + namer_.Variable(field);
|
||||
size_t array_cnt = index + (IsArray(field_type) ? 1 : 0);
|
||||
for (size_t i = 0; in_array && i < array_cnt; i++) {
|
||||
code += "[_idx" + NumToString(i) + "-1]";
|
||||
@@ -566,7 +556,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
void GetStartOfTable(const StructDef &struct_def,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto struct_type = namer_.Type(struct_def.name);
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
// Generate method with struct name.
|
||||
code += "def " + struct_type + "Start(builder): ";
|
||||
code += "builder.StartObject(";
|
||||
@@ -584,11 +574,11 @@ class PythonGenerator : public BaseGenerator {
|
||||
void BuildFieldOfTable(const StructDef &struct_def, const FieldDef &field,
|
||||
const size_t offset, std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const std::string field_var = namer_.Variable(field.name);
|
||||
const std::string field_method = namer_.Method(field.name);
|
||||
const std::string field_var = namer_.Variable(field);
|
||||
const std::string field_method = namer_.Method(field);
|
||||
|
||||
// Generate method with struct name.
|
||||
code += "def " + namer_.Type(struct_def.name) + "Add" + field_method;
|
||||
code += "def " + namer_.Type(struct_def) + "Add" + field_method;
|
||||
code += "(builder, ";
|
||||
code += field_var;
|
||||
code += "): ";
|
||||
@@ -610,8 +600,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
if (!parser_.opts.one_file) {
|
||||
// Generate method without struct name.
|
||||
code += "def Add" + field_method + "(builder, " + field_var + "):\n";
|
||||
code += Indent + "return " + namer_.Type(struct_def.name) + "Add" +
|
||||
field_method;
|
||||
code +=
|
||||
Indent + "return " + namer_.Type(struct_def) + "Add" + field_method;
|
||||
code += "(builder, ";
|
||||
code += field_var;
|
||||
code += ")\n";
|
||||
@@ -622,8 +612,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
void BuildVectorOfTable(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const std::string struct_type = namer_.Type(struct_def.name);
|
||||
const std::string field_method = namer_.Method(field.name);
|
||||
const std::string struct_type = namer_.Type(struct_def);
|
||||
const std::string field_method = namer_.Method(field);
|
||||
|
||||
// Generate method with struct name.
|
||||
code += "def " + struct_type + "Start" + field_method;
|
||||
@@ -653,8 +643,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
if (!nested) { return; } // There is no nested flatbuffer.
|
||||
|
||||
auto &code = *code_ptr;
|
||||
const std::string field_method = namer_.Method(field.name);
|
||||
const std::string struct_type = namer_.Type(struct_def.name);
|
||||
const std::string field_method = namer_.Method(field);
|
||||
const std::string struct_type = namer_.Type(struct_def);
|
||||
|
||||
// Generate method with struct and field name.
|
||||
code += "def " + struct_type + "Make" + field_method;
|
||||
@@ -685,22 +675,21 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &code = *code_ptr;
|
||||
|
||||
// Generate method with struct name.
|
||||
code += "def " + namer_.Type(struct_def.name) + "End";
|
||||
code += "def " + namer_.Type(struct_def) + "End";
|
||||
code += "(builder): ";
|
||||
code += "return builder.EndObject()\n";
|
||||
|
||||
if (!parser_.opts.one_file) {
|
||||
// Generate method without struct name.
|
||||
code += "def End(builder):\n";
|
||||
code +=
|
||||
Indent + "return " + namer_.Type(struct_def.name) + "End(builder)";
|
||||
code += Indent + "return " + namer_.Type(struct_def) + "End(builder)";
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the receiver for function signatures.
|
||||
void GenReceiver(const StructDef &struct_def, std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
code += Indent + "# " + namer_.Type(struct_def.name) + "\n";
|
||||
code += Indent + "# " + namer_.Type(struct_def) + "\n";
|
||||
code += Indent + "def ";
|
||||
}
|
||||
|
||||
@@ -795,7 +784,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
code += Indent + "@classmethod\n";
|
||||
code += Indent + "def " + namer_.Type(struct_def.name);
|
||||
code += Indent + "def " + namer_.Type(struct_def);
|
||||
code += "BufferHasIdentifier(cls, buf, offset, size_prefixed=False):";
|
||||
code += "\n";
|
||||
code += Indent + Indent;
|
||||
@@ -846,7 +835,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
void GenReceiverForObjectAPI(const StructDef &struct_def,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
code += GenIndents(1) + "# " + namer_.ObjectType(struct_def.name);
|
||||
code += GenIndents(1) + "# " + namer_.ObjectType(struct_def);
|
||||
code += GenIndents(1) + "def ";
|
||||
}
|
||||
|
||||
@@ -854,7 +843,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
code += "\n";
|
||||
code += "class " + namer_.ObjectType(struct_def.name) + "(object):";
|
||||
code += "class " + namer_.ObjectType(struct_def) + "(object):";
|
||||
code += "\n";
|
||||
}
|
||||
|
||||
@@ -907,7 +896,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string field_type;
|
||||
switch (ev.union_type.base_type) {
|
||||
case BASE_TYPE_STRUCT:
|
||||
field_type = namer_.ObjectType(ev.union_type.struct_def->name);
|
||||
field_type = namer_.ObjectType(*ev.union_type.struct_def);
|
||||
if (parser_.opts.include_dependence_headers) {
|
||||
auto package_reference = GenPackageReference(ev.union_type);
|
||||
field_type = package_reference + "." + field_type;
|
||||
@@ -927,8 +916,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
// Gets the import lists for the union.
|
||||
if (parser_.opts.include_dependence_headers) {
|
||||
const auto package_reference =
|
||||
GenPackageReference(*field.value.type.enum_def);
|
||||
const auto package_reference = GenPackageReference(field.value.type);
|
||||
import_list->insert("import " + package_reference);
|
||||
}
|
||||
}
|
||||
@@ -939,7 +927,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
import_typing_list->insert("Optional");
|
||||
auto &output = *out_ptr;
|
||||
const Type &type = field.value.type;
|
||||
const std::string object_type = namer_.ObjectType(type.struct_def->name);
|
||||
const std::string object_type = namer_.ObjectType(*type.struct_def);
|
||||
if (parser_.opts.include_dependence_headers) {
|
||||
auto package_reference = GenPackageReference(type);
|
||||
output = package_reference + "." + object_type + "]";
|
||||
@@ -959,7 +947,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
const BaseType base_type = vector_type.base_type;
|
||||
if (base_type == BASE_TYPE_STRUCT) {
|
||||
const std::string object_type =
|
||||
namer_.ObjectType(GenTypeGet(vector_type));
|
||||
namer_.ObjectType(*vector_type.struct_def);
|
||||
field_type = object_type + "]";
|
||||
if (parser_.opts.include_dependence_headers) {
|
||||
auto package_reference = GenPackageReference(vector_type);
|
||||
@@ -1007,7 +995,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
const auto default_value = GetDefaultValue(field);
|
||||
// Wrties the init statement.
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
code += GenIndents(2) + "self." + field_field + " = " + default_value +
|
||||
" # type: " + field_type;
|
||||
}
|
||||
@@ -1045,15 +1033,15 @@ class PythonGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
// Removes the import of the struct itself, if applied.
|
||||
auto struct_import = "import " + GenPackageReference(struct_def);
|
||||
auto struct_import = "import " + namer_.NamespacedType(struct_def);
|
||||
import_list->erase(struct_import);
|
||||
}
|
||||
|
||||
void InitializeFromBuf(const StructDef &struct_def,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto struct_type = namer_.Type(struct_def.name);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
|
||||
code += GenIndents(1) + "@classmethod";
|
||||
code += GenIndents(1) + "def InitFromBuf(cls, buf, pos):";
|
||||
@@ -1066,8 +1054,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
void InitializeFromObjForObject(const StructDef &struct_def,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto struct_object = namer_.ObjectType(struct_def.name);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
const auto struct_object = namer_.ObjectType(struct_def);
|
||||
|
||||
code += GenIndents(1) + "@classmethod";
|
||||
code += GenIndents(1) + "def InitFromObj(cls, " + struct_var + "):";
|
||||
@@ -1080,9 +1068,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
void GenUnPackForStruct(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
auto field_type = TypeName(field);
|
||||
|
||||
if (parser_.opts.include_dependence_headers) {
|
||||
@@ -1108,14 +1096,14 @@ class PythonGenerator : public BaseGenerator {
|
||||
void GenUnPackForUnion(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
const EnumDef &enum_def = *field.value.type.enum_def;
|
||||
auto union_type = namer_.Namespace(enum_def.name);
|
||||
auto union_type = namer_.Type(enum_def);
|
||||
|
||||
if (parser_.opts.include_dependence_headers) {
|
||||
union_type = GenPackageReference(enum_def) + "." + union_type;
|
||||
union_type = namer_.NamespacedType(enum_def) + "." + union_type;
|
||||
}
|
||||
code += GenIndents(2) + "self." + field_field + " = " + union_type +
|
||||
"Creator(" + "self." + field_field + "Type, " + struct_var + "." +
|
||||
@@ -1126,9 +1114,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
|
||||
code += GenIndents(2) + "if not " + struct_var + "." + field_method +
|
||||
"IsNone():";
|
||||
@@ -1160,9 +1148,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr,
|
||||
int indents) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
|
||||
code += GenIndents(indents) + "self." + field_field + " = []";
|
||||
code += GenIndents(indents) + "for i in range(" + struct_var + "." +
|
||||
@@ -1175,9 +1163,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
const FieldDef &field,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
|
||||
code += GenIndents(2) + "if not " + struct_var + "." + field_method +
|
||||
"IsNone():";
|
||||
@@ -1200,9 +1188,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
void GenUnPackForScalar(const StructDef &struct_def, const FieldDef &field,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
|
||||
code += GenIndents(2) + "self." + field_field + " = " + struct_var + "." +
|
||||
field_method + "()";
|
||||
@@ -1248,7 +1236,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
|
||||
// Writes import statements and code into the generated file.
|
||||
auto &code_base = *code_ptr;
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
|
||||
GenReceiverForObjectAPI(struct_def, code_ptr);
|
||||
code_base += "_UnPack(self, " + struct_var + "):";
|
||||
@@ -1269,7 +1257,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
void GenPackForStruct(const StructDef &struct_def,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto struct_fn = namer_.Function(struct_def.name);
|
||||
const auto struct_fn = namer_.Function(struct_def);
|
||||
|
||||
GenReceiverForObjectAPI(struct_def, code_ptr);
|
||||
code += "Pack(self, builder):";
|
||||
@@ -1289,9 +1277,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code_prefix = *code_prefix_ptr;
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto struct_type = namer_.Type(struct_def.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
const auto field_method = namer_.Method(field);
|
||||
|
||||
// Creates the field.
|
||||
code_prefix += GenIndents(2) + "if self." + field_field + " is not None:";
|
||||
@@ -1332,9 +1320,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr,
|
||||
int indents) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_type = namer_.Type(struct_def.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
const auto vectortype = field.value.type.VectorType();
|
||||
|
||||
code += GenIndents(indents) + struct_type + "Start" + field_method +
|
||||
@@ -1368,9 +1356,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
auto &code_prefix = *code_prefix_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_type = namer_.Type(struct_def.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
|
||||
// Adds the field into the struct.
|
||||
code += GenIndents(2) + "if self." + field_field + " is not None:";
|
||||
@@ -1411,9 +1399,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code_prefix = *code_prefix_ptr;
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_type = namer_.Type(struct_def.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
|
||||
if (field.value.type.struct_def->fixed) {
|
||||
// Pure struct fields need to be created along with their parent
|
||||
@@ -1438,9 +1426,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code_prefix = *code_prefix_ptr;
|
||||
auto &code = *code_ptr;
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto struct_type = namer_.Type(struct_def.name);
|
||||
const auto field_field = namer_.Field(field);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
|
||||
// TODO(luwa): TypeT should be moved under the None check as well.
|
||||
code_prefix += GenIndents(2) + "if self." + field_field + " is not None:";
|
||||
@@ -1455,8 +1443,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
std::string *code_ptr) const {
|
||||
auto &code_base = *code_ptr;
|
||||
std::string code, code_prefix;
|
||||
const auto struct_var = namer_.Variable(struct_def.name);
|
||||
const auto struct_type = namer_.Type(struct_def.name);
|
||||
const auto struct_var = namer_.Variable(struct_def);
|
||||
const auto struct_type = namer_.Type(struct_def);
|
||||
|
||||
GenReceiverForObjectAPI(struct_def, code_ptr);
|
||||
code_base += "Pack(self, builder):";
|
||||
@@ -1466,8 +1454,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
|
||||
const auto field_method = namer_.Method(field.name);
|
||||
const auto field_field = namer_.Field(field.name);
|
||||
const auto field_method = namer_.Method(field);
|
||||
const auto field_field = namer_.Field(field);
|
||||
|
||||
switch (field.value.type.base_type) {
|
||||
case BASE_TYPE_STRUCT: {
|
||||
@@ -1555,9 +1543,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
void GenUnionCreatorForStruct(const EnumDef &enum_def, const EnumVal &ev,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto union_type = namer_.Type(enum_def.name);
|
||||
const auto variant = namer_.Variant(ev.name);
|
||||
auto field_type = namer_.ObjectType(GenTypeGet(ev.union_type));
|
||||
const auto union_type = namer_.Type(enum_def);
|
||||
const auto variant = namer_.Variant(ev);
|
||||
auto field_type = namer_.ObjectType(*ev.union_type.struct_def);
|
||||
|
||||
code += GenIndents(1) + "if unionType == " + union_type + "()." +
|
||||
variant + ":";
|
||||
@@ -1573,8 +1561,8 @@ class PythonGenerator : public BaseGenerator {
|
||||
void GenUnionCreatorForString(const EnumDef &enum_def, const EnumVal &ev,
|
||||
std::string *code_ptr) const {
|
||||
auto &code = *code_ptr;
|
||||
const auto union_type = namer_.Type(enum_def.name);
|
||||
const auto variant = namer_.Variant(ev.name);
|
||||
const auto union_type = namer_.Type(enum_def);
|
||||
const auto variant = namer_.Variant(ev);
|
||||
|
||||
code += GenIndents(1) + "if unionType == " + union_type + "()." +
|
||||
variant + ":";
|
||||
@@ -1588,7 +1576,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
if (enum_def.generated) return;
|
||||
|
||||
auto &code = *code_ptr;
|
||||
const auto enum_fn = namer_.Function(enum_def.name);
|
||||
const auto enum_fn = namer_.Function(enum_def);
|
||||
|
||||
code += "\n";
|
||||
code += "def " + enum_fn + "Creator(unionType, table):";
|
||||
@@ -1721,7 +1709,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
if (parser_.opts.one_file && !enumcode.empty()) {
|
||||
*one_file_code += enumcode + "\n\n";
|
||||
} else {
|
||||
if (!SaveType(namer_.File(enum_def.name, SkipFile::Suffix),
|
||||
if (!SaveType(namer_.File(enum_def, SkipFile::Suffix),
|
||||
*enum_def.defined_namespace, enumcode, false))
|
||||
return false;
|
||||
}
|
||||
@@ -1742,7 +1730,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
if (parser_.opts.one_file && !declcode.empty()) {
|
||||
*one_file_code += declcode + "\n\n";
|
||||
} else {
|
||||
if (!SaveType(namer_.File(struct_def.name, SkipFile::Suffix),
|
||||
if (!SaveType(namer_.File(struct_def, SkipFile::Suffix),
|
||||
*struct_def.defined_namespace, declcode, true))
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -380,9 +380,9 @@ class RustGenerator : public BaseGenerator {
|
||||
gen_symbol(symbol);
|
||||
|
||||
const std::string directories =
|
||||
namer_.Directories(symbol.defined_namespace->components);
|
||||
namer_.Directories(*symbol.defined_namespace);
|
||||
EnsureDirExists(directories);
|
||||
const std::string file_path = directories + namer_.File(symbol.name);
|
||||
const std::string file_path = directories + namer_.File(symbol);
|
||||
const bool save_success =
|
||||
SaveFile(file_path.c_str(), code_.ToString(), /*binary=*/false);
|
||||
if (!save_success) return false;
|
||||
@@ -526,8 +526,11 @@ class RustGenerator : public BaseGenerator {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string NamespacedNativeName(const Definition &def) {
|
||||
return WrapInNameSpace(def.defined_namespace, namer_.ObjectType(def.name));
|
||||
std::string NamespacedNativeName(const EnumDef &def) {
|
||||
return WrapInNameSpace(def.defined_namespace, namer_.ObjectType(def));
|
||||
}
|
||||
std::string NamespacedNativeName(const StructDef &def) {
|
||||
return WrapInNameSpace(def.defined_namespace, namer_.ObjectType(def));
|
||||
}
|
||||
|
||||
std::string WrapInNameSpace(const Definition &def) const {
|
||||
@@ -663,7 +666,7 @@ class RustGenerator : public BaseGenerator {
|
||||
|
||||
std::string GetEnumValue(const EnumDef &enum_def,
|
||||
const EnumVal &enum_val) const {
|
||||
return namer_.EnumVariant(enum_def.name, enum_val.name);
|
||||
return namer_.EnumVariant(enum_def, enum_val);
|
||||
}
|
||||
|
||||
// 1 suffix since old C++ can't figure out the overload.
|
||||
@@ -671,7 +674,7 @@ class RustGenerator : public BaseGenerator {
|
||||
std::function<void(const EnumVal &)> cb) {
|
||||
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
|
||||
const auto &ev = **it;
|
||||
code_.SetValue("VARIANT", namer_.Variant(ev.name));
|
||||
code_.SetValue("VARIANT", namer_.Variant(ev));
|
||||
code_.SetValue("VALUE", enum_def.ToString(ev));
|
||||
code_.IncrementIdentLevel();
|
||||
cb(ev);
|
||||
@@ -690,7 +693,7 @@ class RustGenerator : public BaseGenerator {
|
||||
// an enum match function,
|
||||
// and an enum array of values
|
||||
void GenEnum(const EnumDef &enum_def) {
|
||||
code_.SetValue("ENUM_TY", namer_.Type(enum_def.name));
|
||||
code_.SetValue("ENUM_TY", namer_.Type(enum_def));
|
||||
code_.SetValue("BASE_TYPE", GetEnumTypeForDecl(enum_def.underlying_type));
|
||||
code_.SetValue("ENUM_NAMESPACE", namer_.Namespace(enum_def.name));
|
||||
code_.SetValue("ENUM_CONSTANT", namer_.Constant(enum_def.name));
|
||||
@@ -743,7 +746,7 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += "pub const ENUM_VALUES_{{ENUM_CONSTANT}}: [{{ENUM_TY}}; " +
|
||||
num_fields + "] = [";
|
||||
ForAllEnumValues1(enum_def, [&](const EnumVal &ev) {
|
||||
code_ += namer_.EnumVariant(enum_def.name, ev.name) + ",";
|
||||
code_ += namer_.EnumVariant(enum_def, ev) + ",";
|
||||
});
|
||||
code_ += "];";
|
||||
code_ += "";
|
||||
@@ -872,7 +875,7 @@ class RustGenerator : public BaseGenerator {
|
||||
|
||||
if (enum_def.is_union) {
|
||||
// Generate typesafe offset(s) for unions
|
||||
code_.SetValue("UNION_TYPE", namer_.Type(enum_def.name));
|
||||
code_.SetValue("UNION_TYPE", namer_.Type(enum_def));
|
||||
code_ += "pub struct {{UNION_TYPE}}UnionTableOffset {}";
|
||||
code_ += "";
|
||||
if (parser_.opts.generate_object_based_api) { GenUnionObject(enum_def); }
|
||||
@@ -885,13 +888,12 @@ class RustGenerator : public BaseGenerator {
|
||||
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
|
||||
auto &enum_val = **it;
|
||||
if (enum_val.union_type.base_type == BASE_TYPE_NONE) continue;
|
||||
code_.SetValue("VARIANT_NAME", namer_.Variant(enum_val.name));
|
||||
code_.SetValue("VARIANT_NAME", namer_.Variant(enum_val));
|
||||
// For legacy reasons, enum variants are Keep case while enum native
|
||||
// variants are UpperCamel case.
|
||||
code_.SetValue(
|
||||
"NATIVE_VARIANT",
|
||||
ConvertCase(namer_.EscapeKeyword(enum_val.name), Case::kUpperCamel));
|
||||
code_.SetValue("U_ELEMENT_NAME", namer_.Method(enum_val.name));
|
||||
code_.SetValue("NATIVE_VARIANT",
|
||||
namer_.LegacyRustNativeVariant(enum_val));
|
||||
code_.SetValue("U_ELEMENT_NAME", namer_.Method(enum_val));
|
||||
code_.SetValue("U_ELEMENT_TABLE_TYPE",
|
||||
NamespacedNativeName(*enum_val.union_type.struct_def));
|
||||
code_.IncrementIdentLevel();
|
||||
@@ -900,9 +902,9 @@ class RustGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
void GenUnionObject(const EnumDef &enum_def) {
|
||||
code_.SetValue("ENUM_TY", namer_.Type(enum_def.name));
|
||||
code_.SetValue("ENUM_FN", namer_.Function(enum_def.name));
|
||||
code_.SetValue("ENUM_OTY", namer_.ObjectType(enum_def.name));
|
||||
code_.SetValue("ENUM_TY", namer_.Type(enum_def));
|
||||
code_.SetValue("ENUM_FN", namer_.Function(enum_def));
|
||||
code_.SetValue("ENUM_OTY", namer_.ObjectType(enum_def));
|
||||
|
||||
// Generate native union.
|
||||
code_ += "#[allow(clippy::upper_case_acronyms)]"; // NONE's spelling is
|
||||
@@ -997,13 +999,6 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += "}"; // End union methods impl.
|
||||
}
|
||||
|
||||
std::string GetFieldOffsetName(const FieldDef &field) {
|
||||
// FIXME: VT_FIELD_NAME is not screaming snake case by legacy mistake.
|
||||
// but changing this is probably a breaking change.
|
||||
return "VT_" +
|
||||
ConvertCase(namer_.EscapeKeyword(field.name), Case::kAllUpper);
|
||||
}
|
||||
|
||||
enum DefaultContext { kBuilder, kAccessor, kObject };
|
||||
std::string GetDefaultValue(const FieldDef &field,
|
||||
const DefaultContext context) {
|
||||
@@ -1033,7 +1028,7 @@ class RustGenerator : public BaseGenerator {
|
||||
if (!ev) return "Default::default()"; // Bitflags enum.
|
||||
return WrapInNameSpace(
|
||||
field.value.type.enum_def->defined_namespace,
|
||||
namer_.EnumVariant(field.value.type.enum_def->name, ev->name));
|
||||
namer_.EnumVariant(*field.value.type.enum_def, *ev));
|
||||
}
|
||||
case ftUnionValue: {
|
||||
return ObjectFieldType(field, true) + "::NONE";
|
||||
@@ -1539,7 +1534,7 @@ class RustGenerator : public BaseGenerator {
|
||||
|
||||
std::string GenTableAccessorFuncBody(const FieldDef &field,
|
||||
const std::string &lifetime) {
|
||||
const std::string vt_offset = GetFieldOffsetName(field);
|
||||
const std::string vt_offset = namer_.LegacyRustFieldOffsetName(field);
|
||||
const std::string typname = FollowType(field.value.type, lifetime);
|
||||
// Default-y fields (scalars so far) are neither optional nor required.
|
||||
const std::string default_value =
|
||||
@@ -1582,9 +1577,9 @@ class RustGenerator : public BaseGenerator {
|
||||
const EnumVal &ev = **it;
|
||||
// TODO(cneo): Can variants be deprecated, should we skip them?
|
||||
if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; }
|
||||
code_.SetValue("U_ELEMENT_ENUM_TYPE",
|
||||
WrapInNameSpace(def.defined_namespace,
|
||||
namer_.EnumVariant(def.name, ev.name)));
|
||||
code_.SetValue(
|
||||
"U_ELEMENT_ENUM_TYPE",
|
||||
WrapInNameSpace(def.defined_namespace, namer_.EnumVariant(def, ev)));
|
||||
code_.SetValue(
|
||||
"U_ELEMENT_TABLE_TYPE",
|
||||
WrapInNameSpace(ev.union_type.struct_def->defined_namespace,
|
||||
@@ -1601,11 +1596,11 @@ class RustGenerator : public BaseGenerator {
|
||||
// diff when refactoring to the `ForAllX` helper functions.
|
||||
auto go = [&](const FieldDef &field) {
|
||||
if (field.deprecated) return;
|
||||
code_.SetValue("OFFSET_NAME", GetFieldOffsetName(field));
|
||||
code_.SetValue("OFFSET_NAME", namer_.LegacyRustFieldOffsetName(field));
|
||||
code_.SetValue("OFFSET_VALUE", NumToString(field.value.offset));
|
||||
code_.SetValue("FIELD", namer_.Field(field.name));
|
||||
code_.SetValue("FIELD", namer_.Field(field));
|
||||
code_.SetValue("BLDR_DEF_VAL", GetDefaultValue(field, kBuilder));
|
||||
code_.SetValue("DISCRIMINANT", namer_.Method(field.name) + "_type");
|
||||
code_.SetValue("DISCRIMINANT", namer_.Method(field) + "_type");
|
||||
code_.IncrementIdentLevel();
|
||||
cb(field);
|
||||
code_.DecrementIdentLevel();
|
||||
@@ -1620,8 +1615,8 @@ class RustGenerator : public BaseGenerator {
|
||||
// Generate an accessor struct, builder struct, and create function for a
|
||||
// table.
|
||||
void GenTable(const StructDef &struct_def) {
|
||||
code_.SetValue("STRUCT_TY", namer_.Type(struct_def.name));
|
||||
code_.SetValue("STRUCT_FN", namer_.Function(struct_def.name));
|
||||
code_.SetValue("STRUCT_TY", namer_.Type(struct_def));
|
||||
code_.SetValue("STRUCT_FN", namer_.Function(struct_def));
|
||||
|
||||
// Generate an offset type, the base type, the Follow impl, and the
|
||||
// init_from_table impl.
|
||||
@@ -1702,7 +1697,7 @@ class RustGenerator : public BaseGenerator {
|
||||
if (parser_.opts.generate_object_based_api) {
|
||||
// TODO(cneo): Replace more for loops with ForAllX stuff.
|
||||
// TODO(cneo): Manage indentation with IncrementIdentLevel?
|
||||
code_.SetValue("STRUCT_OTY", namer_.ObjectType(struct_def.name));
|
||||
code_.SetValue("STRUCT_OTY", namer_.ObjectType(struct_def));
|
||||
code_ += " pub fn unpack(&self) -> {{STRUCT_OTY}} {";
|
||||
ForAllObjectTableFields(struct_def, [&](const FieldDef &field) {
|
||||
const Type &type = field.value.type;
|
||||
@@ -1939,7 +1934,7 @@ class RustGenerator : public BaseGenerator {
|
||||
const EnumDef &union_def = *field.value.type.enum_def;
|
||||
code_.SetValue("UNION_TYPE", WrapInNameSpace(union_def));
|
||||
code_.SetValue("UNION_TYPE_OFFSET_NAME",
|
||||
GetFieldOffsetName(field) + "_TYPE");
|
||||
namer_.LegacyRustFieldOffsetName(field) + "_TYPE");
|
||||
code_ +=
|
||||
"\n .visit_union::<{{UNION_TYPE}}, _>("
|
||||
"\"{{FIELD}}_type\", Self::{{UNION_TYPE_OFFSET_NAME}}, "
|
||||
@@ -2010,12 +2005,12 @@ class RustGenerator : public BaseGenerator {
|
||||
if (type.base_type == BASE_TYPE_UNION) {
|
||||
const auto &enum_def = *type.enum_def;
|
||||
code_.SetValue("ENUM_TY", WrapInNameSpace(enum_def));
|
||||
code_.SetValue("FIELD", namer_.Field(field.name));
|
||||
code_.SetValue("FIELD", namer_.Field(field));
|
||||
|
||||
code_ += " match self.{{FIELD}}_type() {";
|
||||
code_ += " {{ENUM_TY}}::NONE => (),";
|
||||
ForAllUnionObjectVariantsBesidesNone(enum_def, [&] {
|
||||
code_.SetValue("FIELD", namer_.Field(field.name));
|
||||
code_.SetValue("FIELD", namer_.Field(field));
|
||||
code_ += " {{ENUM_TY}}::{{VARIANT_NAME}} => {";
|
||||
code_ +=
|
||||
" let f = "
|
||||
@@ -2065,7 +2060,7 @@ class RustGenerator : public BaseGenerator {
|
||||
code_ += "impl<'a: 'b, 'b> {{STRUCT_TY}}Builder<'a, 'b> {";
|
||||
ForAllTableFields(struct_def, [&](const FieldDef &field) {
|
||||
const bool is_scalar = IsScalar(field.value.type.base_type);
|
||||
std::string offset = GetFieldOffsetName(field);
|
||||
std::string offset = namer_.LegacyRustFieldOffsetName(field);
|
||||
// Generate functions to add data, which take one of two forms.
|
||||
//
|
||||
// If a value has a default:
|
||||
@@ -2077,8 +2072,7 @@ class RustGenerator : public BaseGenerator {
|
||||
// fn add_x(x_: type) {
|
||||
// fbb_.push_slot_always::<type>(offset, x_);
|
||||
// }
|
||||
code_.SetValue("FIELD_OFFSET",
|
||||
namer_.Type(struct_def.name) + "::" + offset);
|
||||
code_.SetValue("FIELD_OFFSET", namer_.Type(struct_def) + "::" + offset);
|
||||
code_.SetValue("FIELD_TYPE", TableBuilderArgsAddFuncType(field, "'b "));
|
||||
code_.SetValue("FUNC_BODY", TableBuilderArgsAddFuncBody(field));
|
||||
code_ += "#[inline]";
|
||||
@@ -2170,8 +2164,8 @@ class RustGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
void GenTableObject(const StructDef &table) {
|
||||
code_.SetValue("STRUCT_OTY", namer_.ObjectType(table.name));
|
||||
code_.SetValue("STRUCT_TY", namer_.Type(table.name));
|
||||
code_.SetValue("STRUCT_OTY", namer_.ObjectType(table));
|
||||
code_.SetValue("STRUCT_TY", namer_.Type(table));
|
||||
|
||||
// Generate the native object.
|
||||
code_ += "#[non_exhaustive]";
|
||||
@@ -2221,7 +2215,7 @@ class RustGenerator : public BaseGenerator {
|
||||
case ftUnionKey: return; // Generate union type with union value.
|
||||
case ftUnionValue: {
|
||||
code_.SetValue("ENUM_METHOD",
|
||||
namer_.Method(field.value.type.enum_def->name));
|
||||
namer_.Method(*field.value.type.enum_def));
|
||||
code_ +=
|
||||
" let {{FIELD}}_type = "
|
||||
"self.{{FIELD}}.{{ENUM_METHOD}}_type();";
|
||||
@@ -2309,7 +2303,7 @@ class RustGenerator : public BaseGenerator {
|
||||
for (auto it = v.begin(); it != v.end(); it++) {
|
||||
const FieldDef &field = **it;
|
||||
if (field.deprecated) continue;
|
||||
code_.SetValue("FIELD", namer_.Field(field.name));
|
||||
code_.SetValue("FIELD", namer_.Field(field));
|
||||
code_.SetValue("FIELD_OTY", ObjectFieldType(field, true));
|
||||
code_.IncrementIdentLevel();
|
||||
cb(field);
|
||||
@@ -2359,8 +2353,8 @@ class RustGenerator : public BaseGenerator {
|
||||
// must only be called if the root table is defined.
|
||||
void GenRootTableFuncs(const StructDef &struct_def) {
|
||||
FLATBUFFERS_ASSERT(parser_.root_struct_def_ && "root table not defined");
|
||||
code_.SetValue("STRUCT_TY", namer_.Type(struct_def.name));
|
||||
code_.SetValue("STRUCT_FN", namer_.Function(struct_def.name));
|
||||
code_.SetValue("STRUCT_TY", namer_.Type(struct_def));
|
||||
code_.SetValue("STRUCT_FN", namer_.Function(struct_def));
|
||||
code_.SetValue("STRUCT_CONST", namer_.Constant(struct_def.name));
|
||||
|
||||
// The root datatype accessors:
|
||||
@@ -2570,7 +2564,7 @@ class RustGenerator : public BaseGenerator {
|
||||
const auto &field = **it;
|
||||
code_.SetValue("FIELD_TYPE", GetTypeGet(field.value.type));
|
||||
code_.SetValue("FIELD_OTY", ObjectFieldType(field, false));
|
||||
code_.SetValue("FIELD", namer_.Field(field.name));
|
||||
code_.SetValue("FIELD", namer_.Field(field));
|
||||
code_.SetValue("FIELD_OFFSET", NumToString(offset_to_field));
|
||||
code_.SetValue(
|
||||
"REF",
|
||||
@@ -2589,7 +2583,7 @@ class RustGenerator : public BaseGenerator {
|
||||
// platforms.
|
||||
GenComment(struct_def.doc_comment);
|
||||
code_.SetValue("ALIGN", NumToString(struct_def.minalign));
|
||||
code_.SetValue("STRUCT_TY", namer_.Type(struct_def.name));
|
||||
code_.SetValue("STRUCT_TY", namer_.Type(struct_def));
|
||||
code_.SetValue("STRUCT_SIZE", NumToString(struct_def.bytesize));
|
||||
|
||||
// We represent Flatbuffers-structs in Rust-u8-arrays since the data may be
|
||||
@@ -2819,7 +2813,7 @@ class RustGenerator : public BaseGenerator {
|
||||
|
||||
// Generate Object API unpack method.
|
||||
if (parser_.opts.generate_object_based_api) {
|
||||
code_.SetValue("STRUCT_OTY", namer_.ObjectType(struct_def.name));
|
||||
code_.SetValue("STRUCT_OTY", namer_.ObjectType(struct_def));
|
||||
code_ += " pub fn unpack(&self) -> {{STRUCT_OTY}} {";
|
||||
code_ += " {{STRUCT_OTY}} {";
|
||||
ForAllStructFields(struct_def, [&](const FieldDef &field) {
|
||||
|
||||
86
src/namer.h
86
src/namer.h
@@ -112,8 +112,14 @@ class Namer {
|
||||
Namer(Config config, std::set<std::string> keywords)
|
||||
: config_(config), keywords_(std::move(keywords)) {}
|
||||
|
||||
std::string Type(const std::string &s) const {
|
||||
return Format(s, config_.types);
|
||||
// Types are always structs or enums so we can only expose these two
|
||||
// overloads.
|
||||
std::string Type(const StructDef &d) const { return Type(d.name); }
|
||||
|
||||
std::string Type(const EnumDef &d) const { return Type(d.name); }
|
||||
|
||||
template<typename T> std::string Method(const T &s) const {
|
||||
return Method(s.name);
|
||||
}
|
||||
|
||||
std::string Method(const std::string &s) const {
|
||||
@@ -128,25 +134,24 @@ class Namer {
|
||||
return Format(s, config_.functions);
|
||||
}
|
||||
|
||||
std::string Field(const std::string &s) const {
|
||||
return Format(s, config_.fields);
|
||||
}
|
||||
std::string Function(const Definition &s) const { return Function(s.name); }
|
||||
|
||||
std::string Variable(const std::string &s) const {
|
||||
return Format(s, config_.variables);
|
||||
}
|
||||
std::string Field(const FieldDef &s) const { return Field(s.name); }
|
||||
|
||||
std::string Variant(const std::string &s) const {
|
||||
return Format(s, config_.variants);
|
||||
}
|
||||
std::string Variable(const FieldDef &s) const { return Variable(s.name); }
|
||||
|
||||
std::string EnumVariant(const std::string &e, const std::string v) const {
|
||||
std::string Variable(const StructDef &s) const { return Variable(s.name); }
|
||||
|
||||
std::string Variant(const EnumVal &s) const { return Variant(s.name); }
|
||||
|
||||
std::string EnumVariant(const EnumDef &e, const EnumVal &v) const {
|
||||
return Type(e) + config_.enum_variant_seperator + Variant(v);
|
||||
}
|
||||
|
||||
std::string ObjectType(const std::string &s) const {
|
||||
return config_.object_prefix + Type(s) + config_.object_suffix;
|
||||
std::string ObjectType(const StructDef &d) const {
|
||||
return ObjectType(d.name);
|
||||
}
|
||||
std::string ObjectType(const EnumDef &d) const { return ObjectType(d.name); }
|
||||
|
||||
std::string Namespace(const std::string &s) const {
|
||||
return Format(s, config_.namespaces);
|
||||
@@ -161,6 +166,17 @@ class Namer {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string Namespace(const struct Namespace &ns) const {
|
||||
return Namespace(ns.components);
|
||||
}
|
||||
|
||||
std::string NamespacedType(const Definition &def) const {
|
||||
if (def.defined_namespace != nullptr) {
|
||||
return NamespacedType(def.defined_namespace->components, def.name);
|
||||
}
|
||||
return Type(def.name);
|
||||
}
|
||||
|
||||
std::string NamespacedType(const std::vector<std::string> &ns,
|
||||
const std::string &s) const {
|
||||
return Namespace(ns) + config_.namespace_seperator + Type(s);
|
||||
@@ -175,6 +191,11 @@ class Namer {
|
||||
(skip_suffix ? "" : config_.filename_suffix) +
|
||||
(skip_ext ? "" : config_.filename_extension);
|
||||
}
|
||||
template<typename T>
|
||||
std::string File(const T &f, SkipFile skips = SkipFile::None) const {
|
||||
return File(f.name, skips);
|
||||
}
|
||||
|
||||
// Formats `directories` prefixed with the output_path and joined with the
|
||||
// right seperator. Output path prefixing and the trailing separator may be
|
||||
// skiped using `skips`.
|
||||
@@ -194,6 +215,11 @@ class Namer {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string Directories(const struct Namespace &ns,
|
||||
SkipDir skips = SkipDir::None) const {
|
||||
return Directories(ns.components, skips);
|
||||
}
|
||||
|
||||
std::string EscapeKeyword(const std::string &name) const {
|
||||
if (keywords_.find(name) == keywords_.end()) {
|
||||
return name;
|
||||
@@ -202,7 +228,39 @@ class Namer {
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy fields do not really follow the usual config and should be
|
||||
// considered for deprecation.
|
||||
|
||||
std::string LegacyRustNativeVariant(const EnumVal &v) const {
|
||||
return ConvertCase(EscapeKeyword(v.name), Case::kUpperCamel);
|
||||
}
|
||||
|
||||
std::string LegacyRustFieldOffsetName(const FieldDef& field) const {
|
||||
|
||||
return "VT_" + ConvertCase(EscapeKeyword(field.name), Case::kAllUpper);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string Type(const std::string &s) const {
|
||||
return Format(s, config_.types);
|
||||
}
|
||||
|
||||
std::string ObjectType(const std::string &s) const {
|
||||
return config_.object_prefix + Type(s) + config_.object_suffix;
|
||||
}
|
||||
|
||||
std::string Field(const std::string &s) const {
|
||||
return Format(s, config_.fields);
|
||||
}
|
||||
|
||||
std::string Variable(const std::string &s) const {
|
||||
return Format(s, config_.variables);
|
||||
}
|
||||
|
||||
std::string Variant(const std::string &s) const {
|
||||
return Format(s, config_.variants);
|
||||
}
|
||||
|
||||
std::string Format(const std::string &s, Case casing) const {
|
||||
// NOTE: If you need to escape keywords after converting case, which would
|
||||
// make more sense than this, make it a config option.
|
||||
|
||||
Reference in New Issue
Block a user