protobuf 1.4.2 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +4 -0
  2. data/Rakefile +7 -7
  3. data/bin/rpc_server +2 -188
  4. data/bin/rprotoc +44 -38
  5. data/examples/addressbook.pb.rb +2 -2
  6. data/ext/Makefile +11 -0
  7. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
  8. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
  9. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
  10. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
  11. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
  12. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
  13. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
  14. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
  15. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
  16. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
  17. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
  18. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
  19. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
  20. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
  21. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
  22. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
  23. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
  24. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
  25. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
  26. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
  27. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
  28. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
  29. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
  30. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
  31. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
  32. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
  33. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
  34. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
  35. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
  36. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
  37. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
  38. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
  39. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
  40. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
  41. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
  42. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
  43. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
  44. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
  45. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
  46. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
  47. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
  48. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
  49. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
  50. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
  51. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
  52. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
  53. data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
  54. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
  55. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
  56. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
  57. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
  58. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
  59. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
  60. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
  61. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
  62. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
  63. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
  64. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
  65. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
  66. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
  67. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
  68. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
  69. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
  70. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
  71. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
  72. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
  73. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
  74. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
  75. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
  76. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
  77. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
  78. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
  79. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
  80. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
  81. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
  82. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
  83. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
  84. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
  85. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
  86. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
  87. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
  88. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
  89. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
  90. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
  91. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
  92. data/ext/ruby_generator/RubyGenerator.cpp +435 -0
  93. data/ext/ruby_generator/RubyGenerator.h +180 -0
  94. data/ext/ruby_generator/extconf.rb +24 -0
  95. data/lib/protobuf.rb +55 -33
  96. data/lib/protobuf/cli.rb +176 -0
  97. data/lib/protobuf/common/logger.rb +11 -11
  98. data/lib/protobuf/{message/enum.rb → enum.rb} +0 -10
  99. data/lib/protobuf/evented.rb +22 -0
  100. data/lib/protobuf/field.rb +57 -0
  101. data/lib/protobuf/field/base_field.rb +210 -0
  102. data/lib/protobuf/field/bool_field.rb +24 -0
  103. data/lib/protobuf/field/bytes_field.rb +38 -0
  104. data/lib/protobuf/field/double_field.rb +19 -0
  105. data/lib/protobuf/field/enum_field.rb +58 -0
  106. data/lib/protobuf/field/extension_fields.rb +24 -0
  107. data/lib/protobuf/field/field_array.rb +55 -0
  108. data/lib/protobuf/field/fixed32_field.rb +19 -0
  109. data/lib/protobuf/field/fixed64_field.rb +22 -0
  110. data/lib/protobuf/field/float_field.rb +29 -0
  111. data/lib/protobuf/field/int32_field.rb +10 -0
  112. data/lib/protobuf/field/int64_field.rb +10 -0
  113. data/lib/protobuf/field/integer_field.rb +19 -0
  114. data/lib/protobuf/field/message_field.rb +51 -0
  115. data/lib/protobuf/field/sfixed32_field.rb +21 -0
  116. data/lib/protobuf/field/sfixed64_field.rb +24 -0
  117. data/lib/protobuf/field/signed_integer_field.rb +23 -0
  118. data/lib/protobuf/field/sint32_field.rb +10 -0
  119. data/lib/protobuf/field/sint64_field.rb +10 -0
  120. data/lib/protobuf/field/string_field.rb +14 -0
  121. data/lib/protobuf/field/uint32_field.rb +10 -0
  122. data/lib/protobuf/field/uint64_field.rb +10 -0
  123. data/lib/protobuf/field/varint_field.rb +63 -0
  124. data/lib/protobuf/message.rb +376 -0
  125. data/lib/protobuf/message/message.rb +1 -475
  126. data/lib/protobuf/rpc/buffer.rb +1 -1
  127. data/lib/protobuf/rpc/client.rb +26 -26
  128. data/lib/protobuf/rpc/connector.rb +15 -14
  129. data/lib/protobuf/rpc/connectors/eventmachine.rb +13 -13
  130. data/lib/protobuf/rpc/rpc.pb.rb +39 -28
  131. data/lib/protobuf/rpc/server.rb +7 -28
  132. data/lib/protobuf/rpc/servers/evented_runner.rb +15 -15
  133. data/lib/protobuf/rpc/servers/socket/server.rb +2 -1
  134. data/lib/protobuf/rpc/servers/socket_runner.rb +5 -6
  135. data/lib/protobuf/rpc/servers/zmq_runner.rb +4 -5
  136. data/lib/protobuf/socket.rb +22 -0
  137. data/lib/protobuf/version.rb +2 -1
  138. data/lib/protobuf/zmq.rb +21 -0
  139. data/proto/rpc.pb.rb +48 -0
  140. data/protobuf.gemspec +14 -6
  141. data/spec/benchmark/tasks.rb +42 -23
  142. data/spec/functional/evented_server_spec.rb +2 -0
  143. data/spec/functional/socket_server_spec.rb +22 -26
  144. data/spec/functional/zmq_server_spec.rb +22 -26
  145. data/spec/lib/protobuf/cli_spec.rb +182 -0
  146. data/spec/{unit → lib/protobuf}/common/logger_spec.rb +0 -0
  147. data/spec/{unit → lib/protobuf/message}/enum_spec.rb +0 -0
  148. data/spec/{unit → lib/protobuf/message}/message_spec.rb +0 -0
  149. data/spec/{unit → lib/protobuf}/rpc/client_spec.rb +6 -4
  150. data/spec/lib/protobuf/rpc/connector_spec.rb +41 -0
  151. data/spec/{unit → lib/protobuf}/rpc/connectors/base_spec.rb +18 -18
  152. data/spec/{unit → lib/protobuf}/rpc/connectors/common_spec.rb +20 -20
  153. data/spec/{unit → lib/protobuf}/rpc/connectors/socket_spec.rb +9 -8
  154. data/spec/{unit → lib/protobuf}/rpc/connectors/zmq_spec.rb +1 -1
  155. data/spec/{unit → lib/protobuf}/rpc/servers/evented_server_spec.rb +4 -4
  156. data/spec/{unit → lib/protobuf}/rpc/servers/socket_server_spec.rb +11 -23
  157. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/broker_spec.rb +5 -0
  158. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/server_spec.rb +5 -0
  159. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/util_spec.rb +5 -0
  160. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/worker_spec.rb +5 -0
  161. data/spec/{unit → lib/protobuf}/rpc/service_spec.rb +0 -0
  162. data/spec/proto/test.pb.rb +48 -25
  163. data/spec/spec_helper.rb +9 -15
  164. data/spec/support/all.rb +7 -0
  165. data/spec/{helper → support}/server.rb +5 -7
  166. data/spec/{helper → support}/silent_constants.rb +4 -0
  167. data/spec/support/test_app_file.rb +2 -0
  168. data/spec/{helper → support}/tolerance_matcher.rb +0 -0
  169. data/test/proto/types.pb.rb +1 -1
  170. data/test/test_addressbook.rb +1 -1
  171. data/test/test_enum_value.rb +1 -1
  172. data/test/test_standard_message.rb +1 -1
  173. metadata +243 -107
  174. data/lib/protobuf/common/util.rb +0 -59
  175. data/lib/protobuf/compiler/compiler.rb +0 -52
  176. data/lib/protobuf/compiler/nodes.rb +0 -323
  177. data/lib/protobuf/compiler/proto.y +0 -216
  178. data/lib/protobuf/compiler/proto2.ebnf +0 -79
  179. data/lib/protobuf/compiler/proto_parser.rb +0 -1425
  180. data/lib/protobuf/compiler/template/rpc_service_implementation.erb +0 -42
  181. data/lib/protobuf/compiler/visitors.rb +0 -282
  182. data/lib/protobuf/descriptor/descriptor.proto +0 -286
  183. data/lib/protobuf/descriptor/descriptor.rb +0 -55
  184. data/lib/protobuf/descriptor/descriptor_builder.rb +0 -143
  185. data/lib/protobuf/descriptor/descriptor_proto.rb +0 -138
  186. data/lib/protobuf/descriptor/enum_descriptor.rb +0 -33
  187. data/lib/protobuf/descriptor/field_descriptor.rb +0 -49
  188. data/lib/protobuf/descriptor/file_descriptor.rb +0 -37
  189. data/lib/protobuf/message/field.rb +0 -684
  190. data/lib/protobuf/message/protoable.rb +0 -38
  191. data/spec/helper/all.rb +0 -7
  192. data/spec/unit/common/util_spec.rb +0 -17
  193. data/spec/unit/rpc/connector_spec.rb +0 -31
  194. data/spec/unit/rpc/connectors/eventmachine_client_spec.rb +0 -32
  195. data/spec/unit/rpc/connectors/eventmachine_spec.rb +0 -0
  196. data/test/check_unbuild.rb +0 -30
  197. data/test/test_compiler.rb +0 -325
  198. data/test/test_descriptor.rb +0 -122
@@ -0,0 +1,435 @@
1
+ #include "RubyGenerator.h"
2
+
3
+ namespace google {
4
+ namespace protobuf {
5
+ namespace compiler {
6
+ namespace ruby {
7
+
8
+ RubyGenerator::RubyGenerator() {} // Constructor
9
+ RubyGenerator::~RubyGenerator() {} // Destructor
10
+
11
+ // Generate one or more ruby source files for the given proto file.
12
+ bool RubyGenerator::Generate(const FileDescriptor* file,
13
+ const string& parameter,
14
+ GeneratorContext* context,
15
+ string* error) const {
16
+ file_ = file;
17
+ context_ = context;
18
+
19
+ filename = CreateRubyFileName(file_->name());
20
+ ns_vector.clear();
21
+ SplitStringUsing(file_->package(), ".", &ns_vector);
22
+
23
+ // Get a ZeroCopyOutputStream object of the data.
24
+ scoped_ptr<io::ZeroCopyOutputStream> output(context_->Open(filename));
25
+
26
+ // Check the data against Google's proto checking algorithm.
27
+ GOOGLE_CHECK(output.get());
28
+
29
+ // Get a printer.
30
+ io::Printer printer(output.get(), '$');
31
+ printer_ = &printer;
32
+
33
+ PrintGeneratedFileComment();
34
+ PrintGenericRequires();
35
+ PrintImportRequires();
36
+
37
+ PrintEnclosingNamespaceModules();
38
+
39
+ PrintEnumsForFileDescriptor(file_, false);
40
+ PrintNewLine();
41
+ PrintMessagesForFileDescriptor(file_, false);
42
+ PrintNewLine();
43
+
44
+ PrintEnumsForFileDescriptor(file_, true);
45
+ PrintNewLine();
46
+ PrintMessagesForFileDescriptor(file_, true);
47
+ PrintNewLine();
48
+
49
+ PrintServices();
50
+
51
+ PrintEnclosingNamespaceModuleEnds();
52
+
53
+ if (printer.failed()) {
54
+ *error = "An unknown error occured writing file " + filename;
55
+ return false;
56
+ }
57
+ else {
58
+ return true;
59
+ }
60
+ } // end Generate()
61
+
62
+ //
63
+ ///////////////////////////////////////////////// [ namespaces ] //////////////
64
+ //
65
+
66
+ void RubyGenerator::PrintEnclosingNamespaceModules() const {
67
+ PrintNewLine();
68
+ vector<string>::iterator iter;
69
+ map<string,string> data;
70
+ for (iter = ns_vector.begin(); iter < ns_vector.end(); iter++) {
71
+ data["ns"] = Constantize(*iter, false);
72
+
73
+ printer_->Print(data, "module $ns$");
74
+ PrintNewLine();
75
+ printer_->Indent();
76
+ }
77
+ }
78
+
79
+ void RubyGenerator::PrintEnclosingNamespaceModuleEnds() const {
80
+ vector<string>::iterator iter;
81
+ for (iter = ns_vector.begin(); iter < ns_vector.end(); iter++) {
82
+ printer_->Outdent();
83
+ printer_->Print("end");
84
+ PrintNewLine();
85
+ }
86
+ }
87
+
88
+
89
+ //
90
+ ///////////////////////////////////////////////// [ messages ] ////////////////
91
+ //
92
+
93
+ void RubyGenerator::PrintMessagesForFileDescriptor(const FileDescriptor* descriptor, bool print_fields) const {
94
+ if (descriptor->message_type_count() > 0) {
95
+ if (print_fields) {
96
+ PrintComment("Message Fields", true);
97
+ }
98
+ else {
99
+ PrintComment("Message Classes", true);
100
+ }
101
+
102
+ for (int i = 0; i < descriptor->message_type_count(); i++) {
103
+ if (print_fields) {
104
+ PrintMessageFields(descriptor->message_type(i));
105
+ }
106
+ else {
107
+ PrintMessageClass(descriptor->message_type(i));
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ void RubyGenerator::PrintMessagesForDescriptor(const Descriptor* descriptor, bool print_fields) const {
114
+ for (int i = 0; i < descriptor->nested_type_count(); i++) {
115
+ if (print_fields) {
116
+ PrintMessageFields(descriptor->nested_type(i));
117
+ }
118
+ else {
119
+ PrintMessageClass(descriptor->nested_type(i));
120
+ }
121
+ }
122
+ }
123
+ //
124
+ // Print out the given descriptor message as a Ruby class.
125
+ void RubyGenerator::PrintMessageClass(const Descriptor* descriptor) const {
126
+ map<string,string> data;
127
+ data["class_name"] = descriptor->name();
128
+
129
+ printer_->Print(data, "class $class_name$ < ::Protobuf::Message; end");
130
+ PrintNewLine();
131
+
132
+ PrintEnumsForDescriptor(descriptor, false);
133
+ PrintMessagesForDescriptor(descriptor, false);
134
+ }
135
+
136
+ void RubyGenerator::PrintExtensionRangesForDescriptor(const Descriptor* descriptor) const {
137
+ if (descriptor->extension_range_count() > 0) {
138
+ for (int i = 0; i < descriptor->extension_range_count(); i++) {
139
+ const Descriptor::ExtensionRange* range = descriptor->extension_range(i);
140
+ map<string,string> data;
141
+ data["message_class"] = Constantize(descriptor->full_name());
142
+ data["start"] = SimpleItoa(range->start);
143
+ data["end"] = SimpleItoa(range->end);
144
+ printer_->Print(data, "$message_class$.extensions $start$...$end$");
145
+ PrintNewLine();
146
+ }
147
+ }
148
+ }
149
+
150
+ // Print out the given descriptor message as a Ruby class.
151
+ void RubyGenerator::PrintMessageFields(const Descriptor* descriptor) const {
152
+ PrintExtensionRangesForDescriptor(descriptor);
153
+
154
+ if (descriptor->field_count() > 0) {
155
+ for (int i = 0; i < descriptor->field_count(); i++) {
156
+ PrintMessageField(descriptor->field(i));
157
+ }
158
+
159
+ // Print Extension Fields
160
+ if (descriptor->extension_count() > 0) {
161
+ for (int i = 0; i < descriptor->extension_count(); i++) {
162
+ PrintMessageField(descriptor->extension(i));
163
+ }
164
+ }
165
+ PrintNewLine();
166
+ }
167
+
168
+ PrintEnumsForDescriptor(descriptor, true);
169
+ PrintMessagesForDescriptor(descriptor, true);
170
+ }
171
+
172
+ // Print the given FieldDescriptor to the Message DSL methods.
173
+ void RubyGenerator::PrintMessageField(const FieldDescriptor* descriptor) const {
174
+ map<string,string> data;
175
+ data["message_class"] = Constantize(descriptor->containing_type()->full_name());
176
+ data["field_name"] = descriptor->lowercase_name();
177
+ data["tag_number"] = SimpleItoa(descriptor->number());
178
+ data["data_type"] = "";
179
+ data["default_opt"] = "";
180
+ data["packed_opt"] = "";
181
+ data["deprecated_opt"] = "";
182
+ data["extension_opt"] = "";
183
+
184
+ if (descriptor->is_required()) {
185
+ data["field_label"] = "required";
186
+ }
187
+ else if (descriptor->is_optional()) {
188
+ data["field_label"] = "optional";
189
+ }
190
+ else if (descriptor->is_repeated()) {
191
+ data["field_label"] = "repeated";
192
+ }
193
+
194
+ switch (descriptor->type()) {
195
+ // Primitives
196
+ case FieldDescriptor::TYPE_DOUBLE: data["data_type"] = "::Protobuf::Field::DoubleField"; break;
197
+ case FieldDescriptor::TYPE_FLOAT: data["data_type"] = "::Protobuf::Field::FloatField"; break;
198
+ case FieldDescriptor::TYPE_INT64: data["data_type"] = "::Protobuf::Field::Int64Field"; break;
199
+ case FieldDescriptor::TYPE_UINT64: data["data_type"] = "::Protobuf::Field::Uint64Field"; break;
200
+ case FieldDescriptor::TYPE_INT32: data["data_type"] = "::Protobuf::Field::Int32Field"; break;
201
+ case FieldDescriptor::TYPE_FIXED64: data["data_type"] = "::Protobuf::Field::Fixed64Field"; break;
202
+ case FieldDescriptor::TYPE_FIXED32: data["data_type"] = "::Protobuf::Field::Fixed32Field"; break;
203
+ case FieldDescriptor::TYPE_BOOL: data["data_type"] = "::Protobuf::Field::BoolField"; break;
204
+ case FieldDescriptor::TYPE_STRING: data["data_type"] = "::Protobuf::Field::StringField"; break;
205
+ case FieldDescriptor::TYPE_BYTES: data["data_type"] = "::Protobuf::Field::BytesField"; break;
206
+ case FieldDescriptor::TYPE_UINT32: data["data_type"] = "::Protobuf::Field::Uint32Field"; break;
207
+ case FieldDescriptor::TYPE_SFIXED32: data["data_type"] = "::Protobuf::Field::Sfixed32Field"; break;
208
+ case FieldDescriptor::TYPE_SFIXED64: data["data_type"] = "::Protobuf::Field::Sfixed64Field"; break;
209
+ case FieldDescriptor::TYPE_SINT32: data["data_type"] = "::Protobuf::Field::Sint32Field"; break;
210
+ case FieldDescriptor::TYPE_SINT64: data["data_type"] = "::Protobuf::Field::Sint64Field"; break;
211
+
212
+ // Enums
213
+ case FieldDescriptor::TYPE_ENUM:
214
+ data["data_type"] = Constantize(descriptor->enum_type()->full_name());
215
+ break;
216
+
217
+ // Messages
218
+ case FieldDescriptor::TYPE_GROUP:
219
+ case FieldDescriptor::TYPE_MESSAGE:
220
+ default:
221
+ data["data_type"] = Constantize(descriptor->message_type()->full_name());
222
+ break;
223
+ }
224
+
225
+ if (descriptor->has_default_value()) {
226
+ string value;
227
+ switch(descriptor->cpp_type()) {
228
+ case FieldDescriptor::CPPTYPE_INT32: value = SimpleItoa(descriptor->default_value_int32()); break;
229
+ case FieldDescriptor::CPPTYPE_INT64: value = SimpleItoa(descriptor->default_value_int64()); break;
230
+ case FieldDescriptor::CPPTYPE_UINT32: value = SimpleItoa(descriptor->default_value_uint32()); break;
231
+ case FieldDescriptor::CPPTYPE_UINT64: value = SimpleItoa(descriptor->default_value_uint64()); break;
232
+ case FieldDescriptor::CPPTYPE_DOUBLE: value = SimpleDtoa(descriptor->default_value_double()); break;
233
+ case FieldDescriptor::CPPTYPE_FLOAT: value = SimpleFtoa(descriptor->default_value_float()); break;
234
+ case FieldDescriptor::CPPTYPE_BOOL: value = descriptor->default_value_bool() ? "true" : "false"; break;
235
+ case FieldDescriptor::CPPTYPE_ENUM: value = Constantize(descriptor->default_value_enum()->full_name()); break;
236
+ case FieldDescriptor::CPPTYPE_STRING: value = "\"" + descriptor->default_value_string() + "\""; break;
237
+ default: break;
238
+ }
239
+ data["default_opt"] = strings::Substitute(", :default => $0", value);
240
+ }
241
+
242
+ if (descriptor->is_packable() && descriptor->options().has_packed()) {
243
+ string packed_bool = descriptor->options().packed() ? "true" : "false";
244
+ data["packed_opt"] = strings::Substitute(", :packed => $0", packed_bool);
245
+ }
246
+
247
+ if (descriptor->options().has_deprecated()) {
248
+ string deprecated_bool = descriptor->options().deprecated() ? "true" : "false";
249
+ data["deprecated_opt"] = strings::Substitute(", :deprecated => $0", deprecated_bool);
250
+ }
251
+
252
+ if (descriptor->is_extension()) {
253
+ data["extension_opt"] = ", :extension => true";
254
+ }
255
+
256
+ printer_->Print(data,
257
+ "$message_class$.$field_label$("
258
+ "$data_type$, "
259
+ ":$field_name$, "
260
+ "$tag_number$"
261
+ "$default_opt$"
262
+ "$packed_opt$"
263
+ "$deprecated_opt$"
264
+ "$extension_opt$"
265
+ ")\n");
266
+ }
267
+
268
+
269
+ //
270
+ ///////////////////////////////////////////////// [ enums ] ///////////////////
271
+ //
272
+
273
+ void RubyGenerator::PrintEnumsForDescriptor(const Descriptor* descriptor, bool print_values) const {
274
+ for (int i = 0; i < descriptor->enum_type_count(); i++) {
275
+ if (print_values) {
276
+ PrintEnumValues(descriptor->enum_type(i));
277
+ }
278
+ else {
279
+ PrintEnumClass(descriptor->enum_type(i));
280
+ }
281
+ }
282
+ }
283
+
284
+ void RubyGenerator::PrintEnumsForFileDescriptor(const FileDescriptor* descriptor, bool print_values) const {
285
+ if (descriptor->enum_type_count() > 0) {
286
+ if (print_values) {
287
+ PrintComment("Enum Values", true);
288
+ }
289
+ else {
290
+ PrintComment("Enum Classes", true);
291
+ }
292
+
293
+ for (int i = 0; i < descriptor->enum_type_count(); i++) {
294
+ if (print_values) {
295
+ PrintEnumValues(descriptor->enum_type(i));
296
+ }
297
+ else {
298
+ PrintEnumClass(descriptor->enum_type(i));
299
+ }
300
+ }
301
+ }
302
+ }
303
+
304
+ // Print the given enum descriptor as a Ruby class.
305
+ void RubyGenerator::PrintEnumClass(const EnumDescriptor* descriptor) const {
306
+ map<string,string> data;
307
+ data["class_name"] = descriptor->name();
308
+ printer_->Print(data, "class $class_name$ < ::Protobuf::Enum; end");
309
+ PrintNewLine();
310
+ }
311
+
312
+ // Print the given enum descriptor as a Ruby class.
313
+ void RubyGenerator::PrintEnumValues(const EnumDescriptor* descriptor) const {
314
+ for (int i = 0; i < descriptor->value_count(); i++) {
315
+ PrintEnumValue(descriptor->value(i));
316
+ }
317
+ PrintNewLine();
318
+ }
319
+
320
+ // Print the given enum value to the Enum class DSL methods.
321
+ void RubyGenerator::PrintEnumValue(const EnumValueDescriptor* descriptor) const {
322
+ map<string,string> data;
323
+ data["enum_class"] = Constantize(descriptor->type()->full_name());
324
+ data["name"] = descriptor->name();
325
+ data["number"] = ConvertIntToString(descriptor->number());
326
+ printer_->Print(data, "$enum_class$.define :$name$, $number$\n");
327
+ }
328
+
329
+ //
330
+ ///////////////////////////////////////////////// [ services ] ////////////////
331
+ //
332
+
333
+ void RubyGenerator::PrintServices() const {
334
+ if (file_->service_count() > 0) {
335
+ PrintComment("Services", true);
336
+ for (int i = 0; i < file_->service_count(); i++) {
337
+ PrintService(file_->service(i));
338
+ }
339
+ }
340
+ }
341
+
342
+ // Print the given service as a Ruby class.
343
+ void RubyGenerator::PrintService(const ServiceDescriptor* descriptor) const {
344
+ map<string,string> data;
345
+ data["class_name"] = descriptor->name();
346
+
347
+ printer_->Print(data, "class $class_name$ < ::Protobuf::Rpc::Service");
348
+ PrintNewLine();
349
+ printer_->Indent();
350
+
351
+ for (int i = 0; i < descriptor->method_count(); i++) {
352
+ PrintServiceMethod(descriptor->method(i));
353
+ }
354
+
355
+ printer_->Outdent();
356
+ printer_->Print("end");
357
+ PrintNewLine();
358
+ }
359
+
360
+ // Print the rpc DSL declaration to the Ruby service class.
361
+ void RubyGenerator::PrintServiceMethod(const MethodDescriptor* descriptor) const {
362
+ map<string,string> data;
363
+ data["name"] = Underscore(descriptor->name());
364
+ data["request_klass"] = Constantize(descriptor->input_type()->full_name());
365
+ data["response_klass"] = Constantize(descriptor->output_type()->full_name());
366
+ printer_->Print(data, "rpc :$name$, $request_klass$, $response_klass$");
367
+ PrintNewLine();
368
+ }
369
+
370
+
371
+ //
372
+ ///////////////////////////////////////////////// [ general ] ////////////////
373
+ //
374
+
375
+ // Print a header or one-line comment (as indicated by the as_header bool).
376
+ void RubyGenerator::PrintComment(string comment, bool as_header) const {
377
+ char format[] = "# $comment$\n";
378
+ char format_multi[] = "##\n# $comment$\n#\n";
379
+
380
+ map<string,string> data;
381
+ data["comment"] = comment;
382
+
383
+ if (as_header) {
384
+ printer_->Print(data, format_multi);
385
+ }
386
+ else {
387
+ printer_->Print(data, format);
388
+ }
389
+ }
390
+
391
+ // Prints a require with the given ruby library.
392
+ void RubyGenerator::PrintRequire(string lib_name) const {
393
+ map<string,string> data;
394
+ data["lib"] = lib_name;
395
+ printer_->Print(data, "require '$lib$'\n");
396
+ }
397
+
398
+ // Print a comment indicating that the file was generated.
399
+ void RubyGenerator::PrintGeneratedFileComment() const {
400
+ PrintComment("This file is auto-generated. DO NOT EDIT!", true);
401
+ }
402
+
403
+ // Print out message requires as they pertain to the ruby library.
404
+ void RubyGenerator::PrintGenericRequires() const {
405
+ if (file_->message_type_count() > 0) {
406
+ PrintRequire("protobuf/message");
407
+ }
408
+ if (file_->service_count() > 0) {
409
+ PrintRequire("protobuf/rpc/service");
410
+ }
411
+ }
412
+
413
+ void RubyGenerator::PrintImportRequires() const {
414
+ if (file_->dependency_count() > 0) {
415
+ PrintNewLine();
416
+ PrintComment("Imports", true);
417
+ for (int i = 0; i < file_->dependency_count(); i++) {
418
+ PrintRequire(CreateRubyFileName(file_->dependency(i)->name(), true));
419
+ }
420
+ }
421
+ }
422
+
423
+ // Print a one-line comment.
424
+ void RubyGenerator::PrintComment(string comment) const {
425
+ PrintComment(comment, false);
426
+ }
427
+
428
+ void RubyGenerator::PrintNewLine() const {
429
+ printer_->Print("\n");
430
+ }
431
+
432
+ } // namespace ruby
433
+ } // namespace compiler
434
+ } // namespace protobuf
435
+ } // namespace google
@@ -0,0 +1,180 @@
1
+ #ifndef GOOGLE_PROTOBUF_COMPILER_RUBY_GENERATOR_H
2
+ #define GOOGLE_PROTOBUF_COMPILER_RUBY_GENERATOR_H
3
+
4
+ #include <string>
5
+ #include <iostream>
6
+ #include <sstream>
7
+ #include <vector>
8
+
9
+ #include <google/protobuf/compiler/command_line_interface.h>
10
+ #include <google/protobuf/compiler/code_generator.h>
11
+ #include <google/protobuf/descriptor.h>
12
+ #include <google/protobuf/descriptor.pb.h>
13
+ #include <google/protobuf/io/zero_copy_stream.h>
14
+ #include <google/protobuf/io/printer.h>
15
+ #include <google/protobuf/stubs/substitute.h>
16
+ #include <google/protobuf/stubs/strutil.h>
17
+
18
+ namespace google {
19
+ namespace protobuf {
20
+ namespace compiler {
21
+ namespace ruby {
22
+
23
+ class LIBPROTOC_EXPORT RubyGenerator : public CodeGenerator {
24
+ public:
25
+ RubyGenerator();
26
+ ~RubyGenerator();
27
+
28
+ // implemented Generate method from parent
29
+ bool Generate(const FileDescriptor* file,
30
+ const string& parameter,
31
+ GeneratorContext* context,
32
+ string* error) const;
33
+
34
+
35
+ private:
36
+ mutable GeneratorContext* context_;
37
+ mutable io::Printer* printer_;
38
+ mutable const FileDescriptor* file_;
39
+ mutable string filename;
40
+ mutable vector<string> ns_vector;
41
+
42
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RubyGenerator);
43
+
44
+ void PrintEnclosingNamespaceModules() const;
45
+ void PrintEnclosingNamespaceModuleEnds() const;
46
+
47
+ void PrintMessagesForDescriptor(const Descriptor* descriptor, bool print_fields) const;
48
+ void PrintMessagesForFileDescriptor(const FileDescriptor* descriptor, bool print_fields) const;
49
+ void PrintMessageClass(const Descriptor* descriptor) const;
50
+ void PrintExtensionRangesForDescriptor(const Descriptor* descriptor) const;
51
+ void PrintMessageFields(const Descriptor* descriptor) const;
52
+ void PrintMessageField(const FieldDescriptor* descriptor) const;
53
+
54
+ void PrintEnumsForDescriptor(const Descriptor* descriptor, bool print_values) const;
55
+ void PrintEnumsForFileDescriptor(const FileDescriptor* descriptor, bool print_values) const;
56
+ void PrintEnumClass(const EnumDescriptor* descriptor) const;
57
+ void PrintEnumValues(const EnumDescriptor* descriptor) const;
58
+ void PrintEnumValue(const EnumValueDescriptor* descriptor) const;
59
+
60
+ void PrintServices() const;
61
+ void PrintService(const ServiceDescriptor* descriptor) const;
62
+ void PrintServiceMethod(const MethodDescriptor* descriptor) const;
63
+
64
+ void PrintGeneratedFileComment() const;
65
+ void PrintGenericRequires() const;
66
+ void PrintImportRequires() const;
67
+ void PrintComment(string comment) const;
68
+ void PrintComment(string comment, bool as_header) const;
69
+ void PrintRequire(string lib_name) const;
70
+ void PrintNewLine() const;
71
+
72
+ // Take the proto file name, strip ".proto"
73
+ // from the end and add ".pb.rb"
74
+ static string CreateRubyFileName(const string proto_filename) {
75
+ return CreateRubyFileName(proto_filename, false);
76
+ }
77
+
78
+ static string CreateRubyFileName(const string proto_filename, bool for_require) {
79
+ string replacement = for_require ? ".pb" : ".pb.rb";
80
+ return StringReplace(proto_filename, ".proto", replacement, false);
81
+ }
82
+
83
+ static string ConvertIntToString(int number) {
84
+ stringstream stream;
85
+ stream << number;
86
+ return stream.str();
87
+ }
88
+
89
+ static string ConvertDoubleToString(double number) {
90
+ stringstream stream;
91
+ stream << number;
92
+ return stream.str();
93
+ }
94
+
95
+ static string ConvertFloatToString(float number) {
96
+ stringstream stream;
97
+ stream << number;
98
+ return stream.str();
99
+ }
100
+
101
+ static string Constantize(string full_path) {
102
+ return Constantize(full_path, true);
103
+ }
104
+
105
+ static string Constantize(string full_path, bool is_top_level) {
106
+ stringstream constantized;
107
+
108
+ if (is_top_level) {
109
+ constantized << "::";
110
+ }
111
+
112
+ string::iterator i;
113
+ bool segment_end = true;
114
+ for (i = full_path.begin(); i < full_path.end(); i++) {
115
+ char c = *i;
116
+ if (c == 46) { // period char
117
+ constantized << ':' << ':';
118
+ segment_end = true;
119
+ continue;
120
+ }
121
+ else if (c == 95) { // underscore char
122
+ segment_end = true;
123
+ continue;
124
+ }
125
+ else if (segment_end) {
126
+ if (c >= 97 && c <= 122) { // a-z chars
127
+ c -= 32;
128
+ }
129
+ segment_end = false;
130
+ }
131
+ constantized << c;
132
+ }
133
+
134
+ return constantized.str();
135
+ }
136
+
137
+ static string Underscore(string name) {
138
+ stringstream underscored;
139
+
140
+ string::iterator i;
141
+ for (i = name.begin(); i < name.end(); i++) {
142
+ char c = *i;
143
+ if (c >= 65 && c <= 90) { // a-z chars
144
+ if (i != name.begin()) {
145
+ underscored << '_';
146
+ }
147
+ c += 32;
148
+ }
149
+ underscored << c;
150
+ }
151
+
152
+ return underscored.str();
153
+ }
154
+
155
+ }; // class RubyGenerator
156
+
157
+ } // namespace ruby
158
+ } // namespace compiler
159
+ } // namespace protobuf
160
+ } // namespace google
161
+
162
+ #ifdef __cplusplus
163
+ extern "C" {
164
+ #endif
165
+
166
+ int _rprotoc_extern(int argc, char* argv[]) {
167
+ google::protobuf::compiler::CommandLineInterface cli;
168
+
169
+ google::protobuf::compiler::ruby::RubyGenerator ruby_generator;
170
+ cli.RegisterGenerator("--ruby_out", &ruby_generator,
171
+ "Generate Ruby-compatible protobuf message and service classes.");
172
+
173
+ return cli.Run(argc, argv);
174
+ }
175
+
176
+ #ifdef __cplusplus
177
+ }
178
+ #endif
179
+
180
+ #endif // GOOGLE_PROTOBUF_COMPILER_RUBY_GENERATOR_H