Skip to content

Instantly share code, notes, and snippets.

@skuruppu
Created September 1, 2013 06:14
Show Gist options
  • Save skuruppu/5a24615b2c579aedaf68 to your computer and use it in GitHub Desktop.
Save skuruppu/5a24615b2c579aedaf68 to your computer and use it in GitHub Desktop.
Patch for generating Require.js compatible JavaScript modules with the Thrift compiler. Currently only generates code for enums and constants.
diff --git a/compiler/cpp/src/generate/t_js_generator.cc b/compiler/cpp/src/generate/t_js_generator.cc
index 565bb80..1f92444 100644
--- a/compiler/cpp/src/generate/t_js_generator.cc
+++ b/compiler/cpp/src/generate/t_js_generator.cc
@@ -60,6 +60,9 @@ class t_js_generator : public t_oop_generator {
iter = parsed_options.find("jquery");
gen_jquery_ = (iter != parsed_options.end());
+ iter = parsed_options.find("require");
+ gen_require_ = (iter != parsed_options.end());
+
if (gen_node_) {
out_dir_base_ = "gen-nodejs";
} else {
@@ -244,9 +247,15 @@ class t_js_generator : public t_oop_generator {
bool gen_jquery_;
/**
+ * True if we should generate services compatible with require.js.
+ */
+ bool gen_require_;
+
+ /**
* File streams
*/
std::ofstream f_types_;
+ std::ostringstream f_types_s_;
std::ofstream f_service_;
};
@@ -299,6 +308,8 @@ void t_js_generator::init_generator() {
string t_js_generator::js_includes() {
if (gen_node_) {
return string("var Thrift = require('thrift').Thrift;");
+ } else if (gen_require_) {
+ return string("define([\"thrift\"], function(Thrift) {");
}
string inc;
@@ -331,6 +342,13 @@ string t_js_generator::render_includes() {
void t_js_generator::close_generator() {
// Close types file
+ // Close off the require friendly file
+ if (gen_require_) {
+ f_types_ << endl << "return {" << endl
+ << f_types_s_.str() << "}" << endl
+ << endl << "});" << endl;
+ }
+
f_types_.close();
}
@@ -364,6 +382,16 @@ void t_js_generator::generate_enum(t_enum* tenum) {
}
f_types_ << "};"<<endl;
+
+ if (gen_require_) {
+ indent_up();
+ f_types_s_ << indent()
+ << js_type_namespace(tenum->get_program())<<tenum->get_name()
+ << ": "
+ << js_type_namespace(tenum->get_program())<<tenum->get_name()
+ << "," << endl;
+ indent_down();
+ }
}
/**
@@ -376,6 +404,16 @@ void t_js_generator::generate_const(t_const* tconst) {
f_types_ << js_type_namespace(program_) << name << " = ";
f_types_ << render_const_value(type, value) << ";" << endl;
+
+ if (gen_require_) {
+ indent_up();
+ f_types_s_ << indent()
+ << js_type_namespace(program_) << name
+ << ": "
+ << js_type_namespace(program_) << name
+ << "," << endl;
+ indent_down();
+ }
}
/**
@@ -501,6 +539,10 @@ void t_js_generator::generate_xception(t_struct* txception) {
*/
void t_js_generator::generate_js_struct(t_struct* tstruct,
bool is_exception) {
+ // Not generating structs for require friendly JS modules yet
+ if (gen_require_) {
+ return;
+ }
generate_js_struct_definition(f_types_, tstruct, is_exception);
}
@@ -742,6 +784,11 @@ void t_js_generator::generate_js_struct_writer(ofstream& out,
* @param tservice The service definition
*/
void t_js_generator::generate_service(t_service* tservice) {
+ // Not generating services for require friendly JS modules yet
+ if (gen_require_) {
+ return;
+ }
+
string f_service_name = get_out_dir()+service_name_+".js";
f_service_.open(f_service_name.c_str());
@@ -1812,5 +1859,6 @@ string t_js_generator ::type_to_enum(t_type* type) {
THRIFT_REGISTER_GENERATOR(js, "Javascript",
" jquery: Generate jQuery compatible code.\n"
-" node: Generate node.js compatible code.\n")
+" node: Generate node.js compatible code.\n"
+" require: Generate require.js compatible code.\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment