diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index d4d737d30..deb1edf86 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -358,6 +358,7 @@ struct IDLOptions { bool allow_non_utf8; std::string include_prefix; bool binary_schema_comments; + std::string go_namespace; // Possible options for the more general generator below. enum Language { diff --git a/src/flatc.cpp b/src/flatc.cpp index 8eef56170..f12b3d08c 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -90,6 +90,7 @@ std::string FlatCompiler::GetUsageString(const char* program_name) const { " T::c_str() and T::length() must be supported\n" " --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" " --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" @@ -160,6 +161,9 @@ int FlatCompiler::Compile(int argc, const char** argv) { opts.skip_js_exports = true; } else if(arg == "--goog-js-export") { opts.use_goog_js_export_format = true; + } else if(arg == "--go-namespace") { + if (++argi >= argc) Error("missing golang namespace" + arg, true); + opts.go_namespace = 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 88dfd2359..58a60aae6 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -17,6 +17,7 @@ // independent from idl_parser, since this code is not needed for most clients #include +#include #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" @@ -740,9 +741,15 @@ static void GenStructBuilder(const StructDef &struct_def, class GoGenerator : public BaseGenerator { public: GoGenerator(const Parser &parser, const std::string &path, - const std::string &file_name) - : BaseGenerator(parser, path, file_name, "" /* not used*/, - "" /* not used */){}; + const std::string &file_name, const std::string &go_namespace) + : BaseGenerator(parser, path, file_name, "" /* not used*/, "" /* not used */) { + std::istringstream iss(go_namespace); + std::string component; + while (std::getline(iss, component, '.')) { + go_namespace_.components.push_back(component); + } + } + bool generate() { for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { @@ -780,19 +787,22 @@ class GoGenerator : public BaseGenerator { bool needs_imports) { if (!classcode.length()) return true; + Namespace& ns = go_namespace_.components.empty() ? *def.defined_namespace : go_namespace_; std::string code = ""; - BeginFile(LastNamespacePart(*def.defined_namespace), needs_imports, &code); + BeginFile(LastNamespacePart(ns), needs_imports, &code); code += classcode; std::string filename = - NamespaceDir(*def.defined_namespace) + def.name + ".go"; + NamespaceDir(ns) + def.name + ".go"; return SaveFile(filename.c_str(), code, false); } + + Namespace go_namespace_; }; } // namespace go bool GenerateGo(const Parser &parser, const std::string &path, const std::string &file_name) { - go::GoGenerator generator(parser, path, file_name); + go::GoGenerator generator(parser, path, file_name, parser.opts.go_namespace); return generator.generate(); }