THRIFT-3535 Dart generator argument to produce a file structure usable in parent library
Client: Dart
Patch: Mark Erickson <mark.erickson@workiva.com>
This closes #789
diff --git a/compiler/cpp/src/generate/t_dart_generator.cc b/compiler/cpp/src/generate/t_dart_generator.cc
index 5dfaffe..0853a91 100644
--- a/compiler/cpp/src/generate/t_dart_generator.cc
+++ b/compiler/cpp/src/generate/t_dart_generator.cc
@@ -75,6 +75,15 @@
library_name_ = "";
}
+ iter = parsed_options.find("library_prefix");
+ if (iter != parsed_options.end()) {
+ library_prefix_ = (iter->second) + ".";
+ package_prefix_ = replace_all(library_prefix_, ".", "/");
+ } else {
+ library_prefix_ = "";
+ package_prefix_ = "";
+ }
+
iter = parsed_options.find("pubspec_lib");
if (iter != parsed_options.end()) {
pubspec_lib_ = (iter->second);
@@ -263,6 +272,8 @@
std::ofstream f_service_;
std::string library_name_;
+ std::string library_prefix_;
+ std::string package_prefix_;
std::string pubspec_lib_;
std::string base_dir_;
@@ -285,14 +296,17 @@
string subdir = get_out_dir() + "/" + library_name_;
MKDIR(subdir.c_str());
-
base_dir_ = subdir;
- subdir += "/lib";
- MKDIR(subdir.c_str());
- subdir += "/src";
- MKDIR(subdir.c_str());
- src_dir_ = subdir;
+ if (library_prefix_.empty()) {
+ subdir += "/lib";
+ MKDIR(subdir.c_str());
+ subdir += "/src";
+ MKDIR(subdir.c_str());
+ src_dir_ = subdir;
+ } else {
+ src_dir_ = base_dir_;
+ }
}
string t_dart_generator::find_library_name(t_program* program) {
@@ -311,12 +325,13 @@
* @return String of the library, e.g. "library myservice;"
*/
string t_dart_generator::dart_library(string file_name) {
- string out = "library ";
- if (!library_name_.empty()) {
- out += library_name_;
- }
+ string out = "library " + library_prefix_ + library_name_;
if (!file_name.empty()) {
- out += ".src." + file_name;
+ if (library_prefix_.empty()) {
+ out += ".src." + file_name;
+ } else {
+ out += "." + file_name;
+ }
}
return out + ";\n";
}
@@ -337,14 +352,24 @@
*/
string t_dart_generator::dart_thrift_imports() {
string imports = "import 'dart:typed_data' show Uint8List;" + endl +
- "import 'package:thrift/thrift.dart';" + endl +
- "import 'package:" + library_name_ + "/" + library_name_ + ".dart';" + endl;
+ "import 'package:thrift/thrift.dart';" + endl;
+
+ // add import for this library
+ if (package_prefix_.empty()) {
+ imports += "import 'package:" + library_name_ + "/" + library_name_ + ".dart';" + endl;
+ } else {
+ imports += "import 'package:" + package_prefix_ + library_name_ + ".dart';" + endl;
+ }
// add imports for included thrift files
const vector<t_program*>& includes = program_->get_includes();
for (size_t i = 0; i < includes.size(); ++i) {
string include_name = find_library_name(includes[i]);
- imports += "import 'package:" + include_name + "/" + include_name + ".dart';" + endl;
+ if (package_prefix_.empty()) {
+ imports += "import 'package:" + include_name + "/" + include_name + ".dart';" + endl;
+ } else {
+ imports += "import 'package:" + package_prefix_ + include_name + ".dart';" + endl;
+ }
}
return imports;
@@ -355,23 +380,38 @@
*/
void t_dart_generator::close_generator() {
generate_dart_library();
- generate_dart_pubspec();
+
+ if (library_prefix_.empty()) {
+ generate_dart_pubspec();
+ }
}
void t_dart_generator::generate_dart_library() {
- string f_library_name = base_dir_ + "/lib/" + library_name_ + ".dart";
+ string f_library_name;
+ if (library_prefix_.empty()) {
+ f_library_name = base_dir_ + "/lib/" + library_name_ + ".dart";
+ } else {
+ f_library_name = get_out_dir() + "/" + library_name_ + ".dart";
+ }
+
ofstream f_library;
f_library.open(f_library_name.c_str());
f_library << autogen_comment() << endl;
- f_library << "library " << library_name_ << ";" << endl2;
+ f_library << "library " << library_prefix_ << library_name_ << ";" << endl2;
f_library << library_exports_;
f_library.close();
}
void t_dart_generator::export_class_to_library(string file_name, string class_name) {
- library_exports_ += "export 'src/" + file_name + ".dart' show " + class_name + ";" + endl;
+ string subdir;
+ if (library_prefix_.empty()) {
+ subdir = "src";
+ } else {
+ subdir = library_name_;
+ }
+ library_exports_ += "export '" + subdir + "/" + file_name + ".dart' show " + class_name + ";" + endl;
}
void t_dart_generator::generate_dart_pubspec() {
@@ -2393,6 +2433,8 @@
dart,
"Dart",
" library_name: Optional override for library name.\n"
+ " library_prefix: Generate code that can be used within an existing library.\n"
+ " Use a dot-separated string, e.g. \"my_parent_lib.src.gen\"\n"
" pubspec_lib: Optional override for thrift lib dependency in pubspec.yaml,\n"
" e.g. \"thrift: 0.x.x\". Use a pipe delimiter to separate lines,\n"
" e.g. \"thrift:| git:| url: git@foo.com\"\n"