protobuf-cucumber 3.10.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.rubocop.yml +70 -0
  4. data/.rubocop_todo.yml +145 -0
  5. data/.travis.yml +40 -0
  6. data/.yardopts +5 -0
  7. data/CHANGES.md +344 -0
  8. data/CONTRIBUTING.md +16 -0
  9. data/Gemfile +3 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +33 -0
  12. data/Rakefile +64 -0
  13. data/bin/protoc-gen-ruby +22 -0
  14. data/bin/rpc_server +5 -0
  15. data/install-protobuf.sh +28 -0
  16. data/lib/protobuf.rb +129 -0
  17. data/lib/protobuf/cli.rb +257 -0
  18. data/lib/protobuf/code_generator.rb +120 -0
  19. data/lib/protobuf/decoder.rb +28 -0
  20. data/lib/protobuf/deprecation.rb +117 -0
  21. data/lib/protobuf/descriptors.rb +3 -0
  22. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +62 -0
  23. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +301 -0
  24. data/lib/protobuf/encoder.rb +11 -0
  25. data/lib/protobuf/enum.rb +365 -0
  26. data/lib/protobuf/exceptions.rb +9 -0
  27. data/lib/protobuf/field.rb +74 -0
  28. data/lib/protobuf/field/base_field.rb +380 -0
  29. data/lib/protobuf/field/base_field_object_definitions.rb +504 -0
  30. data/lib/protobuf/field/bool_field.rb +64 -0
  31. data/lib/protobuf/field/bytes_field.rb +78 -0
  32. data/lib/protobuf/field/double_field.rb +25 -0
  33. data/lib/protobuf/field/enum_field.rb +61 -0
  34. data/lib/protobuf/field/field_array.rb +104 -0
  35. data/lib/protobuf/field/field_hash.rb +122 -0
  36. data/lib/protobuf/field/fixed32_field.rb +25 -0
  37. data/lib/protobuf/field/fixed64_field.rb +28 -0
  38. data/lib/protobuf/field/float_field.rb +43 -0
  39. data/lib/protobuf/field/int32_field.rb +21 -0
  40. data/lib/protobuf/field/int64_field.rb +34 -0
  41. data/lib/protobuf/field/integer_field.rb +23 -0
  42. data/lib/protobuf/field/message_field.rb +51 -0
  43. data/lib/protobuf/field/sfixed32_field.rb +27 -0
  44. data/lib/protobuf/field/sfixed64_field.rb +28 -0
  45. data/lib/protobuf/field/signed_integer_field.rb +29 -0
  46. data/lib/protobuf/field/sint32_field.rb +21 -0
  47. data/lib/protobuf/field/sint64_field.rb +21 -0
  48. data/lib/protobuf/field/string_field.rb +51 -0
  49. data/lib/protobuf/field/uint32_field.rb +21 -0
  50. data/lib/protobuf/field/uint64_field.rb +21 -0
  51. data/lib/protobuf/field/varint_field.rb +77 -0
  52. data/lib/protobuf/generators/base.rb +85 -0
  53. data/lib/protobuf/generators/enum_generator.rb +39 -0
  54. data/lib/protobuf/generators/extension_generator.rb +27 -0
  55. data/lib/protobuf/generators/field_generator.rb +193 -0
  56. data/lib/protobuf/generators/file_generator.rb +262 -0
  57. data/lib/protobuf/generators/group_generator.rb +122 -0
  58. data/lib/protobuf/generators/message_generator.rb +104 -0
  59. data/lib/protobuf/generators/option_generator.rb +17 -0
  60. data/lib/protobuf/generators/printable.rb +160 -0
  61. data/lib/protobuf/generators/service_generator.rb +50 -0
  62. data/lib/protobuf/lifecycle.rb +33 -0
  63. data/lib/protobuf/logging.rb +39 -0
  64. data/lib/protobuf/message.rb +260 -0
  65. data/lib/protobuf/message/fields.rb +233 -0
  66. data/lib/protobuf/message/serialization.rb +85 -0
  67. data/lib/protobuf/optionable.rb +70 -0
  68. data/lib/protobuf/rpc/buffer.rb +78 -0
  69. data/lib/protobuf/rpc/client.rb +140 -0
  70. data/lib/protobuf/rpc/connectors/base.rb +221 -0
  71. data/lib/protobuf/rpc/connectors/ping.rb +89 -0
  72. data/lib/protobuf/rpc/connectors/socket.rb +78 -0
  73. data/lib/protobuf/rpc/connectors/zmq.rb +319 -0
  74. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +50 -0
  75. data/lib/protobuf/rpc/env.rb +60 -0
  76. data/lib/protobuf/rpc/error.rb +28 -0
  77. data/lib/protobuf/rpc/error/client_error.rb +31 -0
  78. data/lib/protobuf/rpc/error/server_error.rb +43 -0
  79. data/lib/protobuf/rpc/middleware.rb +25 -0
  80. data/lib/protobuf/rpc/middleware/exception_handler.rb +40 -0
  81. data/lib/protobuf/rpc/middleware/logger.rb +95 -0
  82. data/lib/protobuf/rpc/middleware/request_decoder.rb +79 -0
  83. data/lib/protobuf/rpc/middleware/response_encoder.rb +83 -0
  84. data/lib/protobuf/rpc/middleware/runner.rb +18 -0
  85. data/lib/protobuf/rpc/rpc.pb.rb +64 -0
  86. data/lib/protobuf/rpc/rpc_method.rb +16 -0
  87. data/lib/protobuf/rpc/server.rb +39 -0
  88. data/lib/protobuf/rpc/servers/socket/server.rb +121 -0
  89. data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
  90. data/lib/protobuf/rpc/servers/socket_runner.rb +46 -0
  91. data/lib/protobuf/rpc/servers/zmq/broker.rb +194 -0
  92. data/lib/protobuf/rpc/servers/zmq/server.rb +321 -0
  93. data/lib/protobuf/rpc/servers/zmq/util.rb +48 -0
  94. data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
  95. data/lib/protobuf/rpc/servers/zmq_runner.rb +70 -0
  96. data/lib/protobuf/rpc/service.rb +172 -0
  97. data/lib/protobuf/rpc/service_directory.rb +261 -0
  98. data/lib/protobuf/rpc/service_dispatcher.rb +45 -0
  99. data/lib/protobuf/rpc/service_filters.rb +250 -0
  100. data/lib/protobuf/rpc/stat.rb +119 -0
  101. data/lib/protobuf/socket.rb +21 -0
  102. data/lib/protobuf/tasks.rb +1 -0
  103. data/lib/protobuf/tasks/compile.rake +80 -0
  104. data/lib/protobuf/varint.rb +20 -0
  105. data/lib/protobuf/varint_pure.rb +31 -0
  106. data/lib/protobuf/version.rb +3 -0
  107. data/lib/protobuf/wire_type.rb +10 -0
  108. data/lib/protobuf/zmq.rb +21 -0
  109. data/profile.html +5103 -0
  110. data/proto/dynamic_discovery.proto +44 -0
  111. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  112. data/proto/google/protobuf/descriptor.proto +779 -0
  113. data/proto/rpc.proto +69 -0
  114. data/protobuf-cucumber.gemspec +57 -0
  115. data/spec/benchmark/tasks.rb +143 -0
  116. data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
  117. data/spec/encoding/all_types_spec.rb +103 -0
  118. data/spec/encoding/extreme_values_spec.rb +0 -0
  119. data/spec/functional/class_inheritance_spec.rb +52 -0
  120. data/spec/functional/code_generator_spec.rb +58 -0
  121. data/spec/functional/socket_server_spec.rb +59 -0
  122. data/spec/functional/zmq_server_spec.rb +105 -0
  123. data/spec/lib/protobuf/cli_spec.rb +317 -0
  124. data/spec/lib/protobuf/code_generator_spec.rb +87 -0
  125. data/spec/lib/protobuf/enum_spec.rb +307 -0
  126. data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
  127. data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
  128. data/spec/lib/protobuf/field/enum_field_spec.rb +44 -0
  129. data/spec/lib/protobuf/field/field_array_spec.rb +105 -0
  130. data/spec/lib/protobuf/field/field_hash_spec.rb +168 -0
  131. data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
  132. data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
  133. data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
  134. data/spec/lib/protobuf/field/int32_field_spec.rb +120 -0
  135. data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
  136. data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
  137. data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
  138. data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
  139. data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
  140. data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
  141. data/spec/lib/protobuf/field/string_field_spec.rb +79 -0
  142. data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
  143. data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
  144. data/spec/lib/protobuf/field_spec.rb +192 -0
  145. data/spec/lib/protobuf/generators/base_spec.rb +154 -0
  146. data/spec/lib/protobuf/generators/enum_generator_spec.rb +82 -0
  147. data/spec/lib/protobuf/generators/extension_generator_spec.rb +42 -0
  148. data/spec/lib/protobuf/generators/field_generator_spec.rb +197 -0
  149. data/spec/lib/protobuf/generators/file_generator_spec.rb +119 -0
  150. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  151. data/spec/lib/protobuf/generators/service_generator_spec.rb +99 -0
  152. data/spec/lib/protobuf/lifecycle_spec.rb +94 -0
  153. data/spec/lib/protobuf/message_spec.rb +944 -0
  154. data/spec/lib/protobuf/optionable_spec.rb +265 -0
  155. data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
  156. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +226 -0
  157. data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
  158. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +34 -0
  159. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +110 -0
  160. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
  161. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
  162. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
  163. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +91 -0
  164. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
  165. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +43 -0
  166. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
  167. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
  168. data/spec/lib/protobuf/rpc/service_directory_spec.rb +293 -0
  169. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +35 -0
  170. data/spec/lib/protobuf/rpc/service_filters_spec.rb +517 -0
  171. data/spec/lib/protobuf/rpc/service_spec.rb +162 -0
  172. data/spec/lib/protobuf/rpc/stat_spec.rb +101 -0
  173. data/spec/lib/protobuf/varint_spec.rb +29 -0
  174. data/spec/lib/protobuf_spec.rb +105 -0
  175. data/spec/spec_helper.rb +42 -0
  176. data/spec/support/all.rb +6 -0
  177. data/spec/support/packed_field.rb +23 -0
  178. data/spec/support/protos/all_types.data.bin +0 -0
  179. data/spec/support/protos/all_types.data.txt +119 -0
  180. data/spec/support/protos/enum.pb.rb +63 -0
  181. data/spec/support/protos/enum.proto +37 -0
  182. data/spec/support/protos/extreme_values.data.bin +0 -0
  183. data/spec/support/protos/google_unittest.bin +0 -0
  184. data/spec/support/protos/google_unittest.pb.rb +798 -0
  185. data/spec/support/protos/google_unittest.proto +884 -0
  186. data/spec/support/protos/google_unittest_custom_options.bin +0 -0
  187. data/spec/support/protos/google_unittest_custom_options.pb.rb +361 -0
  188. data/spec/support/protos/google_unittest_custom_options.proto +424 -0
  189. data/spec/support/protos/google_unittest_import.pb.rb +55 -0
  190. data/spec/support/protos/google_unittest_import.proto +73 -0
  191. data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
  192. data/spec/support/protos/google_unittest_import_public.proto +41 -0
  193. data/spec/support/protos/map-test.bin +157 -0
  194. data/spec/support/protos/map-test.pb.rb +85 -0
  195. data/spec/support/protos/map-test.proto +68 -0
  196. data/spec/support/protos/multi_field_extensions.pb.rb +59 -0
  197. data/spec/support/protos/multi_field_extensions.proto +35 -0
  198. data/spec/support/protos/resource.pb.rb +172 -0
  199. data/spec/support/protos/resource.proto +137 -0
  200. data/spec/support/resource_service.rb +23 -0
  201. data/spec/support/server.rb +65 -0
  202. data/spec/support/test_app_file.rb +2 -0
  203. data/varint_prof.rb +82 -0
  204. metadata +579 -0
