summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Burke <max@urbanlogiq.com>2023-04-25 21:38:16 -0700
committerGitHub <noreply@github.com>2023-04-25 21:38:16 -0700
commit56ecc1f548f4bb4143b7a01db9b01d331e468977 (patch)
treebeb8a63d614f1ac27b97e7b80cc6145468874d15 /src
parent3fda20d7c7fe1f8006210bddae8cb55bc7a74c3b (diff)
downloadflatbuffers-56ecc1f548f4bb4143b7a01db9b01d331e468977.tar.gz
flatbuffers-56ecc1f548f4bb4143b7a01db9b01d331e468977.tar.bz2
flatbuffers-56ecc1f548f4bb4143b7a01db9b01d331e468977.zip
Optionally generate type prefixes and suffixes for python code (#7857)
* optionally generate type prefixes and suffixes for python code * fix codegen error when qualified name is empty * generated code updated
Diffstat (limited to 'src')
-rw-r--r--src/flatc.cpp6
-rw-r--r--src/idl_gen_python.cpp71
2 files changed, 47 insertions, 30 deletions
diff --git a/src/flatc.cpp b/src/flatc.cpp
index a5dd0b1f..0e20a2f7 100644
--- a/src/flatc.cpp
+++ b/src/flatc.cpp
@@ -250,6 +250,8 @@ const static FlatCOption flatc_options[] = {
{ "", "no-leak-private-annotation", "",
"Prevents multiple type of annotations within a Fbs SCHEMA file. "
"Currently this is required to generate private types in Rust" },
+ { "", "python-no-type-prefix-suffix", "",
+ "Skip emission of Python functions that are prefixed with typenames" },
{ "", "file-names-only", "",
"Print out generated file names without writing to the files"},
};
@@ -650,7 +652,9 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
opts.ts_no_import_ext = true;
} else if (arg == "--no-leak-private-annotation") {
opts.no_leak_private_annotations = true;
- } else if (arg == "--annotate-sparse-vectors") {
+ } else if (arg == "--python-no-type-prefix-suffix") {
+ opts.python_no_type_prefix_suffix = true;
+ } else if (arg == "--annotate-sparse-vectors") {
options.annotate_include_vector_contents = false;
} else if (arg == "--annotate") {
if (++argi >= argc) Error("missing path following: " + arg, true);
diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp
index 5b5ce353..6c93b909 100644
--- a/src/idl_gen_python.cpp
+++ b/src/idl_gen_python.cpp
@@ -139,13 +139,15 @@ class PythonGenerator : public BaseGenerator {
code += Indent + Indent + "return x\n";
code += "\n";
- // Add an alias with the old name
- code += Indent + "@classmethod\n";
- code += Indent + "def GetRootAs" + struct_type + "(cls, buf, offset=0):\n";
- code +=
- Indent + Indent +
- "\"\"\"This method is deprecated. Please switch to GetRootAs.\"\"\"\n";
- code += Indent + Indent + "return cls.GetRootAs(buf, offset)\n";
+ if (!parser_.opts.python_no_type_prefix_suffix) {
+ // Add an alias with the old name
+ code += Indent + "@classmethod\n";
+ code += Indent + "def GetRootAs" + struct_type + "(cls, buf, offset=0):\n";
+ code +=
+ Indent + Indent +
+ "\"\"\"This method is deprecated. Please switch to GetRootAs.\"\"\"\n";
+ code += Indent + Indent + "return cls.GetRootAs(buf, offset)\n";
+ }
}
// Initialize an existing object with other data, to avoid an allocation.
@@ -480,7 +482,10 @@ class PythonGenerator : public BaseGenerator {
if (!nested) { return; } // There is no nested flatbuffer.
const std::string unqualified_name = nested->constant;
- const std::string qualified_name = NestedFlatbufferType(unqualified_name);
+ std::string qualified_name = NestedFlatbufferType(unqualified_name);
+ if (qualified_name.empty()) {
+ qualified_name = nested->constant;
+ }
auto &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
@@ -491,7 +496,7 @@ class PythonGenerator : public BaseGenerator {
code += Indent + Indent + Indent;
code += "from " + qualified_name + " import " + unqualified_name + "\n";
code += Indent + Indent + Indent + "return " + unqualified_name;
- code += ".GetRootAs" + unqualified_name;
+ code += ".GetRootAs";
code += "(self._tab.Bytes, self._tab.Vector(o))\n";
code += Indent + Indent + "return 0\n";
code += "\n";
@@ -605,15 +610,18 @@ class PythonGenerator : public BaseGenerator {
auto &code = *code_ptr;
const auto struct_type = namer_.Type(struct_def);
// Generate method with struct name.
- code += "def " + struct_type + "Start(builder): ";
- code += "builder.StartObject(";
+
+ const auto name = parser_.opts.python_no_type_prefix_suffix ? "Start" : struct_type + "Start";
+
+ code += "def " + name + "(builder):\n";
+ code += Indent + "return builder.StartObject(";
code += NumToString(struct_def.fields.vec.size());
- code += ")\n";
+ code += ")\n\n";
- if (!parser_.opts.one_file) {
+ if (!parser_.opts.one_file && !parser_.opts.python_no_type_prefix_suffix) {
// Generate method without struct name.
code += "def Start(builder):\n";
- code += Indent + "return " + struct_type + "Start(builder)\n";
+ code += Indent + "return " + struct_type + "Start(builder)\n\n";
}
}
@@ -624,12 +632,14 @@ class PythonGenerator : public BaseGenerator {
const std::string field_var = namer_.Variable(field);
const std::string field_method = namer_.Method(field);
+ const auto name = parser_.opts.python_no_type_prefix_suffix ? "Add" + field_method : namer_.Type(struct_def) + "Add" + field_method;
+
// Generate method with struct name.
- code += "def " + namer_.Type(struct_def) + "Add" + field_method;
+ code += "def " + name;
code += "(builder, ";
code += field_var;
- code += "): ";
- code += "builder.Prepend";
+ code += "):\n";
+ code += Indent + "return builder.Prepend";
code += GenMethod(field) + "Slot(";
code += NumToString(offset) + ", ";
if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) {
@@ -646,16 +656,16 @@ class PythonGenerator : public BaseGenerator {
} else {
code += field.value.constant;
}
- code += ")\n";
+ code += ")\n\n";
- if (!parser_.opts.one_file) {
+ if (!parser_.opts.one_file && !parser_.opts.python_no_type_prefix_suffix) {
// Generate method without struct name.
code += "def Add" + field_method + "(builder, " + field_var + "):\n";
code +=
Indent + "return " + namer_.Type(struct_def) + "Add" + field_method;
code += "(builder, ";
code += field_var;
- code += ")\n";
+ code += ")\n\n";
}
}
@@ -667,20 +677,22 @@ class PythonGenerator : public BaseGenerator {
const std::string field_method = namer_.Method(field);
// Generate method with struct name.
- code += "def " + struct_type + "Start" + field_method;
- code += "Vector(builder, numElems): return builder.StartVector(";
+ const auto name = parser_.opts.python_no_type_prefix_suffix ? "Start" + field_method : struct_type + "Start" + field_method;
+ code += "def " + name;
+ code += "Vector(builder, numElems):\n";
+ code += Indent + "return builder.StartVector(";
auto vector_type = field.value.type.VectorType();
auto alignment = InlineAlignment(vector_type);
auto elem_size = InlineSize(vector_type);
code += NumToString(elem_size);
code += ", numElems, " + NumToString(alignment);
- code += ")\n";
+ code += ")\n\n";
- if (!parser_.opts.one_file) {
+ if (!parser_.opts.one_file && !parser_.opts.python_no_type_prefix_suffix) {
// Generate method without struct name.
code += "def Start" + field_method + "Vector(builder, numElems):\n";
code += Indent + "return " + struct_type + "Start";
- code += field_method + "Vector(builder, numElems)\n";
+ code += field_method + "Vector(builder, numElems)\n\n";
}
}
@@ -725,15 +737,16 @@ class PythonGenerator : public BaseGenerator {
std::string *code_ptr) const {
auto &code = *code_ptr;
+ const auto name = parser_.opts.python_no_type_prefix_suffix ? "End" : namer_.Type(struct_def) + "End";
// Generate method with struct name.
- code += "def " + namer_.Type(struct_def) + "End";
- code += "(builder): ";
- code += "return builder.EndObject()\n";
+ code += "def " + name + "(builder):\n";
+ code += Indent + "return builder.EndObject()\n\n";
- if (!parser_.opts.one_file) {
+ if (!parser_.opts.one_file && !parser_.opts.python_no_type_prefix_suffix) {
// Generate method without struct name.
code += "def End(builder):\n";
code += Indent + "return " + namer_.Type(struct_def) + "End(builder)";
+ code += "\n";
}
}