protobuf-closure-library 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.rst CHANGED
@@ -10,9 +10,9 @@ Installation
10
10
 
11
11
  `Protobuf <http://code.google.com/p/protobuf/>`_ runtime compiler and development header and libraries must be present at gem installation time.
12
12
 
13
- * Under linux, user the right development package, eg ``libprotobuf-dev`` for ubuntu.
13
+ * Under linux, user the right development package, eg ``libprotoc-dev`` for ubuntu.
14
14
  * Under osx, use ``brew install protobuf``.
15
- * under windows, use the `official protobuf binaries <http://code.google.com/p/protobuf/downloads/list>`_.
15
+ * Under windows, use the `official protobuf binaries <http://code.google.com/p/protobuf/downloads/list>`_.
16
16
 
17
17
  Then, install the gem:
18
18
 
@@ -27,6 +27,13 @@ Usage
27
27
 
28
28
  require 'protobuf-closure-library'
29
29
  ProtobufClosureLibrary::ProtocJs.compile input_proto_file,
30
- output_dir, optional_protoc_arg, ...
30
+ output_dir, {
31
+ generator_options: {key: value},
32
+ protoc_options: []
33
+ }
31
34
 
32
35
  This generated a ``.pb.js`` file correponded to ``input_proto_file`` in ``output_dir``. The output file contains a subclass of `goog.proto2.Message <http://closure-library.googlecode.com/svn/docs/class_goog_proto2_Message.html>`_.
36
+
37
+ By default, the generated message classes are subclasses of ``goog.proto2.Message``. This can be overriden by using ``generator_options: {js_superclass: 'cusom_package.CustomClass'}`` which generates message classes with ``cusom_package.CustomClass`` as the superclass.
38
+
39
+ Other protoc options can be passed to the compiler by ``protoc_options`` key.
@@ -17,6 +17,7 @@
17
17
  #include "code_generator.h"
18
18
 
19
19
  #include <string>
20
+ #include <vector>
20
21
  #include <iostream> // NOLINT
21
22
  #include <sstream> // NOLINT
22
23
 
@@ -37,12 +38,29 @@ CodeGenerator::CodeGenerator(const std::string &name)
37
38
 
38
39
  CodeGenerator::~CodeGenerator() {}
39
40
 
