From 4016c549d3379ccc2fb64b5a92b35ef51722f8a2 Mon Sep 17 00:00:00 2001 From: Casper Date: Mon, 7 Mar 2022 19:24:46 -0500 Subject: [PATCH] Apply Namer to Python code gen (#7146) * Refactor out a class from Rust Codegen * Convert GenerateRustModuleRootFile * git-clang-format * unused variable * parenthesis * update BUILD file * buildifier * Delete bfbs_gen_rust.h * Delete bfbs_gen_rust.cpp * Addressed some comments * Namer::EnumVariant * Remove do not submit; Add Namespace vector overload * Unshadow variable * removed redundant variables * Apply Namer to Python * Use more variables a bit * Apply const a bunch * More variables * Fix ObjectTypes * git clang format * small thing * Simplified code around nested flatbuffers * Make more methods const. * Python files are kKeep case * Address DO NOT SUBMIT in SaveType * ensure dir exists before saving files * fix space Co-authored-by: Casper Neo --- src/idl_gen_python.cpp | 876 ++++++++++++++++++++--------------------- src/idl_gen_rust.cpp | 1 + src/namer.h | 14 +- 3 files changed, 441 insertions(+), 450 deletions(-) diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index a085e8d8e..8426b6e09 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -26,10 +26,42 @@ #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" +#include "namer.h" namespace flatbuffers { namespace python { +std::set PythonKeywords() { + return { "False", "None", "True", "and", "as", "assert", + "break", "class", "continue", "def", "del", "elif", + "else", "except", "finally", "for", "from", "global", + "if", "import", "in", "is", "lambda", "nonlocal", + "not", "or", "pass", "raise", "return", "try", + "while", "with", "yield" }; +} + +Namer::Config PythonDefaultConfig() { + return { /*types=*/Case::kKeep, + /*constants=*/Case::kScreamingSnake, + /*methods=*/Case::kUpperCamel, + /*functions=*/Case::kUpperCamel, + /*fields=*/Case::kLowerCamel, + /*variable=*/Case::kLowerCamel, + /*variants=*/Case::kKeep, + /*enum_variant_seperator=*/".", + /*namespaces=*/Case::kKeep, // Packages in python. + /*namespace_seperator=*/".", + /*object_prefix=*/"", + /*object_suffix=*/"T", + /*keyword_prefix=*/"", + /*keyword_suffix=*/"_", + /*filenames=*/Case::kKeep, + /*directories=*/Case::kKeep, + /*output_path=*/"", + /*filename_suffix=*/"", + /*filename_extension=*/".py" }; +} + // Hardcode spaces per indentation. const CommentConfig def_comment = { nullptr, "#", nullptr }; const std::string Indent = " "; @@ -40,20 +72,13 @@ class PythonGenerator : public BaseGenerator { const std::string &file_name) : BaseGenerator(parser, path, file_name, "" /* not used */, "" /* not used */, "py"), - float_const_gen_("float('nan')", "float('inf')", "float('-inf')") { - static const char *const keywords[] = { - "False", "None", "True", "and", "as", "assert", "break", - "class", "continue", "def", "del", "elif", "else", "except", - "finally", "for", "from", "global", "if", "import", "in", - "is", "lambda", "nonlocal", "not", "or", "pass", "raise", - "return", "try", "while", "with", "yield" - }; - keywords_.insert(std::begin(keywords), std::end(keywords)); - } + float_const_gen_("float('nan')", "float('inf')", "float('-inf')"), + namer_({ PythonDefaultConfig().WithFlagOptions(parser.opts, path), + PythonKeywords() }) {} // Most field accessors need to retrieve and test the field offset first, // this is the prefix code for that. - std::string OffsetPrefix(const FieldDef &field) { + std::string OffsetPrefix(const FieldDef &field) const { return "\n" + Indent + Indent + "o = flatbuffers.number_types.UOffsetTFlags.py_type" + "(self._tab.Offset(" + NumToString(field.value.offset) + "))\n" + @@ -61,62 +86,39 @@ class PythonGenerator : public BaseGenerator { } // Begin a class declaration. - void BeginClass(const StructDef &struct_def, std::string *code_ptr) { + void BeginClass(const StructDef &struct_def, std::string *code_ptr) const { auto &code = *code_ptr; - code += "class " + NormalizedName(struct_def) + "(object):\n"; + code += "class " + namer_.Type(struct_def.name) + "(object):\n"; code += Indent + "__slots__ = ['_tab']"; code += "\n\n"; } // Begin enum code with a class declaration. - void BeginEnum(const std::string &class_name, std::string *code_ptr) { + void BeginEnum(const EnumDef &enum_def, std::string *code_ptr) const { auto &code = *code_ptr; - code += "class " + class_name + "(object):\n"; - } - - std::string EscapeKeyword(const std::string &name) const { - return keywords_.find(name) == keywords_.end() ? name : name + "_"; - } - - std::string NormalizedName(const Definition &definition) const { - return EscapeKeyword(definition.name); - } - - std::string NormalizedName(const EnumVal &ev) const { - return EscapeKeyword(ev.name); - } - - // Converts the name of a definition into upper Camel format. - std::string MakeUpperCamel(const Definition &definition) const { - return ConvertCase(NormalizedName(definition), Case::kUpperCamel); - } - - // Converts the name of a definition into lower Camel format. - std::string MakeLowerCamel(const Definition &definition) const { - auto name = ConvertCase(NormalizedName(definition), Case::kLowerCamel); - name[0] = CharToLower(name[0]); - return name; + code += "class " + namer_.Type(enum_def.name) + "(object):\n"; } // Starts a new line and then indents. - std::string GenIndents(int num) { + std::string GenIndents(int num) const { return "\n" + std::string(num * Indent.length(), ' '); } // A single enum member. void EnumMember(const EnumDef &enum_def, const EnumVal &ev, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; code += Indent; - code += NormalizedName(ev); + code += namer_.Variant(ev.name); code += " = "; code += enum_def.ToString(ev) + "\n"; } // Initialize a new struct or table from existing data. void NewRootTypeFromBuffer(const StructDef &struct_def, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; + const std::string struct_type = namer_.Type(struct_def.name); code += Indent + "@classmethod\n"; code += Indent + "def GetRootAs"; @@ -125,16 +127,14 @@ class PythonGenerator : public BaseGenerator { code += Indent + Indent; code += "n = flatbuffers.encode.Get"; code += "(flatbuffers.packer.uoffset, buf, offset)\n"; - code += Indent + Indent + "x = " + NormalizedName(struct_def) + "()\n"; + code += Indent + Indent + "x = " + struct_type + "()\n"; code += Indent + Indent + "x.Init(buf, n + offset)\n"; code += Indent + Indent + "return x\n"; code += "\n"; // Add an alias with the old name code += Indent + "@classmethod\n"; - code += Indent + "def GetRootAs"; - code += NormalizedName(struct_def); - code += "(cls, buf, offset=0):\n"; + code += Indent + "def GetRootAs" + struct_type + "(cls, buf, offset=0):\n"; code += Indent + Indent + "\"\"\"This method is deprecated. Please switch to GetRootAs.\"\"\"\n"; @@ -142,7 +142,8 @@ class PythonGenerator : public BaseGenerator { } // Initialize an existing object with other data, to avoid an allocation. - void InitializeExisting(const StructDef &struct_def, std::string *code_ptr) { + void InitializeExisting(const StructDef &struct_def, + std::string *code_ptr) const { auto &code = *code_ptr; GenReceiver(struct_def, code_ptr); @@ -153,12 +154,11 @@ class PythonGenerator : public BaseGenerator { // Get the length of a vector. void GetVectorLen(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; GenReceiver(struct_def, code_ptr); - code += - ConvertCase(NormalizedName(field), Case::kUpperCamel) + "Length(self"; + code += namer_.Method(field.name) + "Length(self"; code += "):" + OffsetPrefix(field); code += Indent + Indent + Indent + "return self._tab.VectorLen(o)\n"; code += Indent + Indent + "return 0\n\n"; @@ -166,12 +166,11 @@ class PythonGenerator : public BaseGenerator { // Determines whether a vector is none or not. void GetVectorIsNone(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; GenReceiver(struct_def, code_ptr); - code += - ConvertCase(NormalizedName(field), Case::kUpperCamel) + "IsNone(self"; + code += namer_.Method(field.name) + "IsNone(self"; code += "):"; code += GenIndents(2) + "o = flatbuffers.number_types.UOffsetTFlags.py_type" + @@ -182,11 +181,12 @@ class PythonGenerator : public BaseGenerator { // Get the value of a struct's scalar. void GetScalarFieldOfStruct(const StructDef &struct_def, - const FieldDef &field, std::string *code_ptr) { + const FieldDef &field, + std::string *code_ptr) const { auto &code = *code_ptr; std::string getter = GenGetter(field.value.type); GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += namer_.Method(field.name); code += "(self): return " + getter; code += "self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type("; code += NumToString(field.value.offset) + "))\n"; @@ -194,11 +194,11 @@ class PythonGenerator : public BaseGenerator { // Get the value of a table's scalar. void GetScalarFieldOfTable(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; std::string getter = GenGetter(field.value.type); GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += namer_.Method(field.name); code += "(self):"; code += OffsetPrefix(field); getter += "o + self._tab.Pos)"; @@ -219,10 +219,11 @@ class PythonGenerator : public BaseGenerator { // Get a struct by initializing an existing struct. // Specific to Struct. void GetStructFieldOfStruct(const StructDef &struct_def, - const FieldDef &field, std::string *code_ptr) { + const FieldDef &field, + std::string *code_ptr) const { auto &code = *code_ptr; GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += namer_.Method(field.name); code += "(self, obj):\n"; code += Indent + Indent + "obj.Init(self._tab.Bytes, self._tab.Pos + "; code += NumToString(field.value.offset) + ")"; @@ -231,11 +232,11 @@ class PythonGenerator : public BaseGenerator { // Get the value of a fixed size array. void GetArrayOfStruct(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; const auto vec_type = field.value.type.VectorType(); GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += namer_.Method(field.name); if (IsStruct(vec_type)) { code += "(self, obj, i):\n"; code += Indent + Indent + "obj.Init(self._tab.Bytes, self._tab.Pos + "; @@ -256,10 +257,10 @@ class PythonGenerator : public BaseGenerator { // Get a struct by initializing an existing struct. // Specific to Table. void GetStructFieldOfTable(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += namer_.Method(field.name); code += "(self):"; code += OffsetPrefix(field); if (field.value.type.struct_def->fixed) { @@ -281,10 +282,10 @@ class PythonGenerator : public BaseGenerator { // Get the value of a string. void GetStringField(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += namer_.Method(field.name); code += "(self):"; code += OffsetPrefix(field); code += Indent + Indent + Indent + "return " + GenGetter(field.value.type); @@ -294,10 +295,10 @@ class PythonGenerator : public BaseGenerator { // Get the value of a union from an object. void GetUnionField(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel) + "(self):"; + code += namer_.Method(field.name) + "(self):"; code += OffsetPrefix(field); // TODO(rw): this works and is not the good way to it: @@ -318,27 +319,33 @@ class PythonGenerator : public BaseGenerator { // Generate the package reference when importing a struct or enum from its // module. - std::string GenPackageReference(const Type &type) { - Namespace *namespaces; + std::string GenPackageReference(const Type &type) const { if (type.struct_def) { - namespaces = type.struct_def->defined_namespace; + return GenPackageReference(*type.struct_def); } else if (type.enum_def) { - namespaces = type.enum_def->defined_namespace; + return GenPackageReference(*type.enum_def); } else { return "." + GenTypeGet(type); } - - return namespaces->GetFullyQualifiedName(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, - const FieldDef &field, std::string *code_ptr) { + const FieldDef &field, + std::string *code_ptr) const { auto &code = *code_ptr; auto vectortype = field.value.type.VectorType(); GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += namer_.Method(field.name); code += "(self, j):" + OffsetPrefix(field); code += Indent + Indent + Indent + "x = self._tab.Vector(o)\n"; code += Indent + Indent + Indent; @@ -362,12 +369,12 @@ class PythonGenerator : public BaseGenerator { // argument to conveniently set the zero value for the result. void GetMemberOfVectorOfNonStruct(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; auto vectortype = field.value.type.VectorType(); GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += namer_.Method(field.name); code += "(self, j):"; code += OffsetPrefix(field); code += Indent + Indent + Indent + "a = self._tab.Vector(o)\n"; @@ -387,7 +394,7 @@ class PythonGenerator : public BaseGenerator { // than iterating over the vector element by element. void GetVectorOfNonStructAsNumpy(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; auto vectortype = field.value.type.VectorType(); @@ -396,14 +403,13 @@ class PythonGenerator : public BaseGenerator { if (!(IsScalar(vectortype.base_type))) { return; } GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel) + - "AsNumpy(self):"; + code += namer_.Method(field.name) + "AsNumpy(self):"; code += OffsetPrefix(field); code += Indent + Indent + Indent; code += "return "; code += "self._tab.GetVectorAsNumpy(flatbuffers.number_types."; - code += ConvertCase(GenTypeGet(field.value.type), Case::kUpperCamel); + code += namer_.Method(GenTypeGet(field.value.type)); code += "Flags, o)\n"; if (IsString(vectortype)) { @@ -414,28 +420,32 @@ class PythonGenerator : public BaseGenerator { code += "\n"; } - // Returns a nested flatbuffer as itself. - void GetVectorAsNestedFlatbuffer(const StructDef &struct_def, - const FieldDef &field, - std::string *code_ptr) { - auto nested = field.attributes.Lookup("nested_flatbuffer"); - if (!nested) { return; } // There is no nested flatbuffer. - - std::string unqualified_name = nested->constant; - std::string qualified_name = nested->constant; - auto nested_root = parser_.LookupStruct(nested->constant); + std::string NestedFlatbufferType(std::string unqualified_name) const { + StructDef* nested_root = parser_.LookupStruct(unqualified_name); + std::string qualified_name; if (nested_root == nullptr) { - qualified_name = - parser_.current_namespace_->GetFullyQualifiedName(nested->constant); + qualified_name = namer_.NamespacedType( + parser_.current_namespace_->components, unqualified_name); + // Double check qualified name just to be sure it exists. nested_root = parser_.LookupStruct(qualified_name); } FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser. - (void)nested_root; + return qualified_name; + } + + // Returns a nested flatbuffer as itself. + void GetVectorAsNestedFlatbuffer(const StructDef &struct_def, + const FieldDef &field, + std::string *code_ptr) const { + auto nested = field.attributes.Lookup("nested_flatbuffer"); + if (!nested) { return; } // There is no nested flatbuffer. + + const std::string unqualified_name = nested->constant; + const std::string qualified_name = NestedFlatbufferType(unqualified_name); auto &code = *code_ptr; GenReceiver(struct_def, code_ptr); - code += ConvertCase(NormalizedName(field), Case::kUpperCamel) + - "NestedRoot(self):"; + code += namer_.Method(field.name) + "NestedRoot(self):"; code += OffsetPrefix(field); @@ -449,11 +459,12 @@ class PythonGenerator : public BaseGenerator { } // Begin the creator function signature. - void BeginBuilderArgs(const StructDef &struct_def, std::string *code_ptr) { + void BeginBuilderArgs(const StructDef &struct_def, + std::string *code_ptr) const { auto &code = *code_ptr; code += "\n"; - code += "def Create" + NormalizedName(struct_def); + code += "def Create" + namer_.Type(struct_def.name); code += "(builder"; } @@ -463,7 +474,7 @@ class PythonGenerator : public BaseGenerator { const std::string nameprefix, const std::string namesuffix, bool has_field_name, const std::string fieldname_suffix, - std::string *code_ptr) { + std::string *code_ptr) const { for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; @@ -476,23 +487,21 @@ class PythonGenerator : public BaseGenerator { // a nested struct, prefix the name with the field name. auto subprefix = nameprefix; if (has_field_name) { - subprefix += MakeLowerCamel(field) + fieldname_suffix; + subprefix += namer_.Field(field.name) + 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 += ConvertCase(NormalizedName(field), Case::kLowerCamel); - } + if (has_field_name) { code += namer_.Field(field.name); } code += namesuffix; } } } // End the creator function signature. - void EndBuilderArgs(std::string *code_ptr) { + void EndBuilderArgs(std::string *code_ptr) const { auto &code = *code_ptr; code += "):\n"; } @@ -501,7 +510,7 @@ class PythonGenerator : public BaseGenerator { // padding. void StructBuilderBody(const StructDef &struct_def, const char *nameprefix, std::string *code_ptr, size_t index = 0, - bool in_array = false) { + bool in_array = false) const { auto &code = *code_ptr; std::string indent(index * 4, ' '); code += @@ -517,9 +526,10 @@ class PythonGenerator : public BaseGenerator { code += indent + " builder.Pad(" + NumToString(field.padding) + ")\n"; if (IsStruct(field_type)) { - StructBuilderBody(*field_type.struct_def, - (nameprefix + (MakeLowerCamel(field) + "_")).c_str(), - code_ptr, index, in_array); + StructBuilderBody( + *field_type.struct_def, + (nameprefix + (namer_.Field(field.name) + "_")).c_str(), code_ptr, + index, in_array); } else { const auto index_var = "_idx" + NumToString(index); if (IsArray(field_type)) { @@ -531,13 +541,12 @@ class PythonGenerator : public BaseGenerator { if (IsStruct(type)) { StructBuilderBody( *field_type.struct_def, - (nameprefix + (MakeLowerCamel(field) + "_")).c_str(), code_ptr, + (nameprefix + (namer_.Field(field.name) + "_")).c_str(), code_ptr, index + 1, in_array); } else { code += IsArray(field_type) ? " " : ""; code += indent + " builder.Prepend" + GenMethod(field) + "("; - code += nameprefix + - ConvertCase(NormalizedName(field), Case::kLowerCamel); + code += nameprefix + namer_.Variable(field.name); 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]"; @@ -548,17 +557,18 @@ class PythonGenerator : public BaseGenerator { } } - void EndBuilderBody(std::string *code_ptr) { + void EndBuilderBody(std::string *code_ptr) const { auto &code = *code_ptr; code += " return builder.Offset()\n"; } // Get the value of a table's starting offset. - void GetStartOfTable(const StructDef &struct_def, std::string *code_ptr) { + void GetStartOfTable(const StructDef &struct_def, + std::string *code_ptr) const { auto &code = *code_ptr; - + const auto struct_type = namer_.Type(struct_def.name); // Generate method with struct name. - code += "def " + NormalizedName(struct_def) + "Start(builder): "; + code += "def " + struct_type + "Start(builder): "; code += "builder.StartObject("; code += NumToString(struct_def.fields.vec.size()); code += ")\n"; @@ -566,31 +576,30 @@ class PythonGenerator : public BaseGenerator { if (!parser_.opts.one_file) { // Generate method without struct name. code += "def Start(builder):\n"; - code += - Indent + "return " + NormalizedName(struct_def) + "Start(builder)\n"; + code += Indent + "return " + struct_type + "Start(builder)\n"; } } // Set the value of a table's field. void BuildFieldOfTable(const StructDef &struct_def, const FieldDef &field, - const size_t offset, std::string *code_ptr) { + 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); // Generate method with struct name. - code += "def " + NormalizedName(struct_def) + "Add" + - ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += "def " + namer_.Type(struct_def.name) + "Add" + field_method; code += "(builder, "; - code += ConvertCase(NormalizedName(field), Case::kLowerCamel); + code += field_var; code += "): "; code += "builder.Prepend"; code += GenMethod(field) + "Slot("; code += NumToString(offset) + ", "; if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) { code += "flatbuffers.number_types.UOffsetTFlags.py_type"; - code += "("; - code += ConvertCase(NormalizedName(field), Case::kLowerCamel) + ")"; + code += "(" + field_var + ")"; } else { - code += ConvertCase(NormalizedName(field), Case::kLowerCamel); + code += field_var; } code += ", "; code += IsFloat(field.value.type.base_type) @@ -600,26 +609,24 @@ class PythonGenerator : public BaseGenerator { if (!parser_.opts.one_file) { // Generate method without struct name. - code += "def Add" + ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += "def Add" + field_method + "(builder, " + field_var + "):\n"; + code += Indent + "return " + namer_.Type(struct_def.name) + "Add" + + field_method; code += "(builder, "; - code += ConvertCase(NormalizedName(field), Case::kLowerCamel); - code += "):\n"; - code += Indent + "return " + NormalizedName(struct_def) + "Add" + - ConvertCase(NormalizedName(field), Case::kUpperCamel); - code += "(builder, "; - code += ConvertCase(NormalizedName(field), Case::kLowerCamel); + code += field_var; code += ")\n"; } } // Set the value of one of the members of a table's vector. void BuildVectorOfTable(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + 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); // Generate method with struct name. - code += "def " + NormalizedName(struct_def) + "Start"; - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += "def " + struct_type + "Start" + field_method; code += "Vector(builder, numElems): return builder.StartVector("; auto vector_type = field.value.type.VectorType(); auto alignment = InlineAlignment(vector_type); @@ -630,12 +637,9 @@ class PythonGenerator : public BaseGenerator { if (!parser_.opts.one_file) { // Generate method without struct name. - code += "def Start"; - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); - code += "Vector(builder, numElems):\n"; - code += Indent + "return " + NormalizedName(struct_def) + "Start"; - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); - code += "Vector(builder, numElems)\n"; + code += "def Start" + field_method + "Vector(builder, numElems):\n"; + code += Indent + "return " + struct_type + "Start"; + code += field_method + "Vector(builder, numElems)\n"; } } @@ -644,26 +648,16 @@ class PythonGenerator : public BaseGenerator { // flatbuffers. void BuildVectorOfTableFromBytes(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto nested = field.attributes.Lookup("nested_flatbuffer"); if (!nested) { return; } // There is no nested flatbuffer. - std::string unqualified_name = nested->constant; - std::string qualified_name = nested->constant; - auto nested_root = parser_.LookupStruct(nested->constant); - if (nested_root == nullptr) { - qualified_name = - parser_.current_namespace_->GetFullyQualifiedName(nested->constant); - nested_root = parser_.LookupStruct(qualified_name); - } - FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser. - (void)nested_root; - auto &code = *code_ptr; + const std::string field_method = namer_.Method(field.name); + const std::string struct_type = namer_.Type(struct_def.name); // Generate method with struct and field name. - code += "def " + NormalizedName(struct_def) + "Make"; - code += ConvertCase(NormalizedName(field), Case::kUpperCamel); + code += "def " + struct_type + "Make" + field_method; code += "VectorFromBytes(builder, bytes):\n"; code += Indent + "builder.StartVector("; auto vector_type = field.value.type.VectorType(); @@ -679,41 +673,40 @@ class PythonGenerator : public BaseGenerator { if (!parser_.opts.one_file) { // Generate method without struct and field name. - code += "def Make" + - ConvertCase(NormalizedName(field), Case::kUpperCamel) + - "VectorFromBytes(builder, bytes):\n"; - code += Indent + "return " + NormalizedName(struct_def) + "Make" + - ConvertCase(NormalizedName(field), Case::kUpperCamel) + + code += "def Make" + field_method + "VectorFromBytes(builder, bytes):\n"; + code += Indent + "return " + struct_type + "Make" + field_method + "VectorFromBytes(builder, bytes)\n"; } } // Get the offset of the end of a table. - void GetEndOffsetOnTable(const StructDef &struct_def, std::string *code_ptr) { + void GetEndOffsetOnTable(const StructDef &struct_def, + std::string *code_ptr) const { auto &code = *code_ptr; // Generate method with struct name. - code += "def " + NormalizedName(struct_def) + "End"; + code += "def " + namer_.Type(struct_def.name) + "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 " + NormalizedName(struct_def) + "End(builder)"; + code += + Indent + "return " + namer_.Type(struct_def.name) + "End(builder)"; } } // Generate the receiver for function signatures. - void GenReceiver(const StructDef &struct_def, std::string *code_ptr) { + void GenReceiver(const StructDef &struct_def, std::string *code_ptr) const { auto &code = *code_ptr; - code += Indent + "# " + NormalizedName(struct_def) + "\n"; + code += Indent + "# " + namer_.Type(struct_def.name) + "\n"; code += Indent + "def "; } // Generate a struct field, conditioned on its child type(s). void GenStructAccessor(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { GenComment(field.doc_comment, code_ptr, &def_comment, Indent.c_str()); if (IsScalar(field.value.type.base_type)) { if (struct_def.fixed) { @@ -757,7 +750,8 @@ class PythonGenerator : public BaseGenerator { } // Generate struct sizeof. - void GenStructSizeOf(const StructDef &struct_def, std::string *code_ptr) { + void GenStructSizeOf(const StructDef &struct_def, + std::string *code_ptr) const { auto &code = *code_ptr; code += Indent + "@classmethod\n"; code += Indent + "def SizeOf(cls):\n"; @@ -767,7 +761,8 @@ class PythonGenerator : public BaseGenerator { } // Generate table constructors, conditioned on its members' types. - void GenTableBuilders(const StructDef &struct_def, std::string *code_ptr) { + void GenTableBuilders(const StructDef &struct_def, + std::string *code_ptr) const { GetStartOfTable(struct_def, code_ptr); for (auto it = struct_def.fields.vec.begin(); @@ -788,7 +783,7 @@ class PythonGenerator : public BaseGenerator { // Generate function to check for proper file identifier void GenHasFileIdentifier(const StructDef &struct_def, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; std::string escapedID; // In the event any of file_identifier characters are special(NULL, \, etc), @@ -800,7 +795,7 @@ class PythonGenerator : public BaseGenerator { } code += Indent + "@classmethod\n"; - code += Indent + "def " + NormalizedName(struct_def); + code += Indent + "def " + namer_.Type(struct_def.name); code += "BufferHasIdentifier(cls, buf, offset, size_prefixed=False):"; code += "\n"; code += Indent + Indent; @@ -811,7 +806,7 @@ class PythonGenerator : public BaseGenerator { } // Generates struct or table methods. - void GenStruct(const StructDef &struct_def, std::string *code_ptr) { + void GenStruct(const StructDef &struct_def, std::string *code_ptr) const { if (struct_def.generated) return; GenComment(struct_def.doc_comment, code_ptr, &def_comment); @@ -849,23 +844,24 @@ class PythonGenerator : public BaseGenerator { } void GenReceiverForObjectAPI(const StructDef &struct_def, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; - code += GenIndents(1) + "# " + NormalizedName(struct_def) + "T"; + code += GenIndents(1) + "# " + namer_.ObjectType(struct_def.name); code += GenIndents(1) + "def "; } void BeginClassForObjectAPI(const StructDef &struct_def, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; code += "\n"; - code += "class " + NormalizedName(struct_def) + "T(object):"; + code += "class " + namer_.ObjectType(struct_def.name) + "(object):"; code += "\n"; } // Gets the accoresponding python builtin type of a BaseType for scalars and // string. - std::string GetBasePythonTypeForScalarAndString(const BaseType &base_type) { + std::string GetBasePythonTypeForScalarAndString( + const BaseType &base_type) const { if (IsBool(base_type)) { return "bool"; } else if (IsFloat(base_type)) { @@ -880,7 +876,7 @@ class PythonGenerator : public BaseGenerator { } } - std::string GetDefaultValue(const FieldDef &field) { + std::string GetDefaultValue(const FieldDef &field) const { BaseType base_type = field.value.type.base_type; if (IsBool(base_type)) { return field.value.constant == "0" ? "False" : "True"; @@ -896,7 +892,7 @@ class PythonGenerator : public BaseGenerator { void GenUnionInit(const FieldDef &field, std::string *field_types_ptr, std::set *import_list, - std::set *import_typing_list) { + std::set *import_typing_list) const { // Gets all possible types in the union. import_typing_list->insert("Union"); auto &field_types = *field_types_ptr; @@ -911,7 +907,7 @@ class PythonGenerator : public BaseGenerator { std::string field_type; switch (ev.union_type.base_type) { case BASE_TYPE_STRUCT: - field_type = GenTypeGet(ev.union_type) + "T"; + field_type = namer_.ObjectType(ev.union_type.struct_def->name); if (parser_.opts.include_dependence_headers) { auto package_reference = GenPackageReference(ev.union_type); field_type = package_reference + "." + field_type; @@ -931,45 +927,43 @@ class PythonGenerator : public BaseGenerator { // Gets the import lists for the union. if (parser_.opts.include_dependence_headers) { - // The package reference is generated based on enum_def, instead - // of struct_def in field.type. That's why GenPackageReference() is - // not used. - Namespace *namespaces = field.value.type.enum_def->defined_namespace; - auto package_reference = namespaces->GetFullyQualifiedName( - MakeUpperCamel(*(field.value.type.enum_def))); - auto union_name = MakeUpperCamel(*(field.value.type.enum_def)); + const auto package_reference = + GenPackageReference(*field.value.type.enum_def); import_list->insert("import " + package_reference); } } - void GenStructInit(const FieldDef &field, std::string *field_type_ptr, + void GenStructInit(const FieldDef &field, std::string *out_ptr, std::set *import_list, - std::set *import_typing_list) { + std::set *import_typing_list) const { import_typing_list->insert("Optional"); - auto &field_type = *field_type_ptr; + auto &output = *out_ptr; + const Type &type = field.value.type; + const std::string object_type = namer_.ObjectType(type.struct_def->name); if (parser_.opts.include_dependence_headers) { - auto package_reference = GenPackageReference(field.value.type); - field_type = package_reference + "." + TypeName(field) + "T]"; + auto package_reference = GenPackageReference(type); + output = package_reference + "." + object_type + "]"; import_list->insert("import " + package_reference); } else { - field_type = TypeName(field) + "T]"; + output = object_type + "]"; } - field_type = "Optional[" + field_type; + output = "Optional[" + output; } void GenVectorInit(const FieldDef &field, std::string *field_type_ptr, std::set *import_list, - std::set *import_typing_list) { + std::set *import_typing_list) const { import_typing_list->insert("List"); auto &field_type = *field_type_ptr; - auto base_type = field.value.type.VectorType().base_type; + const Type &vector_type = field.value.type.VectorType(); + const BaseType base_type = vector_type.base_type; if (base_type == BASE_TYPE_STRUCT) { - field_type = GenTypeGet(field.value.type.VectorType()) + "T]"; + const std::string object_type = + namer_.ObjectType(GenTypeGet(vector_type)); + field_type = object_type + "]"; if (parser_.opts.include_dependence_headers) { - auto package_reference = - GenPackageReference(field.value.type.VectorType()); - field_type = package_reference + "." + - GenTypeGet(field.value.type.VectorType()) + "T]"; + auto package_reference = GenPackageReference(vector_type); + field_type = package_reference + "." + object_type + "]"; import_list->insert("import " + package_reference); } field_type = "List[" + field_type; @@ -980,7 +974,7 @@ class PythonGenerator : public BaseGenerator { } void GenInitialize(const StructDef &struct_def, std::string *code_ptr, - std::set *import_list) { + std::set *import_list) const { std::string code; std::set import_typing_list; for (auto it = struct_def.fields.vec.begin(); @@ -1011,11 +1005,11 @@ class PythonGenerator : public BaseGenerator { break; } - auto default_value = GetDefaultValue(field); + const auto default_value = GetDefaultValue(field); // Wrties the init statement. - auto field_instance_name = MakeLowerCamel(field); - code += GenIndents(2) + "self." + field_instance_name + " = " + - default_value + " # type: " + field_type; + const auto field_field = namer_.Field(field.name); + code += GenIndents(2) + "self." + field_field + " = " + default_value + + " # type: " + field_type; } // Writes __init__ method. @@ -1051,46 +1045,44 @@ class PythonGenerator : public BaseGenerator { } // Removes the import of the struct itself, if applied. - auto package_reference = - struct_def.defined_namespace->GetFullyQualifiedName( - MakeUpperCamel(struct_def)); - auto struct_import = "import " + package_reference; + auto struct_import = "import " + GenPackageReference(struct_def); import_list->erase(struct_import); } - void InitializeFromBuf(const StructDef &struct_def, std::string *code_ptr) { + void InitializeFromBuf(const StructDef &struct_def, + std::string *code_ptr) const { auto &code = *code_ptr; - auto instance_name = MakeLowerCamel(struct_def); - auto struct_name = NormalizedName(struct_def); + const auto struct_var = namer_.Variable(struct_def.name); + const auto struct_type = namer_.Type(struct_def.name); code += GenIndents(1) + "@classmethod"; code += GenIndents(1) + "def InitFromBuf(cls, buf, pos):"; - code += GenIndents(2) + instance_name + " = " + struct_name + "()"; - code += GenIndents(2) + instance_name + ".Init(buf, pos)"; - code += GenIndents(2) + "return cls.InitFromObj(" + instance_name + ")"; + code += GenIndents(2) + struct_var + " = " + struct_type + "()"; + code += GenIndents(2) + struct_var + ".Init(buf, pos)"; + code += GenIndents(2) + "return cls.InitFromObj(" + struct_var + ")"; code += "\n"; } void InitializeFromObjForObject(const StructDef &struct_def, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; - auto instance_name = MakeLowerCamel(struct_def); - auto struct_name = NormalizedName(struct_def); + const auto struct_var = namer_.Variable(struct_def.name); + const auto struct_object = namer_.ObjectType(struct_def.name); code += GenIndents(1) + "@classmethod"; - code += GenIndents(1) + "def InitFromObj(cls, " + instance_name + "):"; - code += GenIndents(2) + "x = " + struct_name + "T()"; - code += GenIndents(2) + "x._UnPack(" + instance_name + ")"; + code += GenIndents(1) + "def InitFromObj(cls, " + struct_var + "):"; + code += GenIndents(2) + "x = " + struct_object + "()"; + code += GenIndents(2) + "x._UnPack(" + struct_var + ")"; code += GenIndents(2) + "return x"; code += "\n"; } void GenUnPackForStruct(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; - auto struct_instance_name = MakeLowerCamel(struct_def); - auto field_instance_name = MakeLowerCamel(field); - auto field_accessor_name = MakeUpperCamel(field); + 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); auto field_type = TypeName(field); if (parser_.opts.include_dependence_headers) { @@ -1098,16 +1090,14 @@ class PythonGenerator : public BaseGenerator { field_type = package_reference + "." + TypeName(field); } - code += GenIndents(2) + "if " + struct_instance_name + "." + - field_accessor_name + "("; + code += GenIndents(2) + "if " + struct_var + "." + field_method + "("; // if field is a struct, we need to create an instance for it first. if (struct_def.fixed && field.value.type.base_type == BASE_TYPE_STRUCT) { code += field_type + "()"; } code += ") is not None:"; - code += GenIndents(3) + "self." + field_instance_name + " = " + field_type + - "T.InitFromObj(" + struct_instance_name + "." + - field_accessor_name + "("; + code += GenIndents(3) + "self." + field_field + " = " + field_type + + "T.InitFromObj(" + struct_var + "." + field_method + "("; // A struct's accessor requires a struct buf instance. if (struct_def.fixed && field.value.type.base_type == BASE_TYPE_STRUCT) { code += field_type + "()"; @@ -1116,82 +1106,81 @@ class PythonGenerator : public BaseGenerator { } void GenUnPackForUnion(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; - auto field_instance_name = MakeLowerCamel(field); - auto field_accessor_name = MakeUpperCamel(field); - auto struct_instance_name = MakeLowerCamel(struct_def); - auto union_name = MakeUpperCamel(*(field.value.type.enum_def)); + 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 EnumDef &enum_def = *field.value.type.enum_def; + auto union_type = namer_.Namespace(enum_def.name); if (parser_.opts.include_dependence_headers) { - Namespace *namespaces = field.value.type.enum_def->defined_namespace; - auto package_reference = namespaces->GetFullyQualifiedName( - MakeUpperCamel(*(field.value.type.enum_def))); - union_name = package_reference + "." + union_name; + union_type = GenPackageReference(enum_def) + "." + union_type; } - code += GenIndents(2) + "self." + field_instance_name + " = " + union_name + - "Creator(" + "self." + field_instance_name + "Type, " + - struct_instance_name + "." + field_accessor_name + "())"; + code += GenIndents(2) + "self." + field_field + " = " + union_type + + "Creator(" + "self." + field_field + "Type, " + struct_var + "." + + field_method + "())"; } void GenUnPackForStructVector(const StructDef &struct_def, - const FieldDef &field, std::string *code_ptr) { + const FieldDef &field, + std::string *code_ptr) const { auto &code = *code_ptr; - auto field_instance_name = MakeLowerCamel(field); - auto field_accessor_name = MakeUpperCamel(field); - auto struct_instance_name = MakeLowerCamel(struct_def); + 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); - code += GenIndents(2) + "if not " + struct_instance_name + "." + - field_accessor_name + "IsNone():"; - code += GenIndents(3) + "self." + field_instance_name + " = []"; - code += GenIndents(3) + "for i in range(" + struct_instance_name + "." + - field_accessor_name + "Length()):"; + code += GenIndents(2) + "if not " + struct_var + "." + field_method + + "IsNone():"; + code += GenIndents(3) + "self." + field_field + " = []"; + code += GenIndents(3) + "for i in range(" + struct_var + "." + + field_method + "Length()):"; - auto field_type_name = TypeName(field); - auto one_instance = field_type_name + "_"; + auto field_type = TypeName(field); + auto one_instance = field_type + "_"; one_instance[0] = CharToLower(one_instance[0]); if (parser_.opts.include_dependence_headers) { auto package_reference = GenPackageReference(field.value.type); - field_type_name = package_reference + "." + TypeName(field); + field_type = package_reference + "." + TypeName(field); } - code += GenIndents(4) + "if " + struct_instance_name + "." + - field_accessor_name + "(i) is None:"; - code += GenIndents(5) + "self." + field_instance_name + ".append(None)"; + code += GenIndents(4) + "if " + struct_var + "." + field_method + + "(i) is None:"; + code += GenIndents(5) + "self." + field_field + ".append(None)"; code += GenIndents(4) + "else:"; - code += GenIndents(5) + one_instance + " = " + field_type_name + - "T.InitFromObj(" + struct_instance_name + "." + - field_accessor_name + "(i))"; - code += GenIndents(5) + "self." + field_instance_name + ".append(" + - one_instance + ")"; + code += GenIndents(5) + one_instance + " = " + field_type + + "T.InitFromObj(" + struct_var + "." + field_method + "(i))"; + code += + GenIndents(5) + "self." + field_field + ".append(" + one_instance + ")"; } void GenUnpackforScalarVectorHelper(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr, int indents) { + std::string *code_ptr, + int indents) const { auto &code = *code_ptr; - auto field_instance_name = MakeLowerCamel(field); - auto field_accessor_name = MakeUpperCamel(field); - auto struct_instance_name = MakeLowerCamel(struct_def); + 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); - code += GenIndents(indents) + "self." + field_instance_name + " = []"; - code += GenIndents(indents) + "for i in range(" + struct_instance_name + - "." + field_accessor_name + "Length()):"; - code += GenIndents(indents + 1) + "self." + field_instance_name + - ".append(" + struct_instance_name + "." + field_accessor_name + - "(i))"; + code += GenIndents(indents) + "self." + field_field + " = []"; + code += GenIndents(indents) + "for i in range(" + struct_var + "." + + field_method + "Length()):"; + code += GenIndents(indents + 1) + "self." + field_field + ".append(" + + struct_var + "." + field_method + "(i))"; } void GenUnPackForScalarVector(const StructDef &struct_def, - const FieldDef &field, std::string *code_ptr) { + const FieldDef &field, + std::string *code_ptr) const { auto &code = *code_ptr; - auto field_instance_name = MakeLowerCamel(field); - auto field_accessor_name = MakeUpperCamel(field); - auto struct_instance_name = MakeLowerCamel(struct_def); + 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); - code += GenIndents(2) + "if not " + struct_instance_name + "." + - field_accessor_name + "IsNone():"; + code += GenIndents(2) + "if not " + struct_var + "." + field_method + + "IsNone():"; // String does not have the AsNumpy method. if (!(IsScalar(field.value.type.VectorType().base_type))) { @@ -1204,23 +1193,23 @@ class PythonGenerator : public BaseGenerator { // If numpy exists, use the AsNumpy method to optimize the unpack speed. code += GenIndents(3) + "else:"; - code += GenIndents(4) + "self." + field_instance_name + " = " + - struct_instance_name + "." + field_accessor_name + "AsNumpy()"; + code += GenIndents(4) + "self." + field_field + " = " + struct_var + "." + + field_method + "AsNumpy()"; } void GenUnPackForScalar(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; - auto field_instance_name = MakeLowerCamel(field); - auto field_accessor_name = MakeUpperCamel(field); - auto struct_instance_name = MakeLowerCamel(struct_def); + 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); - code += GenIndents(2) + "self." + field_instance_name + " = " + - struct_instance_name + "." + field_accessor_name + "()"; + code += GenIndents(2) + "self." + field_field + " = " + struct_var + "." + + field_method + "()"; } // Generates the UnPack method for the object class. - void GenUnPack(const StructDef &struct_def, std::string *code_ptr) { + void GenUnPack(const StructDef &struct_def, std::string *code_ptr) const { std::string code; // Items that needs to be imported. No duplicate modules will be imported. std::set import_list; @@ -1259,12 +1248,11 @@ class PythonGenerator : public BaseGenerator { // Writes import statements and code into the generated file. auto &code_base = *code_ptr; - auto struct_instance_name = MakeLowerCamel(struct_def); - auto struct_name = MakeUpperCamel(struct_def); + const auto struct_var = namer_.Variable(struct_def.name); GenReceiverForObjectAPI(struct_def, code_ptr); - code_base += "_UnPack(self, " + struct_instance_name + "):"; - code_base += GenIndents(2) + "if " + struct_instance_name + " is None:"; + code_base += "_UnPack(self, " + struct_var + "):"; + code_base += GenIndents(2) + "if " + struct_var + " is None:"; code_base += GenIndents(3) + "return"; // Write the import statements. @@ -1278,13 +1266,14 @@ class PythonGenerator : public BaseGenerator { code_base += "\n"; } - void GenPackForStruct(const StructDef &struct_def, std::string *code_ptr) { + void GenPackForStruct(const StructDef &struct_def, + std::string *code_ptr) const { auto &code = *code_ptr; - auto struct_name = MakeUpperCamel(struct_def); + const auto struct_fn = namer_.Function(struct_def.name); GenReceiverForObjectAPI(struct_def, code_ptr); code += "Pack(self, builder):"; - code += GenIndents(2) + "return Create" + struct_name + "(builder"; + code += GenIndents(2) + "return Create" + struct_fn + "(builder"; StructBuilderArgs(struct_def, /* nameprefix = */ "self.", @@ -1297,65 +1286,61 @@ class PythonGenerator : public BaseGenerator { void GenPackForStructVectorField(const StructDef &struct_def, const FieldDef &field, std::string *code_prefix_ptr, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code_prefix = *code_prefix_ptr; auto &code = *code_ptr; - auto field_instance_name = MakeLowerCamel(field); - auto struct_name = NormalizedName(struct_def); - auto field_accessor_name = MakeUpperCamel(field); + 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); // Creates the field. - code_prefix += - GenIndents(2) + "if self." + field_instance_name + " is not None:"; + code_prefix += GenIndents(2) + "if self." + field_field + " is not None:"; if (field.value.type.struct_def->fixed) { - code_prefix += GenIndents(3) + struct_name + "Start" + - field_accessor_name + "Vector(builder, len(self." + - field_instance_name + "))"; + code_prefix += GenIndents(3) + struct_type + "Start" + field_method + + "Vector(builder, len(self." + field_field + "))"; code_prefix += GenIndents(3) + "for i in reversed(range(len(self." + - field_instance_name + "))):"; + field_field + "))):"; code_prefix += - GenIndents(4) + "self." + field_instance_name + "[i].Pack(builder)"; - code_prefix += - GenIndents(3) + field_instance_name + " = builder.EndVector()"; + GenIndents(4) + "self." + field_field + "[i].Pack(builder)"; + code_prefix += GenIndents(3) + field_field + " = builder.EndVector()"; } else { // If the vector is a struct vector, we need to first build accessor for // each struct element. - code_prefix += GenIndents(3) + field_instance_name + "list = []"; + code_prefix += GenIndents(3) + field_field + "list = []"; code_prefix += GenIndents(3); - code_prefix += "for i in range(len(self." + field_instance_name + ")):"; - code_prefix += GenIndents(4) + field_instance_name + "list.append(self." + - field_instance_name + "[i].Pack(builder))"; + code_prefix += "for i in range(len(self." + field_field + ")):"; + code_prefix += GenIndents(4) + field_field + "list.append(self." + + field_field + "[i].Pack(builder))"; - code_prefix += GenIndents(3) + struct_name + "Start" + - field_accessor_name + "Vector(builder, len(self." + - field_instance_name + "))"; + code_prefix += GenIndents(3) + struct_type + "Start" + field_method + + "Vector(builder, len(self." + field_field + "))"; code_prefix += GenIndents(3) + "for i in reversed(range(len(self." + - field_instance_name + "))):"; + field_field + "))):"; code_prefix += GenIndents(4) + "builder.PrependUOffsetTRelative" + "(" + - field_instance_name + "list[i])"; - code_prefix += - GenIndents(3) + field_instance_name + " = builder.EndVector()"; + field_field + "list[i])"; + code_prefix += GenIndents(3) + field_field + " = builder.EndVector()"; } // Adds the field into the struct. - code += GenIndents(2) + "if self." + field_instance_name + " is not None:"; - code += GenIndents(3) + struct_name + "Add" + field_accessor_name + - "(builder, " + field_instance_name + ")"; + code += GenIndents(2) + "if self." + field_field + " is not None:"; + code += GenIndents(3) + struct_type + "Add" + field_method + "(builder, " + + field_field + ")"; } void GenPackForScalarVectorFieldHelper(const StructDef &struct_def, const FieldDef &field, - std::string *code_ptr, int indents) { + std::string *code_ptr, + int indents) const { auto &code = *code_ptr; - auto field_instance_name = MakeLowerCamel(field); - auto field_accessor_name = MakeUpperCamel(field); - auto struct_name = NormalizedName(struct_def); - auto vectortype = field.value.type.VectorType(); + 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 vectortype = field.value.type.VectorType(); - code += GenIndents(indents) + struct_name + "Start" + field_accessor_name + - "Vector(builder, len(self." + field_instance_name + "))"; + code += GenIndents(indents) + struct_type + "Start" + field_method + + "Vector(builder, len(self." + field_field + "))"; code += GenIndents(indents) + "for i in reversed(range(len(self." + - field_instance_name + "))):"; + field_field + "))):"; code += GenIndents(indents + 1) + "builder.Prepend"; std::string type_name; @@ -1380,118 +1365,109 @@ class PythonGenerator : public BaseGenerator { void GenPackForScalarVectorField(const StructDef &struct_def, const FieldDef &field, std::string *code_prefix_ptr, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; auto &code_prefix = *code_prefix_ptr; - auto field_instance_name = MakeLowerCamel(field); - auto field_accessor_name = MakeUpperCamel(field); - auto struct_name = NormalizedName(struct_def); + 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); // Adds the field into the struct. - code += GenIndents(2) + "if self." + field_instance_name + " is not None:"; - code += GenIndents(3) + struct_name + "Add" + field_accessor_name + - "(builder, " + field_instance_name + ")"; + code += GenIndents(2) + "if self." + field_field + " is not None:"; + code += GenIndents(3) + struct_type + "Add" + field_method + "(builder, " + + field_field + ")"; // Creates the field. - code_prefix += - GenIndents(2) + "if self." + field_instance_name + " is not None:"; + code_prefix += GenIndents(2) + "if self." + field_field + " is not None:"; // If the vector is a string vector, we need to first build accessor for // each string element. And this generated code, needs to be // placed ahead of code_prefix. auto vectortype = field.value.type.VectorType(); if (IsString(vectortype)) { - code_prefix += GenIndents(3) + MakeLowerCamel(field) + "list = []"; - code_prefix += GenIndents(3) + "for i in range(len(self." + - field_instance_name + ")):"; - code_prefix += GenIndents(4) + MakeLowerCamel(field) + - "list.append(builder.CreateString(self." + - field_instance_name + "[i]))"; - GenPackForScalarVectorFieldHelper(struct_def, field, code_prefix_ptr, 3); - code_prefix += "(" + MakeLowerCamel(field) + "list[i])"; + code_prefix += GenIndents(3) + field_field + "list = []"; code_prefix += - GenIndents(3) + field_instance_name + " = builder.EndVector()"; + GenIndents(3) + "for i in range(len(self." + field_field + ")):"; + code_prefix += GenIndents(4) + field_field + + "list.append(builder.CreateString(self." + field_field + + "[i]))"; + GenPackForScalarVectorFieldHelper(struct_def, field, code_prefix_ptr, 3); + code_prefix += "(" + field_field + "list[i])"; + code_prefix += GenIndents(3) + field_field + " = builder.EndVector()"; return; } code_prefix += GenIndents(3) + "if np is not None and type(self." + - field_instance_name + ") is np.ndarray:"; - code_prefix += GenIndents(4) + field_instance_name + - " = builder.CreateNumpyVector(self." + field_instance_name + - ")"; + field_field + ") is np.ndarray:"; + code_prefix += GenIndents(4) + field_field + + " = builder.CreateNumpyVector(self." + field_field + ")"; code_prefix += GenIndents(3) + "else:"; GenPackForScalarVectorFieldHelper(struct_def, field, code_prefix_ptr, 4); - code_prefix += "(self." + field_instance_name + "[i])"; - code_prefix += - GenIndents(4) + field_instance_name + " = builder.EndVector()"; + code_prefix += "(self." + field_field + "[i])"; + code_prefix += GenIndents(4) + field_field + " = builder.EndVector()"; } void GenPackForStructField(const StructDef &struct_def, const FieldDef &field, std::string *code_prefix_ptr, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code_prefix = *code_prefix_ptr; auto &code = *code_ptr; - auto field_instance_name = MakeLowerCamel(field); - - auto field_accessor_name = MakeUpperCamel(field); - auto struct_name = NormalizedName(struct_def); + 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); if (field.value.type.struct_def->fixed) { // Pure struct fields need to be created along with their parent // structs. - code += - GenIndents(2) + "if self." + field_instance_name + " is not None:"; - code += GenIndents(3) + field_instance_name + " = self." + - field_instance_name + ".Pack(builder)"; + code += GenIndents(2) + "if self." + field_field + " is not None:"; + code += GenIndents(3) + field_field + " = self." + field_field + + ".Pack(builder)"; } else { // Tables need to be created before their parent structs are created. - code_prefix += - GenIndents(2) + "if self." + field_instance_name + " is not None:"; - code_prefix += GenIndents(3) + field_instance_name + " = self." + - field_instance_name + ".Pack(builder)"; - code += - GenIndents(2) + "if self." + field_instance_name + " is not None:"; + code_prefix += GenIndents(2) + "if self." + field_field + " is not None:"; + code_prefix += GenIndents(3) + field_field + " = self." + field_field + + ".Pack(builder)"; + code += GenIndents(2) + "if self." + field_field + " is not None:"; } - code += GenIndents(3) + struct_name + "Add" + field_accessor_name + - "(builder, " + field_instance_name + ")"; + code += GenIndents(3) + struct_type + "Add" + field_method + "(builder, " + + field_field + ")"; } void GenPackForUnionField(const StructDef &struct_def, const FieldDef &field, std::string *code_prefix_ptr, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code_prefix = *code_prefix_ptr; auto &code = *code_ptr; - auto field_instance_name = MakeLowerCamel(field); - - auto field_accessor_name = MakeUpperCamel(field); - auto struct_name = NormalizedName(struct_def); + 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); // TODO(luwa): TypeT should be moved under the None check as well. - code_prefix += - GenIndents(2) + "if self." + field_instance_name + " is not None:"; - code_prefix += GenIndents(3) + field_instance_name + " = self." + - field_instance_name + ".Pack(builder)"; - code += GenIndents(2) + "if self." + field_instance_name + " is not None:"; - code += GenIndents(3) + struct_name + "Add" + field_accessor_name + - "(builder, " + field_instance_name + ")"; + code_prefix += GenIndents(2) + "if self." + field_field + " is not None:"; + code_prefix += GenIndents(3) + field_field + " = self." + field_field + + ".Pack(builder)"; + code += GenIndents(2) + "if self." + field_field + " is not None:"; + code += GenIndents(3) + struct_type + "Add" + field_method + "(builder, " + + field_field + ")"; } - void GenPackForTable(const StructDef &struct_def, std::string *code_ptr) { + void GenPackForTable(const StructDef &struct_def, + std::string *code_ptr) const { auto &code_base = *code_ptr; std::string code, code_prefix; - auto struct_instance_name = MakeLowerCamel(struct_def); - auto struct_name = NormalizedName(struct_def); + const auto struct_var = namer_.Variable(struct_def.name); + const auto struct_type = namer_.Type(struct_def.name); GenReceiverForObjectAPI(struct_def, code_ptr); code_base += "Pack(self, builder):"; - code += GenIndents(2) + struct_name + "Start(builder)"; + code += GenIndents(2) + struct_type + "Start(builder)"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (field.deprecated) continue; - auto field_accessor_name = MakeUpperCamel(field); - auto field_instance_name = MakeLowerCamel(field); + const auto field_method = namer_.Method(field.name); + const auto field_field = namer_.Field(field.name); switch (field.value.type.base_type) { case BASE_TYPE_STRUCT: { @@ -1516,37 +1492,34 @@ class PythonGenerator : public BaseGenerator { break; } case BASE_TYPE_STRING: { - code_prefix += GenIndents(2) + "if self." + field_instance_name + - " is not None:"; - code_prefix += GenIndents(3) + field_instance_name + - " = builder.CreateString(self." + field_instance_name + - ")"; - code += GenIndents(2) + "if self." + field_instance_name + - " is not None:"; - code += GenIndents(3) + struct_name + "Add" + field_accessor_name + - "(builder, " + field_instance_name + ")"; + code_prefix += + GenIndents(2) + "if self." + field_field + " is not None:"; + code_prefix += GenIndents(3) + field_field + + " = builder.CreateString(self." + field_field + ")"; + code += GenIndents(2) + "if self." + field_field + " is not None:"; + code += GenIndents(3) + struct_type + "Add" + field_method + + "(builder, " + field_field + ")"; break; } default: // Generates code for scalar values. If the value equals to the // default value, builder will automatically ignore it. So we don't // need to check the value ahead. - code += GenIndents(2) + struct_name + "Add" + field_accessor_name + - "(builder, self." + field_instance_name + ")"; + code += GenIndents(2) + struct_type + "Add" + field_method + + "(builder, self." + field_field + ")"; break; } } - code += GenIndents(2) + struct_instance_name + " = " + struct_name + - "End(builder)"; - code += GenIndents(2) + "return " + struct_instance_name; + code += GenIndents(2) + struct_var + " = " + struct_type + "End(builder)"; + code += GenIndents(2) + "return " + struct_var; code_base += code_prefix + code; code_base += "\n"; } void GenStructForObjectAPI(const StructDef &struct_def, - std::string *code_ptr) { + std::string *code_ptr) const { if (struct_def.generated) return; std::set import_list; @@ -1580,14 +1553,14 @@ class PythonGenerator : public BaseGenerator { } void GenUnionCreatorForStruct(const EnumDef &enum_def, const EnumVal &ev, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; - auto union_name = NormalizedName(enum_def); - auto field_name = NormalizedName(ev); - auto field_type = GenTypeGet(ev.union_type) + "T"; + 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)); - code += GenIndents(1) + "if unionType == " + union_name + "()." + - field_name + ":"; + code += GenIndents(1) + "if unionType == " + union_type + "()." + + variant + ":"; if (parser_.opts.include_dependence_headers) { auto package_reference = GenPackageReference(ev.union_type); code += GenIndents(2) + "import " + package_reference; @@ -1598,27 +1571,27 @@ class PythonGenerator : public BaseGenerator { } void GenUnionCreatorForString(const EnumDef &enum_def, const EnumVal &ev, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; - auto union_name = NormalizedName(enum_def); - auto field_name = NormalizedName(ev); + const auto union_type = namer_.Type(enum_def.name); + const auto variant = namer_.Variant(ev.name); - code += GenIndents(1) + "if unionType == " + union_name + "()." + - field_name + ":"; + code += GenIndents(1) + "if unionType == " + union_type + "()." + + variant + ":"; code += GenIndents(2) + "tab = Table(table.Bytes, table.Pos)"; code += GenIndents(2) + "union = tab.String(table.Pos)"; code += GenIndents(2) + "return union"; } // Creates an union object based on union type. - void GenUnionCreator(const EnumDef &enum_def, std::string *code_ptr) { + void GenUnionCreator(const EnumDef &enum_def, std::string *code_ptr) const { if (enum_def.generated) return; auto &code = *code_ptr; - auto union_name = MakeUpperCamel(enum_def); + const auto enum_fn = namer_.Function(enum_def.name); code += "\n"; - code += "def " + union_name + "Creator(unionType, table):"; + code += "def " + enum_fn + "Creator(unionType, table):"; code += GenIndents(1) + "from flatbuffers.table import Table"; code += GenIndents(1) + "if not isinstance(table, Table):"; code += GenIndents(2) + "return None"; @@ -1641,11 +1614,11 @@ class PythonGenerator : public BaseGenerator { } // Generate enum declarations. - void GenEnum(const EnumDef &enum_def, std::string *code_ptr) { + void GenEnum(const EnumDef &enum_def, std::string *code_ptr) const { if (enum_def.generated) return; GenComment(enum_def.doc_comment, code_ptr, &def_comment); - BeginEnum(NormalizedName(enum_def), code_ptr); + BeginEnum(enum_def, code_ptr); for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) { auto &ev = **it; GenComment(ev.doc_comment, code_ptr, &def_comment, Indent.c_str()); @@ -1654,25 +1627,25 @@ class PythonGenerator : public BaseGenerator { } // Returns the function name that is able to read a value of the given type. - std::string GenGetter(const Type &type) { + std::string GenGetter(const Type &type) const { switch (type.base_type) { case BASE_TYPE_STRING: return "self._tab.String("; case BASE_TYPE_UNION: return "self._tab.Union("; case BASE_TYPE_VECTOR: return GenGetter(type.VectorType()); default: return "self._tab.Get(flatbuffers.number_types." + - ConvertCase(GenTypeGet(type), Case::kUpperCamel) + "Flags, "; + namer_.Method(GenTypeGet(type)) + "Flags, "; } } // Returns the method name for use with add/put calls. - std::string GenMethod(const FieldDef &field) { + std::string GenMethod(const FieldDef &field) const { return (IsScalar(field.value.type.base_type) || IsArray(field.value.type)) - ? ConvertCase(GenTypeBasic(field.value.type), Case::kUpperCamel) + ? namer_.Method(GenTypeBasic(field.value.type)) : (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative"); } - std::string GenTypeBasic(const Type &type) { + std::string GenTypeBasic(const Type &type) const { // clang-format off static const char *ctypename[] = { #define FLATBUFFERS_TD(ENUM, IDLTYPE, \ @@ -1686,7 +1659,7 @@ class PythonGenerator : public BaseGenerator { : type.base_type]; } - std::string GenTypePointer(const Type &type) { + std::string GenTypePointer(const Type &type) const { switch (type.base_type) { case BASE_TYPE_STRING: return "string"; case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType()); @@ -1697,16 +1670,17 @@ class PythonGenerator : public BaseGenerator { } } - std::string GenTypeGet(const Type &type) { + std::string GenTypeGet(const Type &type) const { return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type); } - std::string TypeName(const FieldDef &field) { + std::string TypeName(const FieldDef &field) const { return GenTypeGet(field.value.type); } // Create a struct with a builder and the struct's arguments. - void GenStructBuilder(const StructDef &struct_def, std::string *code_ptr) { + void GenStructBuilder(const StructDef &struct_def, + std::string *code_ptr) const { BeginBuilderArgs(struct_def, code_ptr); StructBuilderArgs(struct_def, /* nameprefix = */ "", @@ -1725,7 +1699,8 @@ class PythonGenerator : public BaseGenerator { if (!generateStructs(&one_file_code)) return false; if (parser_.opts.one_file) { - return SaveType(file_name_ + "_generated", *parser_.current_namespace_, + // Legacy file format uses keep casing. + return SaveType(file_name_ + "_generated.py", *parser_.current_namespace_, one_file_code, true); } @@ -1733,7 +1708,7 @@ class PythonGenerator : public BaseGenerator { } private: - bool generateEnums(std::string *one_file_code) { + bool generateEnums(std::string *one_file_code) const { for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { auto &enum_def = **it; @@ -1746,15 +1721,15 @@ class PythonGenerator : public BaseGenerator { if (parser_.opts.one_file && !enumcode.empty()) { *one_file_code += enumcode + "\n\n"; } else { - if (!SaveType(enum_def.name, *enum_def.defined_namespace, enumcode, - false)) + if (!SaveType(namer_.File(enum_def.name, SkipFile::Suffix), + *enum_def.defined_namespace, enumcode, false)) return false; } } return true; } - bool generateStructs(std::string *one_file_code) { + bool generateStructs(std::string *one_file_code) const { for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); ++it) { auto &struct_def = **it; @@ -1767,8 +1742,8 @@ class PythonGenerator : public BaseGenerator { if (parser_.opts.one_file && !declcode.empty()) { *one_file_code += declcode + "\n\n"; } else { - if (!SaveType(struct_def.name, *struct_def.defined_namespace, declcode, - true)) + if (!SaveType(namer_.File(struct_def.name, SkipFile::Suffix), + *struct_def.defined_namespace, declcode, true)) return false; } } @@ -1777,7 +1752,7 @@ class PythonGenerator : public BaseGenerator { // Begin by declaring namespace and imports. void BeginFile(const std::string &name_space_name, const bool needs_imports, - std::string *code_ptr) { + std::string *code_ptr) const { auto &code = *code_ptr; code = code + "# " + FlatBuffersGeneratedWarning() + "\n\n"; code += "# namespace: " + name_space_name + "\n\n"; @@ -1790,28 +1765,31 @@ class PythonGenerator : public BaseGenerator { // Save out the generated code for a Python Table type. bool SaveType(const std::string &defname, const Namespace &ns, - const std::string &classcode, bool needs_imports) { + const std::string &classcode, bool needs_imports) const { if (!classcode.length()) return true; - std::string namespace_dir = path_; - auto &namespaces = ns.components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (it != namespaces.begin()) namespace_dir += kPathSeparator; - namespace_dir += *it; - std::string init_py_filename = namespace_dir + "/__init__.py"; - SaveFile(init_py_filename.c_str(), "", false); - } - std::string code = ""; BeginFile(LastNamespacePart(ns), needs_imports, &code); code += classcode; - std::string filename = NamespaceDir(ns) + defname + ".py"; + + const std::string directories = + parser_.opts.one_file ? path_ : namer_.Directories(ns.components); + EnsureDirExists(directories); + + for (size_t i = path_.size() + 1; i != std::string::npos; + i = directories.find(kPathSeparator, i + 1)) { + const std::string init_py = + directories.substr(0, i) + kPathSeparator + "__init__.py"; + SaveFile(init_py.c_str(), "", false); + } + + const std::string filename = directories + defname; return SaveFile(filename.c_str(), code, false); } private: - std::unordered_set keywords_; const SimpleFloatConstantGenerator float_const_gen_; + const Namer namer_; }; } // namespace python diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 542fe91c7..17853a09d 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -34,6 +34,7 @@ Namer::Config RustDefaultConfig() { /*methods=*/Case::kSnake, /*functions=*/Case::kSnake, /*fields=*/Case::kKeep, + /*variables=*/Case::kUnknown, // Unused. /*variants=*/Case::kKeep, /*enum_variant_seperator=*/"::", /*namespaces=*/Case::kSnake, diff --git a/src/namer.h b/src/namer.h index 53214a895..f0bbe493d 100644 --- a/src/namer.h +++ b/src/namer.h @@ -13,7 +13,7 @@ enum class SkipFile { Extension = 2, SuffixAndExtension = 3, }; -SkipFile operator&(SkipFile a, SkipFile b) { +inline SkipFile operator&(SkipFile a, SkipFile b) { return static_cast(static_cast(a) & static_cast(b)); } @@ -40,6 +40,9 @@ class Namer { // Case style for flatbuffers-defined fields. // e.g. `struct Struct { int my_field; }` Case fields; + // Case style for flatbuffers-defined variables. + // e.g. `int my_variable = 2` + Case variables; // Case style for flatbuffers-defined variants. // e.g. `enum class Enum { MyVariant, }` Case variants; @@ -117,6 +120,10 @@ class Namer { 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); } @@ -142,6 +149,11 @@ class Namer { return result; } + std::string NamespacedType(const std::vector &ns, + const std::string &s) const { + return Namespace(ns) + config_.namespace_seperator + Type(s); + } + // Returns `filename` with the right casing, suffix, and extension. std::string File(const std::string &filename, SkipFile skips = SkipFile::None) const {