Skip to content

Commit ce47a60

Browse files
committed
Added --python-fields-snake-case option
1 parent 592dc50 commit ce47a60

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

include/flatbuffers/idl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,9 @@ struct IDLOptions {
729729
// Whether to generate numpy helpers.
730730
bool python_gen_numpy;
731731

732+
// Use snake-case for field names in the Python object API.
733+
bool python_fields_snake_case;
734+
732735
bool ts_omit_entrypoint;
733736
ProtoIdGapAction proto_id_gap_action;
734737

@@ -856,6 +859,7 @@ struct IDLOptions {
856859
python_no_type_prefix_suffix(false),
857860
python_typing(false),
858861
python_gen_numpy(true),
862+
python_fields_snake_case(false),
859863
ts_omit_entrypoint(false),
860864
proto_id_gap_action(ProtoIdGapAction::WARNING),
861865
mini_reflect(IDLOptions::kNone),

src/flatc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ const static FlatCOption flatc_options[] = {
266266
{"", "python-decode-obj-api-strings", "",
267267
"Decode bytes to strings for the Python Object API"},
268268
{"", "python-gen-numpy", "", "Whether to generate numpy helpers."},
269+
{"", "python-fields-snake-case", "",
270+
"Generate Python fields using snake_case naming convention."},
269271
{"", "ts-omit-entrypoint", "",
270272
"Omit emission of namespace entrypoint file"},
271273
{"", "file-names-only", "",
@@ -708,6 +710,8 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
708710
} else if (arg == "--no-python-gen-numpy" ||
709711
arg == "--python-gen-numpy=false") {
710712
opts.python_gen_numpy = false;
713+
} else if (arg == "--python-fields-snake-case") {
714+
opts.python_fields_snake_case = true;
711715
} else if (arg == "--ts-omit-entrypoint") {
712716
opts.ts_omit_entrypoint = true;
713717
} else if (arg == "--annotate-sparse-vectors") {

src/idl_gen_python.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,22 @@ typedef std::set<ImportMapEntry> ImportMap;
4848
static const CommentConfig def_comment = {nullptr, "#", nullptr};
4949
static const std::string Indent = " ";
5050

51+
inline Namer::Config PythonNamerConfig(const Namer::Config& input,
52+
const IDLOptions& opts,
53+
const std::string& path) {
54+
auto cfg = WithFlagOptions(input, opts, path);
55+
if (opts.python_fields_snake_case) {
56+
cfg.fields = Case::kSnake;
57+
}
58+
return cfg;
59+
}
60+
5161
class PythonStubGenerator {
5262
public:
5363
PythonStubGenerator(const Parser& parser, const std::string& path,
5464
const Version& version)
5565
: parser_{parser},
56-
namer_{WithFlagOptions(kStubConfig, parser.opts, path),
66+
namer_{PythonNamerConfig(kStubConfig, parser.opts, path),
5767
Keywords(version)},
5868
version_(version) {}
5969

@@ -698,7 +708,7 @@ class PythonGenerator : public BaseGenerator {
698708
: BaseGenerator(parser, path, file_name, "" /* not used */,
699709
"" /* not used */, "py"),
700710
float_const_gen_("float('nan')", "float('inf')", "float('-inf')"),
701-
namer_(WithFlagOptions(kConfig, parser.opts, path), Keywords(version)) {
711+
namer_(PythonNamerConfig(kConfig, parser.opts, path), Keywords(version)) {
702712
}
703713

704714
// Most field accessors need to retrieve and test the field offset first,
@@ -1321,7 +1331,7 @@ class PythonGenerator : public BaseGenerator {
13211331
} else {
13221332
code += IsArray(field_type) ? " " : "";
13231333
code += indent + " builder.Prepend" + GenMethod(field) + "(";
1324-
code += nameprefix + namer_.Variable(field);
1334+
code += nameprefix + namer_.Field(field);
13251335
size_t array_cnt = index + (IsArray(field_type) ? 1 : 0);
13261336
for (size_t i = 0; in_array && i < array_cnt; i++) {
13271337
code += "[_idx" + NumToString(i) + "-1]";

0 commit comments

Comments
 (0)