41
+ std::string CodeGenerator::js_superclass_ = "goog.proto2.Message";
42
+ bool CodeGenerator::advanced_ = false;
43
+
40
44
  bool CodeGenerator::Generate(
41
45
  const google::protobuf::FileDescriptor *file,
42
- const std::string &/* parameter */,
46
+ const std::string & parameter ,
43
47
  google::protobuf::compiler::OutputDirectory *output_directory,
44
48
  std::string *error) const {
45
49
 
50
+ std::vector<std::pair<std::string, std::string> > options;
51
+ google::protobuf::compiler::ParseGeneratorParameter(parameter, &options);
52
+
53
+ for (unsigned int i = 0; i < options.size(); i++) {
54
+ if (options[i].first == "js_superclass") {
55
+ CodeGenerator::js_superclass_ = options[i].second;
56
+ } else if (options[i].first == "advanced" && options[i].second == "true") {
57
+ CodeGenerator::advanced_ = true;
58
+ } else {
59
+ *error = "Unknown generator option: " + options[i].first;
60
+ return false;
61
+ }
62
+ }
63
+
46
64
  const std::string file_name = file->name();
47
65
  std::string output_file_name = file->name();
48
66
  std::size_t loc = output_file_name.rfind(".");
@@ -76,8 +94,8 @@ bool CodeGenerator::Generate(
76
94
  }
77
95
 
78
96
  printer.Print("\n");
79
- printer.Print("goog.require('goog.proto2.Message');\n"
80
- "\n");
97
+ printer.Print("goog.require('$js_superclass$');\n"
98
+ "\n", "js_superclass", CodeGenerator::js_superclass_);
81
99
  for (int i = 0; i < file->dependency_count(); ++i) {
82
100
  for (int j = 0; j < file->dependency(i)->message_type_count(); j++) {
83
101
  printer.Print(
@@ -172,30 +190,33 @@ void CodeGenerator::GenDescriptor(
172
190
  "/**\n"
173
191
  " * Message $name$.\n"
174
192
  " * @constructor\n"
175
- " * @extends {goog.proto2.Message}\n"
193
+ " * @extends {$js_superclass$}\n"
176
194
  " */\n",
177
- "name", message->name());
195
+ "name", message->name(),
196
+ "js_superclass", CodeGenerator::js_superclass_);
178
197
  printer->Print("$name$ = function() {\n",
179
198
  "name", JsFullName(message->file(),
180
199
  message->full_name()));
181
200
  printer->Indent();
182
- printer->Print("goog.proto2.Message.apply(this);\n");
201
+ printer->Print("$js_superclass$.apply(this);\n", "js_superclass", CodeGenerator::js_superclass_);
183
202
  printer->Outdent();
184
203
  printer->Print("};\n"
185
- "goog.inherits($name$, goog.proto2.Message);\n"
204
+ "goog.inherits($name$, $js_superclass$);\n"
186
205
  "\n"
187
206
  "\n",
188
207
  "name", JsFullName(message->file(),
189
- message->full_name()));
208
+ message->full_name()),
209
+ "js_superclass", CodeGenerator::js_superclass_);
190
210
 
191
211
  printer->Print(
192
212
  "/**\n"
193
- " * Overrides {@link goog.proto2.Message#clone} to specify its exact "
213
+ " * Overrides {@link $js_superclass$#clone} to specify its exact "
194
214
  "return type.\n"
195
215
  " * @return {!$name$} The cloned message.\n"
196
216
  " * @override\n"
197
217
  " */\n"
198
218
  "$name$.prototype.clone;\n",
219
+ "js_superclass", CodeGenerator::js_superclass_,
199
220
  "name", JsFullName(message->file(),
200
221
  message->full_name()));
201
222
 
@@ -393,16 +414,17 @@ void CodeGenerator::GenFieldDescriptor(
393
414
  " * Sets the value of the $name$ field.\n",
394
415
  "name", field->name());
395
416
  printer->Print(" * @param {$opt$$type$} value The value.\n"
417
+ " * @param {Object} opt_options Options.\n"
396
418
  " */\n",
397
419
  "opt", type_is_primitive ? "" : "!",
398
420
  "type", type);
399
- printer->Print("$prefix$.prototype.set$field$ = function(value) {\n",
421
+ printer->Print("$prefix$.prototype.set$field$ = function(value, opt_options) {\n",
400
422
  "prefix", JsFullName(field->containing_type()->file(),
401
423
  field->containing_type()->full_name()),
402
424
  "field", upper_name);
403
425
  printer->Indent();
404
426
  printer->Print(
405
- "this.set$$Value($number$, value);\n",
427
+ "this.set$$Value($number$, value, opt_options);\n",
406
428
  "number", number.str());
407
429
  printer->Outdent();
408
430
  printer->Print("};\n"
@@ -508,6 +530,61 @@ void CodeGenerator::GenFieldDescriptor(
508
530
  "number", number.str());
509
531
  printer->Outdent();
510
532
  printer->Print("};\n");
533
+
534
+ if (CodeGenerator::advanced_) {
535
+ // update events
536
+ printer->Print("\n"
537
+ "/**\n"
538
+ " * Listens to update event on $name$ field.\n"
539
+ " * @param {function} callback The callback to invoke.\n"
540
+ " * @param {Object} context The 'this' sent to callback..\n"
541
+ " */\n",
542
+ "name", field->name());
543
+ printer->Print("$prefix$.prototype.onUpdate$field$ = function(callback, context) {\n",
544
+ "prefix", JsFullName(field->containing_type()->file(),
545
+ field->containing_type()->full_name()),
546
+ "field", upper_name);
547
+ printer->Indent();
548
+ printer->Print("this.listen('update:$number$', callback, context);\n",
549
+ "number", number.str());
550
+ printer->Outdent();
551
+ printer->Print("};\n");
552
+ printer->Print("\n"
553
+ "/**\n"
554
+ " * Clears update event on $name$ field.\n"
555
+ " */\n",
556
+ "name", field->name());
557
+ printer->Print("$prefix$.prototype.forgetUpdate$field$ = function() {\n",
558
+ "prefix", JsFullName(field->containing_type()->file(),
559
+ field->containing_type()->full_name()),
560
+ "field", upper_name);
561
+ printer->Indent();
562
+ printer->Print("this.forget('update:$number$');\n",
563
+ "number", number.str());
564
+ printer->Outdent();
565
+ printer->Print("};\n");
566
+
567
+ // collections
568
+ printer->Print("\n"
569
+ "/**\n"
570
+ " * Returns the collection in the $name$ field.\n",
571
+ "name", field->name());
572
+ printer->Print(
573
+ " * @return {Object} The values in the field.\n"
574
+ " */\n");
575
+ printer->Print("$prefix$.prototype.$field$Collection = function() {\n",
576
+ "prefix", JsFullName(field->containing_type()->file(),
577
+ field->containing_type()->full_name()),
578
+ "field", field->camelcase_name());
579
+ printer->Indent();
580
+ printer->Print(
581
+ "return (this.collection$$Values($number$));"
582
+ "\n",
583
+ "number", number.str());
584
+ printer->Outdent();
585
+ printer->Print("};\n"
586
+ "\n");
587
+ } // if (CodeGenerator::advanced_)
511
588
  }
512
589
 
513
590
  void CodeGenerator::GenEnumDescriptor(
@@ -546,7 +623,8 @@ void CodeGenerator::GenDescriptorMetadata(
546
623
  google::protobuf::io::Printer *printer) {
547
624
  printer->Print("\n"
548
625
  "\n"
549
- "goog.proto2.Message.set$$Metadata($name$, {\n",
626
+ "$js_superclass$.set$$Metadata($name$, {\n",
627
+ "js_superclass", CodeGenerator::js_superclass_,
550
628
  "name", JsFullName(message->file(),
551
629
  message->full_name()));
552
630
  printer->Indent();
@@ -714,7 +792,8 @@ void CodeGenerator::GenFieldDescriptorMetadata(
714
792
  if (field->is_repeated()) {
715
793
  printer->Print("repeated: true,\n");
716
794
  }
717
- printer->Print("fieldType: goog.proto2.Message.FieldType.$js_type$,\n",
795
+ printer->Print("fieldType: $js_superclass$.FieldType.$js_type$,\n",
796
+ "js_superclass", CodeGenerator::js_superclass_,
718
797
  "js_type", js_type);
719
798
  if (field->has_default_value() ||
720
799
  field->type() == google::protobuf::FieldDescriptor::TYPE_ENUM) {
@@ -40,6 +40,8 @@ class CodeGenerator : public ::google::protobuf::compiler::CodeGenerator {
40
40
 
41
41
  private:
42
42
  std::string name_;
43
+ static std::string js_superclass_;
44
+ static bool advanced_;
43
45
 
44
46
  static std::string JsFullName(
45
47
  const google::protobuf::FileDescriptor *file,
@@ -1,12 +1,24 @@
1
1
  module ProtobufClosureLibrary
2
2
 
3
3
  class ProtocJs
4
- def self.compile proto_file, out_dir, *args
4
+ def self.compile proto_file, out_dir, options
5
+ options = {
6
+ generator_options: {},
7
+ protoc_options: []
8
+ }.merge options
9
+
10
+ if !File.exist? out_dir
11
+ FileUtils.mkdir_p out_dir
12
+ end
13
+
14
+ generator_options = options[:generator_options].map{|k, v| "#{k}=#{v}" }.join(',')
15
+ js_out_options = generator_options.empty? ? out_dir : "#{generator_options}:#{out_dir}"
16
+
5
17
  ProtocJsCore.compile RUBY_BIN, proto_file,
6
18
  @@static_proto_path | [
7
- "--js_out=#{out_dir}",
19
+ "--js_out=#{js_out_options}",
8
20
  "--proto_path=#{File.dirname proto_file}"
9
- ] | args
21
+ ] | options[:protoc_options]
10
22
  end
11
23
 
12
24
  private
@@ -1,3 +1,3 @@
1
1
  module ProtobufClosureLibrary
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-closure-library
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-13 00:00:00.000000000 Z
12
+ date: 2012-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rice
16
- requirement: &2169520160 !ruby/object:Gem::Requirement
16
+ requirement: &2157106240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2169520160
24
+ version_requirements: *2157106240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: shoulda
27
- requirement: &2169519740 !ruby/object:Gem::Requirement
27
+ requirement: &2157105620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2169519740
35
+ version_requirements: *2157105620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake-compiler
38
- requirement: &2169519320 !ruby/object:Gem::Requirement
38
+ requirement: &2157105140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2169519320
46
+ version_requirements: *2157105140
47
47
  description: ! " A wrapper for native protocol buffer javascript compiler\n which
48
48
  generates closure library proto2 messages.\n"
49
49
  email:
@@ -85,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
85
  version: '0'
86
86
  segments:
87
87
  - 0
88
- hash: 1293859870650302432
88
+ hash: 3792040639640788879
89
89
  required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  none: false
91
91
  requirements:
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  version: '0'
95
95
  segments:
96
96
  - 0
97
- hash: 1293859870650302432
97
+ hash: 3792040639640788879
98
98
  requirements: []
99
99
  rubyforge_project: protobuf-closure-library
100
100
  rubygems_version: 1.8.10