@@ -0,0 +1,122 @@
1
+ require 'protobuf/generators/enum_generator'
2
+ require 'protobuf/generators/extension_generator'
3
+ require 'protobuf/generators/field_generator'
4
+ require 'protobuf/generators/message_generator'
5
+ require 'protobuf/generators/option_generator'
6
+ require 'protobuf/generators/service_generator'
7
+
8
+ module Protobuf
9
+ module Generators
10
+ class GroupGenerator
11
+ include ::Protobuf::Generators::Printable
12
+
13
+ attr_reader :groups, :indent_level
14
+ attr_writer :order
15
+
16
+ def initialize(indent_level = 0)
17
+ @groups = Hash.new { |h, k| h[k] = [] }
18
+ @headers = {}
19
+ @comments = {}
20
+ @handlers = {}
21
+ @indent_level = indent_level
22
+ @order = [:enum, :message_declaration, :options, :message, :extended_message, :service]
23
+ init_printer(indent_level)
24
+ end
25
+
26
+ def add_options(option_descriptor)
27
+ @groups[:options] << OptionGenerator.new(option_descriptor, indent_level)
28
+ end
29
+
30
+ def add_enums(enum_descriptors, options)
31
+ enum_descriptors.each do |enum_descriptor|
32
+ @groups[:enum] << EnumGenerator.new(enum_descriptor, indent_level, options)
33
+ end
34
+ end
35
+
36
+ def add_comment(type, message)
37
+ @comments[type] = message
38
+ end
39
+
40
+ def add_extended_messages(extended_messages, skip_empty_fields = true)
41
+ extended_messages.each do |message_type, field_descriptors|
42
+ next if skip_empty_fields && field_descriptors.empty?
43
+ @groups[:extended_message] << ExtensionGenerator.new(message_type, field_descriptors, indent_level)
44
+ end
45
+ end
46
+
47
+ def add_extension_fields(field_descriptors)
48
+ field_descriptors.each do |field_descriptor|
49
+ @groups[:extension_field] << FieldGenerator.new(field_descriptor, nil, indent_level)
50
+ end
51
+ end
52
+
53
+ def add_extension_ranges(extension_ranges, &item_handler)
54
+ @groups[:extension_range] = extension_ranges
55
+ @handlers[:extension_range] = item_handler
56
+ end
57
+
58
+ def add_header(type, message)
59
+ @headers[type] = message
60
+ end
61
+
62
+ def add_message_declarations(descriptors)
63
+ descriptors.each do |descriptor|
64
+ # elide synthetic map entry messages (we handle map fields differently)
65
+ next if descriptor.options.try(:map_entry?) { false }
66
+ @groups[:message_declaration] << MessageGenerator.new(descriptor, indent_level, :declaration => true)
67
+ end
68
+ end
69
+
70
+ def add_message_fields(field_descriptors, msg_descriptor)
71
+ field_descriptors.each do |field_descriptor|
72
+ @groups[:field] << FieldGenerator.new(field_descriptor, msg_descriptor, indent_level)
73
+ end
74
+ end
75
+
76
+ def add_messages(descriptors, options = {})
77
+ descriptors.each do |descriptor|
78
+ # elide synthetic map entry message (we handle map fields differently)
79
+ next if descriptor.options.try(:map_entry?) { false }
80
+ @groups[:message] << MessageGenerator.new(descriptor, indent_level, options)
81
+ end
82
+ end
83
+
84
+ def add_services(service_descriptors)
85
+ service_descriptors.each do |service_descriptor|
86
+ @groups[:service] << ServiceGenerator.new(service_descriptor, indent_level)
87
+ end
88
+ end
89
+
90
+ def compile
91
+ @order.each do |type|
92
+ items = @groups[type]
93
+ next if items.empty?
94
+
95
+ item_handler = @handlers[type]
96
+
97
+ item_header = @headers[type]
98
+ header(item_header) if item_header
99
+
100
+ item_comment = @comments[type]
101
+ comment(item_comment) if item_comment
102
+
103
+ items.each do |item|
104
+ if item_handler
105
+ puts item_handler.call(item)
106
+ else
107
+ print item.to_s
108
+ end
109
+ end
110
+
111
+ puts if type == :message_declaration || type == :options
112
+ end
113
+ end
114
+
115
+ def to_s
116
+ compile
117
+ print_contents
118
+ end
119
+
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,104 @@
1
+ require 'protobuf/generators/base'
2
+ require 'protobuf/generators/group_generator'
3
+
4
+ module Protobuf
5
+ module Generators
6
+ class MessageGenerator < Base
7
+
8
+ def initialize(descriptor, indent_level, options = {})
9
+ super
10
+ @only_declarations = options.fetch(:declaration) { false }
11
+ @extension_fields = options.fetch(:extension_fields) { {} }
12
+ end
13
+
14
+ def compile
15
+ run_once(:compile) do
16
+ if @only_declarations
17
+ compile_declaration
18
+ else
19
+ compile_message
20
+ end
21
+ end
22
+ end
23
+
24
+ def compile_declaration
25
+ run_once(:compile_declaration) do
26
+ if printable?
27
+ print_class(descriptor.name, :message) do
28
+ group = GroupGenerator.new(current_indent)
29
+ group.add_enums(descriptor.enum_type, :namespace => type_namespace)
30
+ group.add_message_declarations(descriptor.nested_type)
31
+ print group.to_s
32
+ end
33
+ else
34
+ print_class(descriptor.name, :message)
35
+ end
36
+ end
37
+ end
38
+
39
+ def compile_message
40
+ run_once(:compile_message) do
41
+ if printable?
42
+ print_class(descriptor.name, nil) do
43
+ group = GroupGenerator.new(current_indent)
44
+ group.add_messages(descriptor.nested_type, :extension_fields => @extension_fields, :namespace => type_namespace)
45
+ group.add_comment(:options, 'Message Options')
46
+ group.add_options(descriptor.options) if options?
47
+ group.add_message_fields(descriptor.field, descriptor)
48
+ self.class.validate_tags(fully_qualified_type_namespace, descriptor.field.map(&:number))
49
+
50
+ group.add_comment(:extension_range, 'Extension Fields')
51
+ group.add_extension_ranges(descriptor.extension_range) do |extension_range|
52
+ "extensions #{extension_range.start}...#{extension_range.end}"
53
+ end
54
+
55
+ group.add_extension_fields(message_extension_fields)
56
+
57
+ group.order = [:message, :options, :field, :extension_range, :extension_field]
58
+ print group.to_s
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def extensions?
67
+ !message_extension_fields.empty?
68
+ end
69
+
70
+ def fields?
71
+ descriptor.field.count > 0
72
+ end
73
+
74
+ def options?
75
+ descriptor.options
76
+ end
77
+
78
+ def nested_enums?
79
+ descriptor.enum_type.count > 0
80
+ end
81
+
82
+ def nested_messages?
83
+ descriptor.nested_type.count > 0
84
+ end
85
+
86
+ def nested_types?
87
+ nested_enums? || nested_messages?
88
+ end
89
+
90
+ def printable?
91
+ if @only_declarations
92
+ nested_types?
93
+ else
94
+ fields? || nested_messages? || extensions? || options?
95
+ end
96
+ end
97
+
98
+ def message_extension_fields
99
+ @extension_fields.fetch(fully_qualified_type_namespace) { [] }
100
+ end
101
+
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,17 @@
1
+ require 'protobuf/generators/base'
2
+
3
+ module Protobuf
4
+ module Generators
5
+ class OptionGenerator < Base
6
+ def compile
7
+ run_once(:compile) do
8
+ descriptor.each_field.map do |field, value|
9
+ next unless descriptor.field?(field.name)
10
+ serialized_value = serialize_value(value)
11
+ puts "set_option #{field.fully_qualified_name.inspect}, #{serialized_value}"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,160 @@
1
+ module Protobuf
2
+ module Generators
3
+ module Printable
4
+
5
+ PARENT_CLASS_MESSAGE = "::Protobuf::Message".freeze
6
+ PARENT_CLASS_ENUM = "::Protobuf::Enum".freeze
7
+ PARENT_CLASS_SERVICE = "::Protobuf::Rpc::Service".freeze
8
+
9
+ # Initialize the printer.
10
+ # Must be called by any class/module that includes the Printable module.
11
+ #
12
+ def init_printer(indent_level)
13
+ @io = ::StringIO.new
14
+ self.current_indent = indent_level.to_i
15
+ end
16
+
17
+ protected
18
+
19
+ attr_accessor :current_indent
20
+
21
+ private
22
+
23
+ # Print a one-line comment.
24
+ #
25
+ def comment(message)
26
+ puts "# #{message}"
27
+ end
28
+
29
+ # Print a "header" comment.
30
+ #
31
+ # header("Lorem ipsum dolor")
32
+ # ##
33
+ # # Lorem ipsum dolor
34
+ # #
35
+ def header(message)
36
+ puts
37
+ puts "##"
38
+ puts "# #{message}"
39
+ puts "#"
40
+ end
41
+
42
+ # Increase the indent level. An outdent will only occur if given a block
43
+ # (after the block is finished).
44
+ #
45
+ def indent
46
+ self.current_indent += 1
47
+ yield
48
+ outdent
49
+ end
50
+
51
+ # Take a string and upcase the first character of each namespace.
52
+ # Due to the nature of varying standards about how class/modules are named
53
+ # (e.g. CamelCase, Underscore_Case, SCREAMING_SNAKE_CASE), we only want
54
+ # to capitalize the first character to ensure ruby will treat the value
55
+ # as a constant. Otherwise we do not attempt to change the
56
+ # token's definition.
57
+ #
58
+ # modulize("foo.bar.Baz") -> "::Foo::Bar::Baz"
59
+ # modulize("foo.bar.baz") -> "::Foo::Bar::Baz"
60
+ # modulize("foo.bar.BAZ") -> "::Foo::Bar::BAZ"
61
+ #
62
+ def modulize(name)
63
+ name = name.gsub(/\./, '::')
64
+ name = name.gsub(/(^(?:::)?[a-z]|::[a-z])/, &:upcase)
65
+ name
66
+ end
67
+
68
+ # Decrease the indent level. Cannot be negative.
69
+ #
70
+ def outdent
71
+ self.current_indent -= 1 unless current_indent.zero?
72
+ end
73
+
74
+ # Return the parent class for a given type.
75
+ # Valid types are :message, :enum, and :service, otherwise an error
76
+ # will be thrown.
77
+ #
78
+ def parent_class(type)
79
+ case type
80
+ when :message then
81
+ PARENT_CLASS_MESSAGE
82
+ when :enum then
83
+ PARENT_CLASS_ENUM
84
+ when :service then
85
+ PARENT_CLASS_SERVICE
86
+ else
87
+ fail "Unknown parent class type #{type}: #{caller[0..5].join("\n")}"
88
+ end
89
+ end
90
+
91
+ # Print a class or module block, indicated by type.
92
+ # If a class, can be given a parent class to inherit from.
93
+ # If a block is given, call the block from within an indent block.
94
+ # Otherwise, end the block on the same line.
95
+ #
96
+ def print_block(name, parent_klass, type)
97
+ name = modulize(name)
98
+ block_def = "#{type} #{name}"
99
+ block_def += " < #{parent_class(parent_klass)}" if parent_klass
100
+
101
+ if block_given?
102
+ puts block_def
103
+ indent { yield }
104
+ puts "end"
105
+ puts
106
+ else
107
+ block_def += "; end"
108
+ puts block_def
109
+ end
110
+ end
111
+
112
+ # Use print_block to print a class, with optional parent class
113
+ # to inherit from. Accepts a block for use with print_block.
114
+ #
115
+ def print_class(name, parent_klass, &block)
116
+ print_block(name, parent_klass, :class, &block)
117
+ end
118
+
119
+ # Use print_block to print a module.
120
+ # Accepts a block for use with print_block.
121
+ #
122
+ def print_module(name, &block)
123
+ print_block(name, nil, :module, &block)
124
+ end
125
+
126
+ # Print a file require.
127
+ #
128
+ # print_require('foo/bar/baz') -> "require 'foo/bar/baz'"
129
+ #
130
+ def print_require(file, relative = false)
131
+ puts "require#{'_relative' if relative} '#{file}'"
132
+ end
133
+
134
+ # Puts the given message prefixed by the indent level.
135
+ # If no message is given print a newline.
136
+ #
137
+ def puts(message = nil)
138
+ if message
139
+ @io.puts((" " * current_indent) + message)
140
+ else
141
+ @io.puts
142
+ end
143
+ end
144
+
145
+ # Print the given message raw, no indent.
146
+ #
147
+ def print(contents)
148
+ @io.print(contents)
149
+ end
150
+
151
+ # Returns the contents of the underlying StringIO object.
152
+ #
153
+ def print_contents
154
+ @io.rewind
155
+ @io.read
156
+ end
157
+
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,50 @@
1
+ require 'protobuf/generators/base'
2
+ require 'protobuf/generators/option_generator'
3
+
4
+ module Protobuf
5
+ module Generators
6
+ class ServiceGenerator < Base
7
+
8
+ def compile
9
+ run_once(:compile) do
10
+ print_class(descriptor.name, :service) do
11
+ print OptionGenerator.new(descriptor.options, current_indent).to_s if descriptor.options
12
+ descriptor.method.each do |method_descriptor|
13
+ print_method(method_descriptor)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def print_method(method_descriptor)
22
+ request_klass = modulize(method_descriptor.input_type)
23
+ response_klass = modulize(method_descriptor.output_type)
24
+ name = ENV.key?('PB_USE_RAW_RPC_NAMES') ? method_descriptor.name : method_descriptor.name.underscore
25
+ options = {}
26
+ if method_descriptor.options
27
+ method_descriptor.options.each_field do |field_option|
28
+ option_value = method_descriptor.options[field_option.name]
29
+ next unless method_descriptor.options.field?(field_option.name)
30
+ options[field_option.fully_qualified_name] = serialize_value(option_value)
31
+ end
32
+ end
33
+
34
+ rpc = "rpc :#{name}, #{request_klass}, #{response_klass}"
35
+
36
+ if options.empty?
37
+ puts rpc
38
+ return
39
+ end
40
+
41
+ puts rpc + " do"
42
+ options.each do |option_name, value|
43
+ indent { puts "set_option #{option_name.inspect}, #{value}" }
44
+ end
45
+ puts "end"
46
+ end
47
+
48
+ end
49
+ end
50
+ end