Commit b8f5f844 authored by Flier Lu's avatar Flier Lu Committed by Wouter van Oortmerssen

add command line argument for go namespace (#4222)

parent f2071e4f
......@@ -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 {
......
......@@ -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") {
......
......@@ -17,6 +17,7 @@
// independent from idl_parser, since this code is not needed for most clients
#include <string>
#include <sstream>
#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();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment