mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 23:40:01 +00:00
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
This commit is contained in:
@@ -381,6 +381,7 @@ struct IDLOptions {
|
|||||||
bool keep_include_path;
|
bool keep_include_path;
|
||||||
bool binary_schema_comments;
|
bool binary_schema_comments;
|
||||||
bool skip_flatbuffers_import;
|
bool skip_flatbuffers_import;
|
||||||
|
std::string go_import;
|
||||||
std::string go_namespace;
|
std::string go_namespace;
|
||||||
bool reexport_ts_modules;
|
bool reexport_ts_modules;
|
||||||
bool protobuf_ascii_alike;
|
bool protobuf_ascii_alike;
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ std::string FlatCompiler::GetUsageString(const char* program_name) const {
|
|||||||
" --no-includes Don\'t generate include statements for included\n"
|
" --no-includes Don\'t generate include statements for included\n"
|
||||||
" schemas the generated file depends on (C++).\n"
|
" schemas the generated file depends on (C++).\n"
|
||||||
" --gen-mutable Generate accessors that can mutate buffers in-place.\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-name-strings Generate type name functions for C++.\n"
|
||||||
" --gen-object-api Generate an additional object-based API.\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"
|
" --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"
|
" --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"
|
" --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n"
|
||||||
" --go-namespace Generate the overrided namespace in Golang.\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"
|
" --raw-binary Allow binaries without file_indentifier to be read.\n"
|
||||||
" This may crash flatc given a mismatched schema.\n"
|
" This may crash flatc given a mismatched schema.\n"
|
||||||
" --proto Input is a .proto, translate to .fbs.\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") {
|
} else if(arg == "--go-namespace") {
|
||||||
if (++argi >= argc) Error("missing golang namespace" + arg, true);
|
if (++argi >= argc) Error("missing golang namespace" + arg, true);
|
||||||
opts.go_namespace = argv[argi];
|
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") {
|
} else if(arg == "--defaults-json") {
|
||||||
opts.output_default_scalars_in_json = true;
|
opts.output_default_scalars_in_json = true;
|
||||||
} else if (arg == "--unknown-json") {
|
} else if (arg == "--unknown-json") {
|
||||||
|
|||||||
@@ -34,6 +34,12 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace flatbuffers {
|
namespace flatbuffers {
|
||||||
|
|
||||||
|
static std::string GeneratedFileName(const std::string &path,
|
||||||
|
const std::string &file_name) {
|
||||||
|
return path + file_name + "_generated.go";
|
||||||
|
}
|
||||||
|
|
||||||
namespace go {
|
namespace go {
|
||||||
|
|
||||||
// see https://golang.org/ref/spec#Keywords
|
// see https://golang.org/ref/spec#Keywords
|
||||||
@@ -752,18 +758,35 @@ class GoGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool generate() {
|
bool generate() {
|
||||||
|
std::string one_file_code;
|
||||||
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
|
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
|
||||||
++it) {
|
++it) {
|
||||||
std::string enumcode;
|
std::string enumcode;
|
||||||
go::GenEnum(**it, &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();
|
for (auto it = parser_.structs_.vec.begin();
|
||||||
it != parser_.structs_.vec.end(); ++it) {
|
it != parser_.structs_.vec.end(); ++it) {
|
||||||
std::string declcode;
|
std::string declcode;
|
||||||
go::GenStruct(**it, &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;
|
return true;
|
||||||
@@ -778,7 +801,11 @@ class GoGenerator : public BaseGenerator {
|
|||||||
code += "package " + name_space_name + "\n\n";
|
code += "package " + name_space_name + "\n\n";
|
||||||
if (needs_imports) {
|
if (needs_imports) {
|
||||||
code += "import (\n";
|
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";
|
code += ")\n\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user