From a0e5d78353ac07b9fd6cbbbc1c372c4d6dadf49c Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 9 Oct 2017 09:27:30 -0700 Subject: [PATCH] Add new flatbuffer_go_library to generate Go library for flatbuffers This CL also introduces the following changes to allow the generation of the Go library for flatbuffers: - add support for --gen-onefile for Go to simplify the build rule (mapping each input .fbs to a single separate .go file) - add a new --go-import flag to override the default import line (currently github.com/google/flatbuffers/go) - add new go_library in BUILD for flatbuffer (for files in flatbuffers/go) (mirrored from cr/171126159) Change-Id: I83e705a9a9d9544837af0baf9366ec37757799aa --- include/flatbuffers/idl.h | 1 + src/flatc.cpp | 7 ++++++- src/idl_gen_go.cpp | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 201d15b58..dac254353 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -381,6 +381,7 @@ struct IDLOptions { bool keep_include_path; bool binary_schema_comments; bool skip_flatbuffers_import; + std::string go_import; std::string go_namespace; bool reexport_ts_modules; bool protobuf_ascii_alike; diff --git a/src/flatc.cpp b/src/flatc.cpp index ef77b0a98..da26fe10c 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -83,7 +83,7 @@ std::string FlatCompiler::GetUsageString(const char* program_name) const { " --no-includes Don\'t generate include statements for included\n" " schemas the generated file depends on (C++).\n" " --gen-mutable Generate accessors that can mutate buffers in-place.\n" - " --gen-onefile Generate single output file for C#.\n" + " --gen-onefile Generate single output file for C# and Go.\n" " --gen-name-strings Generate type name functions for C++.\n" " --gen-object-api Generate an additional object-based API.\n" " --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n" @@ -96,6 +96,8 @@ std::string FlatCompiler::GetUsageString(const char* program_name) const { " --no-js-exports Removes Node.js style export lines in JS.\n" " --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n" " --go-namespace Generate the overrided namespace in Golang.\n" + " --go-import Generate the overrided import for flatbuffers in Golang.\n" + " (default is \"github.com/google/flatbuffers/go\")\n" " --raw-binary Allow binaries without file_indentifier to be read.\n" " This may crash flatc given a mismatched schema.\n" " --proto Input is a .proto, translate to .fbs.\n" @@ -183,6 +185,9 @@ int FlatCompiler::Compile(int argc, const char** argv) { } else if(arg == "--go-namespace") { if (++argi >= argc) Error("missing golang namespace" + arg, true); opts.go_namespace = argv[argi]; + } else if(arg == "--go-import") { + if (++argi >= argc) Error("missing golang import" + arg, true); + opts.go_import = argv[argi]; } else if(arg == "--defaults-json") { opts.output_default_scalars_in_json = true; } else if (arg == "--unknown-json") { diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 0cc2e9349..1f8e09e07 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -34,6 +34,12 @@ #endif namespace flatbuffers { + +static std::string GeneratedFileName(const std::string &path, + const std::string &file_name) { + return path + file_name + "_generated.go"; +} + namespace go { // see https://golang.org/ref/spec#Keywords @@ -752,18 +758,35 @@ class GoGenerator : public BaseGenerator { } bool generate() { + std::string one_file_code; for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { std::string enumcode; go::GenEnum(**it, &enumcode); - if (!SaveType(**it, enumcode, false)) return false; + if (parser_.opts.one_file) { + one_file_code += enumcode; + } else { + if (!SaveType(**it, enumcode, false)) return false; + } } for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); ++it) { std::string declcode; go::GenStruct(**it, &declcode); - if (!SaveType(**it, declcode, true)) return false; + if (parser_.opts.one_file) { + one_file_code += declcode; + } else { + if (!SaveType(**it, declcode, true)) return false; + } + } + + if (parser_.opts.one_file) { + std::string code = ""; + BeginFile(LastNamespacePart(go_namespace_), true, &code); + code += one_file_code; + const std::string filename = GeneratedFileName(path_, file_name_); + return SaveFile(filename.c_str(), code, false); } return true; @@ -778,7 +801,11 @@ class GoGenerator : public BaseGenerator { code += "package " + name_space_name + "\n\n"; if (needs_imports) { code += "import (\n"; - code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n"; + if (!parser_.opts.go_import.empty()) { + code += "\tflatbuffers \"" + parser_.opts.go_import +"\"\n"; + } else{ + code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n"; + } code += ")\n\n"; } }