gapic-generator 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +13 -0
  3. data/CHANGELOG.md +43 -0
  4. data/CODE_OF_CONDUCT.md +43 -0
  5. data/CONTRIBUTING.md +28 -0
  6. data/LICENSE +202 -0
  7. data/README.md +72 -0
  8. data/bin/gapic-generator +103 -0
  9. data/bin/protoc-gen-ruby_gapic +33 -0
  10. data/default-rubocop.yml +10 -0
  11. data/gem_templates/binary.erb +20 -0
  12. data/gem_templates/dockerfile.erb +39 -0
  13. data/gem_templates/entrypoint.erb +24 -0
  14. data/gem_templates/gapic_sh.erb +97 -0
  15. data/gem_templates/gemfile.erb +8 -0
  16. data/gem_templates/gemspec.erb +36 -0
  17. data/gem_templates/generator.erb +37 -0
  18. data/gem_templates/gitignore.erb +10 -0
  19. data/gem_templates/rakefile.erb +29 -0
  20. data/gem_templates/readme.erb +69 -0
  21. data/gem_templates/rubocop.erb +16 -0
  22. data/gem_templates/shared/_header.erb +4 -0
  23. data/gem_templates/shared/_license.erb +13 -0
  24. data/gem_templates/shared/_warning.erb +1 -0
  25. data/gem_templates/test_generator.erb +13 -0
  26. data/gem_templates/test_helper.erb +25 -0
  27. data/gem_templates/version.erb +10 -0
  28. data/lib/gapic/file_formatter.rb +62 -0
  29. data/lib/gapic/formatting_utils.rb +109 -0
  30. data/lib/gapic/gem_builder.rb +98 -0
  31. data/lib/gapic/generator.rb +30 -0
  32. data/lib/gapic/generator/version.rb +21 -0
  33. data/lib/gapic/generators/base_generator.rb +91 -0
  34. data/lib/gapic/generators/default_generator.rb +101 -0
  35. data/lib/gapic/grpc_service_config/method_config.rb +49 -0
  36. data/lib/gapic/grpc_service_config/parser.rb +218 -0
  37. data/lib/gapic/grpc_service_config/parsing_error.rb +25 -0
  38. data/lib/gapic/grpc_service_config/retry_policy.rb +51 -0
  39. data/lib/gapic/grpc_service_config/service_config.rb +42 -0
  40. data/lib/gapic/path_template.rb +35 -0
  41. data/lib/gapic/path_template/parser.rb +83 -0
  42. data/lib/gapic/path_template/segment.rb +67 -0
  43. data/lib/gapic/resource_lookup.rb +91 -0
  44. data/lib/gapic/runner.rb +76 -0
  45. data/lib/gapic/schema.rb +17 -0
  46. data/lib/gapic/schema/api.rb +264 -0
  47. data/lib/gapic/schema/loader.rb +269 -0
  48. data/lib/gapic/schema/wrappers.rb +717 -0
  49. data/lib/google/api/annotations.pb.rb +39 -0
  50. data/lib/google/api/client.pb.rb +43 -0
  51. data/lib/google/api/field_behavior.pb.rb +51 -0
  52. data/lib/google/api/http.pb.rb +60 -0
  53. data/lib/google/api/resource.pb.rb +80 -0
  54. data/lib/google/longrunning/operations.pb.rb +115 -0
  55. data/lib/google/protobuf/any.pb.rb +40 -0
  56. data/lib/google/protobuf/compiler/plugin.pb.rb +72 -0
  57. data/lib/google/protobuf/descriptor.pb.rb +359 -0
  58. data/lib/google/protobuf/empty.pb.rb +36 -0
  59. data/lib/google/rpc/status.pb.rb +46 -0
  60. data/templates/default/gem/_version.erb +2 -0
  61. data/templates/default/gem/changelog.erb +3 -0
  62. data/templates/default/gem/gemfile.erb +4 -0
  63. data/templates/default/gem/gemspec.erb +37 -0
  64. data/templates/default/gem/gitignore.erb +20 -0
  65. data/templates/default/gem/license.erb +22 -0
  66. data/templates/default/gem/rakefile.erb +27 -0
  67. data/templates/default/gem/readme.erb +24 -0
  68. data/templates/default/gem/rubocop.erb +59 -0
  69. data/templates/default/gem/version.erb +6 -0
  70. data/templates/default/gem/yardopts.erb +12 -0
  71. data/templates/default/helpers/default_helper.rb +50 -0
  72. data/templates/default/helpers/filepath_helper.rb +38 -0
  73. data/templates/default/helpers/namespace_helper.rb +44 -0
  74. data/templates/default/helpers/presenter_helper.rb +24 -0
  75. data/templates/default/helpers/presenters/enum_presenter.rb +35 -0
  76. data/templates/default/helpers/presenters/enum_value_presenter.rb +33 -0
  77. data/templates/default/helpers/presenters/field_presenter.rb +146 -0
  78. data/templates/default/helpers/presenters/file_presenter.rb +53 -0
  79. data/templates/default/helpers/presenters/gem_presenter.rb +140 -0
  80. data/templates/default/helpers/presenters/message_presenter.rb +66 -0
  81. data/templates/default/helpers/presenters/method_presenter.rb +293 -0
  82. data/templates/default/helpers/presenters/package_presenter.rb +65 -0
  83. data/templates/default/helpers/presenters/resource_presenter.rb +92 -0
  84. data/templates/default/helpers/presenters/sample_presenter.rb +74 -0
  85. data/templates/default/helpers/presenters/service_presenter.rb +276 -0
  86. data/templates/default/layouts/_ruby.erb +20 -0
  87. data/templates/default/package.erb +6 -0
  88. data/templates/default/proto_docs/_enum.erb +13 -0
  89. data/templates/default/proto_docs/_message.erb +23 -0
  90. data/templates/default/proto_docs/_proto_file.erb +9 -0
  91. data/templates/default/proto_docs/proto_file.erb +6 -0
  92. data/templates/default/proto_docs/readme.erb +5 -0
  93. data/templates/default/service.erb +8 -0
  94. data/templates/default/service/client.erb +6 -0
  95. data/templates/default/service/client/_client.erb +137 -0
  96. data/templates/default/service/client/_config.erb +155 -0
  97. data/templates/default/service/client/_credentials.erb +21 -0
  98. data/templates/default/service/client/_helpers.erb +9 -0
  99. data/templates/default/service/client/_operations.erb +88 -0
  100. data/templates/default/service/client/_paths.erb +8 -0
  101. data/templates/default/service/client/_requires.erb +1 -0
  102. data/templates/default/service/client/_resource.erb +6 -0
  103. data/templates/default/service/client/_self_configure.erb +9 -0
  104. data/templates/default/service/client/_self_configure_defaults.erb +22 -0
  105. data/templates/default/service/client/_self_configure_retry_policy.erb +15 -0
  106. data/templates/default/service/client/method/_def.erb +21 -0
  107. data/templates/default/service/client/method/def/_options_defaults.erb +29 -0
  108. data/templates/default/service/client/method/def/_request.erb +6 -0
  109. data/templates/default/service/client/method/def/_request_normal.erb +4 -0
  110. data/templates/default/service/client/method/def/_request_streaming.erb +9 -0
  111. data/templates/default/service/client/method/def/_rescue.erb +1 -0
  112. data/templates/default/service/client/method/def/_response.erb +6 -0
  113. data/templates/default/service/client/method/def/_response_normal.erb +8 -0
  114. data/templates/default/service/client/method/def/_response_paged.erb +9 -0
  115. data/templates/default/service/client/method/docs/_error.erb +2 -0
  116. data/templates/default/service/client/method/docs/_request.erb +6 -0
  117. data/templates/default/service/client/method/docs/_request_field.erb +7 -0
  118. data/templates/default/service/client/method/docs/_request_normal.erb +20 -0
  119. data/templates/default/service/client/method/docs/_request_streaming.erb +5 -0
  120. data/templates/default/service/client/method/docs/_response.erb +6 -0
  121. data/templates/default/service/client/method/docs/_sample.erb +20 -0
  122. data/templates/default/service/client/method/docs/_sample_response.erb +24 -0
  123. data/templates/default/service/client/method/docs/_samples.erb +6 -0
  124. data/templates/default/service/client/method/docs/request_field/_arg.erb +10 -0
  125. data/templates/default/service/client/method/docs/request_field/_hash.erb +19 -0
  126. data/templates/default/service/client/method/docs/sample_response/_comment.erb +5 -0
  127. data/templates/default/service/client/method/docs/sample_response/_define.erb +2 -0
  128. data/templates/default/service/client/method/docs/sample_response/_loop.erb +12 -0
  129. data/templates/default/service/client/method/docs/sample_response/_print.erb +2 -0
  130. data/templates/default/service/client/method/docs/sample_response/_write_file.erb +2 -0
  131. data/templates/default/service/client/resource/_def.erb +6 -0
  132. data/templates/default/service/client/resource/_doc.erb +8 -0
  133. data/templates/default/service/client/resource/_multi.erb +28 -0
  134. data/templates/default/service/client/resource/_single.erb +11 -0
  135. data/templates/default/service/credentials.erb +6 -0
  136. data/templates/default/service/operations.erb +6 -0
  137. data/templates/default/service/paths.erb +6 -0
  138. data/templates/default/service/test/client.erb +24 -0
  139. data/templates/default/service/test/client_operations.erb +24 -0
  140. data/templates/default/service/test/method/_assert_response.erb +11 -0
  141. data/templates/default/service/test/method/_bidi.erb +100 -0
  142. data/templates/default/service/test/method/_client.erb +84 -0
  143. data/templates/default/service/test/method/_normal.erb +69 -0
  144. data/templates/default/service/test/method/_server.erb +85 -0
  145. data/templates/default/service/test/method/_setup.erb +21 -0
  146. data/templates/default/service/test/smoke.erb +12 -0
  147. data/templates/default/shared/_header.erb +4 -0
  148. data/templates/default/shared/_license.erb +21 -0
  149. data/templates/default/shared/_warning.erb +1 -0
  150. metadata +351 -0
@@ -0,0 +1,269 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "google/api/annotations.pb"
18
+ require "google/api/client.pb"
19
+ require "google/api/field_behavior.pb"
20
+ require "google/api/resource.pb"
21
+ require "google/longrunning/operations.pb"
22
+ require "google/protobuf/descriptor.pb"
23
+ require "gapic/schema/wrappers"
24
+
25
+ module Gapic
26
+ module Schema
27
+ # Loader
28
+ class Loader
29
+ # Initializes the loader
30
+ def initialize
31
+ @prior_messages = []
32
+ @prior_enums = []
33
+ end
34
+
35
+ # Loads a file.
36
+ #
37
+ # @param file_descriptor [Google::Protobuf::FileDescriptorProto] the
38
+ # descriptor of the proto file.
39
+ # @oaram file_to_generate [Boolean] Whether this file is to be
40
+ # generated.
41
+ def load_file file_descriptor, file_to_generate
42
+ # Setup.
43
+ address = file_descriptor.package.split "."
44
+ path = []
45
+ registry = {}
46
+
47
+ # Load the docs.
48
+ location = file_descriptor.source_code_info.location
49
+ docs = location.each_with_object({}) { |l, ans| ans[l.path] = l }
50
+
51
+ # Load top-level enums.
52
+ enums = file_descriptor.enum_type.each_with_index.map do |e, i|
53
+ load_enum registry, e, address, docs, [5, i]
54
+ end
55
+
56
+ # Load top-level messages.
57
+ messages = file_descriptor.message_type.each_with_index.map do |m, i|
58
+ load_message registry, m, address, docs, [4, i]
59
+ end
60
+ messages.each(&method(:update_fields!))
61
+
62
+ # Load services.
63
+ services = file_descriptor.service.each_with_index.map do |s, i|
64
+ load_service registry, s, address, docs, [6, i]
65
+ end
66
+
67
+ # Construct and return the file.
68
+ File.new file_descriptor, address, docs[path], messages, enums,
69
+ services, file_to_generate, registry
70
+ end
71
+
72
+ # Updates the fields of a message and it's nested messages.
73
+ #
74
+ # @param message [Message] the message whose fields and nested messages
75
+ # to update.
76
+ def update_fields! message
77
+ message.nested_messages.each(&method(:update_fields!))
78
+ non_primitives = message.fields.reject { |f| f.type_name.empty? }
79
+ non_primitives.each do |f|
80
+ f.message ||= cached_message f.type_name
81
+ f.enum ||= cached_enum f.type_name
82
+ end
83
+ end
84
+
85
+ # Loads an enum.
86
+ #
87
+ # @param descriptor [Google::Protobuf::EnumDescriptorProto] the
88
+ # descriptor of this enum.
89
+ # @param address [Enumerable<String>] The address of the parent.
90
+ # @param docs [Hash<Enumerable<Integer>,
91
+ # Google::Protobuf::SourceCodeInfo::Location>]
92
+ # A mapping of a path to the docs. See Proto#docs for more info.
93
+ # @param path [Enumerable<Integer>] The current path. This is used to
94
+ # get the docs for a proto. See Proto#docs for more info.
95
+ # @return [Enum] The loaded enum.
96
+ def load_enum registry, descriptor, address, docs, path
97
+ # Update Address.
98
+ address = address.clone << descriptor.name
99
+
100
+ # Load Enum Values
101
+ values = descriptor.value.each_with_index.map do |value, i|
102
+ load_enum_value registry, value, address, docs, path + [2, i]
103
+ end
104
+
105
+ # Construct, cache and return enum.
106
+ enum = Enum.new descriptor, address, docs[path], values
107
+ @prior_enums << enum
108
+ add_to_registry registry, address, enum
109
+ end
110
+
111
+ # Loads an enum value.
112
+ #
113
+ # @param descriptor [Google::Protobuf::EnumValueDescriptorProto] the
114
+ # descriptor of this enum value.
115
+ # @param address [Enumerable<String>] the address of the parent.
116
+ # @param docs [Hash<Enumerable<Integer>,
117
+ # Google::Protobuf::SourceCodeInfo::Location>]
118
+ # A mapping of a path to the docs. See Proto#docs for more info.
119
+ # @param path [Enumerable<Integer>] The current path. This is used to
120
+ # get the docs for a proto. See Proto#docs for more info.
121
+ # @return [EnumValue] The loaded enum value.
122
+ def load_enum_value registry, descriptor, address, docs, path
123
+ # Update Address.
124
+ address = address.clone << descriptor.name
125
+
126
+ # Construct and return value.
127
+ enum_value = EnumValue.new descriptor, address, docs[path]
128
+ add_to_registry registry, address, enum_value
129
+ end
130
+
131
+ # Loads a message. As a side effect, this alters @messages and @enums
132
+ # with the nested messages that are found.
133
+ #
134
+ # @param descriptor [Google::Protobuf::DescriptorProto] the
135
+ # descriptor of this message.
136
+ # @param address [Enumerable<String>] the address of the parent.
137
+ # @param docs [Hash<Enumerable<Integer>,
138
+ # Google::Protobuf::SourceCodeInfo::Location>]
139
+ # A mapping of a path to the docs. See Proto#docs for more info.
140
+ # @param path [Enumerable<Integer>] The current path. This is used to
141
+ # get the docs for a proto. See Proto#docs for more info.
142
+ # @return [Message] The loaded message.
143
+ def load_message registry, descriptor, address, docs, path
144
+ # Update Address.
145
+ address = address.clone << descriptor.name
146
+
147
+ # Load Children
148
+ nested_messages = descriptor.nested_type.each_with_index.map do |m, i|
149
+ load_message registry, m, address, docs, path + [3, i]
150
+ end
151
+ nested_enums = descriptor.enum_type.each_with_index.map do |e, i|
152
+ load_enum registry, e, address, docs, path + [4, i]
153
+ end
154
+ fields = descriptor.field.each_with_index.map do |f, i|
155
+ load_field registry, f, address, docs, path + [2, i]
156
+ end
157
+ extensions = descriptor.extension.each_with_index.map do |e, i|
158
+ load_field registry, e, address, docs, path + [6, i]
159
+ end
160
+
161
+ # Construct, cache, and return.
162
+ msg = Message.new(descriptor, address, docs[path], fields, extensions,
163
+ nested_messages, nested_enums)
164
+ @prior_messages << msg
165
+ add_to_registry registry, address, msg
166
+ end
167
+
168
+ # Loads a field.
169
+ #
170
+ # @param descriptor [Google::Protobuf::FieldDescriptorProto] the
171
+ # descriptor of this field.
172
+ # @param address [Enumerable<String>] The address of the parent.
173
+ # @param docs [Hash<Enumerable<Integer>,
174
+ # Google::Protobuf::SourceCodeInfo::Location>]
175
+ # A mapping of a path to the docs. See Proto#docs for more info.
176
+ # @param path [Enumerable<Integer>] The current path. This is used to
177
+ # get the docs for a proto. See Proto#docs for more info.
178
+ # @return [Field] The loaded field.
179
+ def load_field registry, descriptor, address, docs, path
180
+ # Update address.
181
+ address = address.clone << descriptor.name
182
+
183
+ # Construct and return the field.
184
+ field = Field.new(descriptor, address, docs[path],
185
+ cached_message(descriptor.type_name), cached_enum(descriptor.type_name))
186
+ add_to_registry registry, address, field
187
+ end
188
+
189
+ # Loads a service.
190
+ #
191
+ # @param descriptor [Google::Protobuf::ServiceDescriptorProto] the
192
+ # descriptor of this service.
193
+ # @param address [Enumerable<String>] The address of the parent.
194
+ # @param docs [Hash<Enumerable<Integer>,
195
+ # Google::Protobuf::SourceCodeInfo::Location>]
196
+ # A mapping of a path to the docs. See Proto#docs for more info.
197
+ # @param path [Enumerable<Integer>] The current path. This is used to
198
+ # get the docs for a proto. See Proto#docs for more info.
199
+ # @return [Service] The loaded service.
200
+ def load_service registry, descriptor, address, docs, path
201
+ # Update the address.
202
+ address = address.clone << descriptor.name
203
+
204
+ # Load children
205
+ methods = descriptor.method.each_with_index.map do |m, i|
206
+ load_method registry, m, address, docs, path + [2, i]
207
+ end
208
+
209
+ # Construct and return the service.
210
+ service = Service.new descriptor, address, docs[path], methods
211
+ add_to_registry registry, address, service
212
+ end
213
+
214
+ # Loads a method.
215
+ #
216
+ # @param descriptor [Google::Protobuf::MethodDescriptorProto] the
217
+ # descriptor of this service.
218
+ # @param address [Enumerable<String>] The address of the parent.
219
+ # @param docs [Hash<Enumerable<Integer>,
220
+ # Google::Protobuf::SourceCodeInfo::Location>]
221
+ # A mapping of a path to the docs. See Proto#docs for more info.
222
+ # @param path [Enumerable<Integer>] The current path. This is used to
223
+ # get the docs for a proto. See Proto#docs for more info.
224
+ # @return [Method] The loaded method.
225
+ def load_method registry, descriptor, address, docs, path
226
+ # Update the address.
227
+ address = address.clone << descriptor.name
228
+
229
+ # Construct and return the method.
230
+ method = Method.new(descriptor, address, docs[path],
231
+ cached_message(descriptor.input_type), cached_message(descriptor.output_type))
232
+ add_to_registry registry, address, method
233
+ end
234
+
235
+ # Retrieves an Enum if it has been seen before.
236
+ #
237
+ # @param type_name [String] the type name of the enum.
238
+ # @return [Enum | nil] The enum if it has already been seen or nil if
239
+ # no enum can be found.
240
+ def cached_enum type_name
241
+ cached_obj @prior_enums, type_name
242
+ end
243
+
244
+ # Retrieves a Message if it has been seen before.
245
+ #
246
+ # @param type_name [String] the type name of the message.
247
+ # @return [Enum | nil] The message if it has already been seen or nil if
248
+ # no message can be found.
249
+ def cached_message type_name
250
+ cached_obj @prior_messages, type_name
251
+ end
252
+
253
+ private
254
+
255
+ def add_to_registry registry, address, obj
256
+ registry[address.join(".")] = obj
257
+ obj
258
+ end
259
+
260
+ def cached_obj collection, type_name
261
+ # Remove leading dot.
262
+ type_name = type_name[1..-1] if type_name && type_name[0] == "."
263
+
264
+ # Create an address from the type & check cache.
265
+ collection.find { |m| m.address == type_name.split(".") }
266
+ end
267
+ end
268
+ end
269
+ end
@@ -0,0 +1,717 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "gapic/formatting_utils"
18
+
19
+ module Gapic
20
+ module Schema
21
+ # Base class for all generic proto types including: enums, messages,
22
+ # methods and services.
23
+ #
24
+ # @!attribute [r] descriptor
25
+ # @return [Object] the descriptor of the proto
26
+ # @!attribute [r] address
27
+ # @return [Enumerable<String>] A sequence of names which determines
28
+ # the address of the proto. For example, a message named "Foo" in
29
+ # package "google.example.v1" will have the address:
30
+ # ["google", "example", "v1", "Foo"].
31
+ # and a nested message within "Foo" named "Bar" will have the address:
32
+ # ["google", "example", "v1", "Foo", "Bar"]
33
+ # @!attribute [r] docs
34
+ # @return [Google::Protobuf::SourceCodeInfo::Location]
35
+ # A Location identifies a piece of source code in a .proto file which
36
+ # corresponds to a particular definition. This information is
37
+ # intended to be useful to IDEs, code indexers, documentation
38
+ # generators, and similar tools.
39
+ #
40
+ # For example, say we have a file like:
41
+ # message Foo {
42
+ # optional string foo = 1;
43
+ # }
44
+ # Let's look at just the field definition:
45
+ # optional string foo = 1;
46
+ # ^ ^^ ^^ ^ ^^^
47
+ # a bc de f ghi
48
+ # We have the following locations:
49
+ # span path represents
50
+ # [a,i) [ 4, 0, 2, 0 ] The whole field definition.
51
+ # [a,b) [ 4, 0, 2, 0, 4 ] The label (optional).
52
+ # [c,d) [ 4, 0, 2, 0, 5 ] The type (string).
53
+ # [e,f) [ 4, 0, 2, 0, 1 ] The name (foo).
54
+ # [g,h) [ 4, 0, 2, 0, 3 ] The number (1).
55
+ #
56
+ # Notes:
57
+ # - A location may refer to a repeated field itself (i.e. not to any
58
+ # particular index within it). This is used whenever a set of
59
+ # elements are logically enclosed in a single code segment. For
60
+ # example, an entire extend block (possibly containing multiple
61
+ # extension definitions) will have an outer location whose path
62
+ # refers to the "extensions" repeated field without an index.
63
+ # - Multiple locations may have the same path. This happens when a
64
+ # single logical declaration is spread out across multiple places.
65
+ # The most obvious example is the "extend" block again -- there may
66
+ # be multiple extend blocks in the same scope, each of which will
67
+ # have the same path.
68
+ # - A location's span is not always a subset of its parent's span.
69
+ # For example, the "extendee" of an extension declaration appears at
70
+ # the beginning of the "extend" block and is shared by all
71
+ # extensions within the block.
72
+ # - Just because a location's span is a subset of some other
73
+ # location's span does not mean that it is a descendent. For
74
+ # example, a "group" defines both a type and a field in a single
75
+ # declaration. Thus, the locations corresponding to the type and
76
+ # field and their components will overlap.
77
+ # - Code which tries to interpret locations should probably be
78
+ # designed to ignore those that it doesn't understand, as more types
79
+ # of locations could be recorded in the future.
80
+ class Proto
81
+ extend Forwardable
82
+ attr_reader :descriptor, :address, :docs
83
+ attr_accessor :parent
84
+
85
+ # Initializes a Proto object.
86
+ # @param descriptor [Object] the protobuf
87
+ # representation of this service.
88
+ # @param address [Enumerable<String>] The address of the proto. See
89
+ # #address for more info.
90
+ # @param docs [Google::Protobuf::SourceCodeInfo::Location] The docs
91
+ # of the proto. See #docs for more info.
92
+ def initialize descriptor, address, docs
93
+ @descriptor = descriptor
94
+ @address = address
95
+ @docs = docs
96
+ end
97
+
98
+ # Gets the cleaned up leading comments documentation
99
+ def docs_leading_comments
100
+ return nil if @docs.nil?
101
+ return nil if @docs.leading_comments.empty?
102
+
103
+ lines = @docs.leading_comments.each_line.to_a
104
+ lines.map! { |line| line.start_with?(" ") ? line[1..-1] : line }
105
+ lines = FormattingUtils.escape_braces lines
106
+ lines.join
107
+ end
108
+
109
+ # @!method path
110
+ # @return [Array<Integer>]
111
+ # Identifies which part of the FileDescriptorProto was defined at
112
+ # this location.
113
+ #
114
+ # Each element is a field number or an index. They form a path from
115
+ # the root FileDescriptorProto to the place where the definition.
116
+ # For example, this path:
117
+ # [ 4, 3, 2, 7, 1 ]
118
+ # refers to:
119
+ # file.message_type(3) // 4, 3
120
+ # .field(7) // 2, 7
121
+ # .name() // 1
122
+ # This is because FileDescriptorProto.message_type has field number
123
+ # 4:
124
+ # repeated DescriptorProto message_type = 4;
125
+ # and DescriptorProto.field has field number 2:
126
+ # repeated FieldDescriptorProto field = 2;
127
+ # and FieldDescriptorProto.name has field number 1:
128
+ # optional string name = 1;
129
+ #
130
+ # Thus, the above path gives the location of a field name. If we
131
+ # removed the last element:
132
+ # [ 4, 3, 2, 7 ]
133
+ # this path refers to the whole field declaration (from the
134
+ # beginning of the label to the terminating semicolon).
135
+ # @!method span
136
+ # @return [Array<Integer
137
+ # Always has exactly three or four elements: start line, start
138
+ # column, end line (optional, otherwise assumed same as start line),
139
+ # end column. These are packed into a single field for efficiency.
140
+ # Note that line and column numbers are zero-based -- typically you
141
+ # will want to add 1 to each before displaying to a user.
142
+ # @!method leading_comments
143
+ # @return [String]
144
+ # If this SourceCodeInfo represents a complete declaration, these
145
+ # are any comments appearing before and after the declaration which
146
+ # appear to be attached to the declaration.
147
+ #
148
+ # A series of line comments appearing on consecutive lines, with no
149
+ # other tokens appearing on those lines, will be treated as a single
150
+ # comment.
151
+ #
152
+ # leading_detached_comments will keep paragraphs of comments that
153
+ # appear before (but not connected to) the current element. Each
154
+ # paragraph, separated by empty lines, will be one comment element
155
+ # in the repeated field.
156
+ #
157
+ # Only the comment content is provided; comment markers (e.g. //)
158
+ # are stripped out. For block comments, leading whitespace and an
159
+ # asterisk will be stripped from the beginning of each line other
160
+ # than the first. Newlines are included in the output.
161
+ #
162
+ # Examples:
163
+ #
164
+ # optional int32 foo = 1; // Comment attached to foo.
165
+ # // Comment attached to bar.
166
+ # optional int32 bar = 2;
167
+ #
168
+ # optional string baz = 3;
169
+ # // Comment attached to baz.
170
+ # // Another line attached to baz.
171
+ #
172
+ # // Comment attached to qux.
173
+ # //
174
+ # // Another line attached to qux.
175
+ # optional double qux = 4;
176
+ #
177
+ # // Detached comment for corge. This is not leading or trailing
178
+ # // comments to qux or corge because there are blank lines
179
+ # // separating it from both.
180
+ #
181
+ # // Detached comment for corge paragraph 2.
182
+ #
183
+ # optional string corge = 5;
184
+ # /* Block comment attached
185
+ # * to corge. Leading asterisks
186
+ # * will be removed. */
187
+ # /* Block comment attached to
188
+ # * grault. */
189
+ # optional int32 grault = 6;
190
+ #
191
+ # // ignored detached comments.
192
+ # @!method trailing_comments
193
+ # @return [String] (see #leading_comments)
194
+ # @!method leading_detached_comments
195
+ # @return [Array<String>] (see #leading_comments)
196
+ def_delegators(
197
+ :docs,
198
+ :path,
199
+ :leading_comments,
200
+ :trailing_comments,
201
+ :leading_detached_comments
202
+ )
203
+ end
204
+
205
+ # Wrapper for a protobuf service.
206
+ #
207
+ # @!attribute [r] methods
208
+ # @ return [Enumerable<Method>] The methods of this service.
209
+ class Service < Proto
210
+ extend Forwardable
211
+ attr_reader :methods
212
+
213
+ # Initializes a Service object.
214
+ # @param descriptor [Google::Protobuf::ServiceDescriptorProto] the
215
+ # protobuf representation of this service.
216
+ # @param address [Enumerable<String>] The address of the proto. See
217
+ # #address for more info.
218
+ # @param docs [Google::Protobuf::SourceCodeInfo::Location] The docs
219
+ # of the proto. See #docs for more info.
220
+ # @param methods [Enumerable<Method>] The methods of this service.
221
+ def initialize descriptor, address, docs, methods
222
+ super descriptor, address, docs
223
+ @methods = methods || []
224
+ @methods.each { |m| m.parent = self }
225
+ end
226
+
227
+ # @return [String] The hostname for this service
228
+ # (e.g. "foo.googleapis.com"). This should be specified with no
229
+ # prefix.
230
+ def host
231
+ options[:".google.api.default_host"] if options
232
+ end
233
+
234
+ # @return [Array<String>] The OAuth scopes information for the client.
235
+ def scopes
236
+ String(options[:".google.api.oauth_scopes"]).split "," if options
237
+ end
238
+
239
+ # @return [String] Ruby Package
240
+ def ruby_package
241
+ return nil if parent.nil?
242
+
243
+ parent.ruby_package
244
+ end
245
+
246
+ # @return [Array<Google::Api::ResourceDescriptor>] A representation of the resource.
247
+ # This is generally intended to be attached to the "name" field.
248
+ # See `google/api/resource.proto`.
249
+ def resources
250
+ require "gapic/resource_lookup"
251
+
252
+ @resources ||= Gapic::ResourceLookup.for_service self
253
+ end
254
+
255
+ # @!method name
256
+ # @return [String] the unqualified name of the service.
257
+ # @!method options
258
+ # @return [Google::Protobuf::ServiceOptions] the options of this
259
+ # service.
260
+ def_delegators(
261
+ :descriptor,
262
+ :name,
263
+ :options
264
+ )
265
+ end
266
+
267
+ # Wrapper for a protobuf method.
268
+ #
269
+ # @!attribute [r] input
270
+ # @ return [Message] The input message of this method.
271
+ # @!attribute [r] output
272
+ # @ return [Message] The output message of this method.
273
+ class Method < Proto
274
+ extend Forwardable
275
+ attr_reader :input, :output
276
+
277
+ # Initializes a method object.
278
+ # @param descriptor [Google::Protobuf::MethodDescriptorProto] the
279
+ # protobuf representation of this service.
280
+ # @param address [Enumerable<String>] The address of the proto. See
281
+ # #address for more info.
282
+ # @param docs [Google::Protobuf::SourceCodeInfo::Location] The docs
283
+ # of the proto. See #docs for more info.
284
+ # @param input [Message] The input message of this method.
285
+ # @param output [Message] The output message of this method.
286
+ def initialize descriptor, address, docs, input, output
287
+ super descriptor, address, docs
288
+ @input = input
289
+ @output = output
290
+ end
291
+
292
+ # @return [Array<Array<String>>] The parameter lists
293
+ # defined for this method. See `google/api/client.proto`.
294
+ def signatures
295
+ return [] if options.nil?
296
+
297
+ Array(options[:".google.api.method_signature"]).map do |sig|
298
+ String(sig).split ","
299
+ end
300
+ end
301
+
302
+ # @return [Google::Longrunning::OperationInfo] Additional information
303
+ # regarding long-running operations.
304
+ # In particular, this specifies the types that are returned from
305
+ # long-running operations.
306
+ # Required for methods that return `google.longrunning.Operation`;
307
+ # invalid otherwise.
308
+ def operation_info
309
+ options[:".google.longrunning.operation_info"] if options
310
+ end
311
+
312
+ # @return [Google::Api::HttpRule] The HTTP bindings for this method. See
313
+ # `google/api/http.proto`.
314
+ def http
315
+ options[:".google.api.http"] if options
316
+ end
317
+
318
+ # @!method name
319
+ # @return [String] the unqualified name of the method.
320
+ # @!method options
321
+ # @return [Google::Protobuf::MethodOptions] the options of this
322
+ # method.
323
+ # @!method client_streaming
324
+ # @return [Boolean]
325
+ # Identifies if client streams multiple client messages.
326
+ # @!method server_streaming
327
+ # @return [Boolean]
328
+ # Identifies if server streams multiple server messages.
329
+ def_delegators(
330
+ :descriptor,
331
+ :name,
332
+ :options,
333
+ :client_streaming,
334
+ :server_streaming
335
+ )
336
+ end
337
+
338
+ # Wrapper for a protobuf file.
339
+ #
340
+ # @!attribute [r] messages
341
+ # @ return [Enumerable<Message>] The top level messages contained in
342
+ # this file.
343
+ # @!attribute [r] enums
344
+ # @ return [Enumerable<Enum>] The top level enums contained in this
345
+ # file.
346
+ # @!attribute [r] services
347
+ # @ return [Enumerable<Service>] The services contained in this file.
348
+ class File < Proto
349
+ extend Forwardable
350
+ attr_reader :messages, :enums, :services, :registry
351
+
352
+ # Initializes a message object.
353
+ # @param descriptor [Google::Protobuf::DescriptorProto] the protobuf
354
+ # representation of this service.
355
+ # @param address [Enumerable<String>] The address of the proto. See
356
+ # #address for more info.
357
+ # @param docs [Google::Protobuf::SourceCodeInfo::Location] The docs
358
+ # of the proto. See #docs for more info.
359
+ # @param messages [Enumerable<Message>] The top level messages of this
360
+ # file.
361
+ # @param enums [Enumerable<Enum>] The top level enums of this file.
362
+ # @param services [Enumerable<Service>] The services of this file.
363
+ # @param generate [Boolean] Whether this file should be generated.
364
+ def initialize descriptor, address, docs, messages, enums, services,
365
+ generate, registry
366
+ super descriptor, address, docs
367
+ @messages = messages || []
368
+ @enums = enums || []
369
+ @services = services || []
370
+ @generate = generate
371
+ @registry = registry
372
+
373
+ # Apply parent
374
+ @messages.each { |m| m.parent = self }
375
+ @enums.each { |m| m.parent = self }
376
+ @services.each { |m| m.parent = self }
377
+ end
378
+
379
+ def lookup address
380
+ address = address.split(".").reject(&:empty?).join(".")
381
+ @registry[address]
382
+ end
383
+
384
+ def generate?
385
+ @generate
386
+ end
387
+
388
+ # @return [String] Ruby Package
389
+ def ruby_package
390
+ options[:ruby_package] if options
391
+ end
392
+
393
+ # @return [Array<Google::Api::ResourceDescriptor>] A representation of the resource.
394
+ # This is generally intended to be attached to the "name" field.
395
+ # See `google/api/resource.proto`.
396
+ def resources
397
+ options[:".google.api.resource_definition"] if options
398
+ end
399
+
400
+ # @!method name
401
+ # @return [String] file name, relative to root of source tree.
402
+ # @!method package
403
+ # @return [String] package of the file. e.g. "foo", "foo.bar", etc.
404
+ # @!method dependency
405
+ # @return [Array<String>] Names of files imported by this file.
406
+ # @!method public_dependency
407
+ # @return [Array<Integer>] Indexes of the public imported files in the
408
+ # dependency list returned by #dependency.
409
+ # @!method options
410
+ # @return [Google::Protobuf::FileOptions] the options of this file.
411
+ def_delegators(
412
+ :descriptor,
413
+ :name,
414
+ :package,
415
+ :dependency,
416
+ :public_dependency,
417
+ :options
418
+ )
419
+ end
420
+
421
+ # Wrapper for a protobuf enum.
422
+ #
423
+ # @!attribute [r] values
424
+ # @ return [EnumValue] the EnumValues contained in this file.
425
+ class Enum < Proto
426
+ extend Forwardable
427
+ attr_reader :values
428
+
429
+ # Initializes a message object.
430
+ # @param descriptor [Google::Protobuf::DescriptorProto] the protobuf
431
+ # representation of this service.
432
+ # @param address [Enumerable<String>] The address of the proto. See
433
+ # #address for more info.
434
+ # @param docs [Google::Protobuf::SourceCodeInfo::Location] The docs
435
+ # of the proto. See #docs for more info.
436
+ # @param values [Enumerable<EnumValue>] The EnumValues of this enum.
437
+ def initialize descriptor, address, docs, values
438
+ super descriptor, address, docs
439
+ @values = values || []
440
+ @values.each { |v| v.parent = self }
441
+ end
442
+
443
+ # @!method name
444
+ # @return [String] the unqualified name of the Enum.
445
+ # @!method options
446
+ # @return [Array<Google::Protobuf::EnumOptions>] the options of the
447
+ # enum.
448
+ def_delegators(
449
+ :descriptor,
450
+ :name,
451
+ :options
452
+ )
453
+ end
454
+
455
+ # Wrapper for a protobuf Enum Value.
456
+ class EnumValue < Proto
457
+ extend Forwardable
458
+
459
+ # Initializes a message object.
460
+ # @param descriptor [Google::Protobuf::DescriptorProto] the protobuf
461
+ # representation of this service.
462
+ # @param address [Enumerable<String>] The address of the proto. See
463
+ # #address for more info.
464
+ # @param docs [Google::Protobuf::SourceCodeInfo::Location] The docs
465
+ # of the proto. See #docs for more info.
466
+ def initialize descriptor, address, docs
467
+ super descriptor, address, docs
468
+ end
469
+
470
+ # @!method name
471
+ # @return [String] the unqualified name of the EnumValue
472
+ # @!method number
473
+ # @return [Integer] The number value.
474
+ # @!method options
475
+ # @return [Array<Google::Protobuf::EnumValueOptions>]
476
+ # The options of the enum value.
477
+ def_delegators(
478
+ :descriptor,
479
+ :name,
480
+ :number,
481
+ :options
482
+ )
483
+ end
484
+
485
+ # Wrapper for a protobuf Message.
486
+ #
487
+ # @!attribute [r] fields
488
+ # @ return [Enumerable<Field>] The fields of a message.
489
+ # @!attribute [r] extensions
490
+ # @ return [Enumerable<Field>] The extensions of a message.
491
+ # @!attribute [r] nested_messages
492
+ # @ return [Enumerable<Message>] The nested message declarations of a
493
+ # message.
494
+ # @!attribute [r] nested_enums
495
+ # @ return [Enumerable<Enum>] The nested enum declarations of a message.
496
+ class Message < Proto
497
+ extend Forwardable
498
+ attr_reader :fields, :extensions, :nested_messages, :nested_enums
499
+
500
+ # Initializes a message object.
501
+ # @param descriptor [Google::Protobuf::DescriptorProto] the protobuf
502
+ # representation of this service.
503
+ # @param address [Enumerable<String>] The address of the proto. See
504
+ # #address for more info.
505
+ # @param docs [Google::Protobuf::SourceCodeInfo::Location] The docs
506
+ # of the proto. See #docs for more info.
507
+ # @param fields [Enumerable<Field>] The fields of this message.
508
+ # @param extensions [Enumerable<Field>] The extensions of this message.
509
+ # @param nested_messages [Enumerable<Message>] The nested message
510
+ # declarations of this message.
511
+ # @param nested_enums [Enumerable<Enum>] The nested enum declarations
512
+ # of this message.
513
+ def initialize descriptor, address, docs, fields, extensions,
514
+ nested_messages, nested_enums
515
+ super descriptor, address, docs
516
+ @fields = fields || []
517
+ @extensions = extensions || []
518
+ @nested_messages = nested_messages || []
519
+ @nested_enums = nested_enums || []
520
+
521
+ @fields.each { |f| f.parent = self }
522
+ @extensions.each { |x| x.parent = self }
523
+ @nested_messages.each { |m| m.parent = self }
524
+ @nested_enums.each { |e| e.parent = self }
525
+ end
526
+
527
+ # @return [Google::Api::ResourceDescriptor] A representation of the resource.
528
+ # This is generally intended to be attached to the "name" field.
529
+ # See `google/api/resource.proto`.
530
+ def resource
531
+ options[:".google.api.resource"] if options
532
+ end
533
+
534
+ # @return [Boolean] whether this type is a map entry
535
+ def map_entry?
536
+ descriptor.options&.map_entry
537
+ end
538
+
539
+ # @!method name
540
+ # @return [String] the unqualified name of the message.
541
+ # @!method oneof_decl
542
+ # @return [Array<Google::Protobuf::OneofDescriptorProto>]
543
+ # The oneofs declared in this message.
544
+ # @!method options
545
+ # @return [Array<Google::Protobuf::MessageOptions>]
546
+ # The options of this meessage.
547
+ def_delegators(
548
+ :descriptor,
549
+ :name,
550
+ :oneof_decl,
551
+ :options
552
+ )
553
+ end
554
+
555
+ # Wrapper for a protobuf Field.
556
+ #
557
+ # @!attribute [r] message
558
+ # @ return [Message | nil] The message if the field is a message, nil
559
+ # otherwise.
560
+ # @!attribute [r] enum
561
+ # @ return [Enum | nil] The enum if the field is an enum, nil
562
+ # otherwise.
563
+ class Field < Proto
564
+ extend Forwardable
565
+ attr_reader :message, :enum
566
+ attr_writer :message, :enum
567
+
568
+ # Initializes a message object.
569
+ # @param descriptor [Google::Protobuf::FieldDescriptorProto] the
570
+ # protobuf representation of this service.
571
+ # @param address [Enumerable<String>] The address of the proto. See
572
+ # #address for more info.
573
+ # @param docs [Google::Protobuf::SourceCodeInfo::Location] The docs
574
+ # of the proto. See #docs for more info.
575
+ # @param message [Message | nil] The message if the field is a message,
576
+ # nil otherwise.
577
+ # @param enum [Enum | nil] The enum if the field is an enum, nil
578
+ # otherwise.
579
+ def initialize descriptor, address, docs, message, enum
580
+ super descriptor, address, docs
581
+ @message = message
582
+ @enum = enum
583
+ end
584
+
585
+ # Whether this field is a message.
586
+ # @return [Boolean]
587
+ def message?
588
+ return true if @message
589
+
590
+ false
591
+ end
592
+
593
+ # Whether this field is a repeated field.
594
+ # @return [Boolean]
595
+ def repeated?
596
+ label == Google::Protobuf::FieldDescriptorProto::Label::LABEL_REPEATED
597
+ end
598
+
599
+ # Whether this field is an enum.
600
+ # @return [Boolean]
601
+ def enum?
602
+ return true if @enum
603
+
604
+ false
605
+ end
606
+
607
+ # Whether this field is a map
608
+ # @return [Boolean]
609
+ def map?
610
+ return true if repeated? && @message&.map_entry?
611
+
612
+ false
613
+ end
614
+
615
+ # @return [String] A reference to another resource message or resource
616
+ # definition. See `google/api/resource.proto`.
617
+ def resource_reference
618
+ options[:".google.api.resource_reference"] if options
619
+ end
620
+
621
+ # @return [Array<Google::Api::FieldBehavior>] A designation of a
622
+ # specific field behavior (required, output only, etc.) in protobuf
623
+ # messages.
624
+ def field_behavior
625
+ return options[:".google.api.field_behavior"] if options
626
+
627
+ []
628
+ end
629
+
630
+ # Specifically denotes a field as optional. While all fields in protocol
631
+ # buffers are optional, this may be specified for emphasis if
632
+ # appropriate.
633
+ def optional?
634
+ field_behavior.include? Google::Api::FieldBehavior::OPTIONAL
635
+ end
636
+
637
+ # Denotes a field as required. This indicates that the field **must** be
638
+ # provided as part of the request, and failure to do so will cause an
639
+ # error (usually `INVALID_ARGUMENT`).
640
+ def required?
641
+ field_behavior.include? Google::Api::FieldBehavior::REQUIRED
642
+ end
643
+
644
+ # Denotes a field as output only. This indicates that the field is
645
+ # provided in responses, but including the field in a request does
646
+ # nothing (the server *must* ignore it and *must not* throw an error as
647
+ # a result of the field's presence).
648
+ def output_only?
649
+ field_behavior.include? Google::Api::FieldBehavior::OUTPUT_ONLY
650
+ end
651
+
652
+ # Denotes a field as input only. This indicates that the field is
653
+ # provided in requests, and the corresponding field is not included in
654
+ # output.
655
+ def input_only?
656
+ field_behavior.include? Google::Api::FieldBehavior::INPUT_ONLY
657
+ end
658
+
659
+ # Denotes a field as immutable. This indicates that the field may be set
660
+ # once in a request to create a resource, but may not be changed
661
+ # thereafter.
662
+ def immutable?
663
+ field_behavior.include? Google::Api::FieldBehavior::IMMUTABLE
664
+ end
665
+
666
+ # @!method name
667
+ # @return [String] the unqualified name of the field.
668
+ # @!method number
669
+ # @return [Integer] the number of the field.
670
+ # @!method label
671
+ # @return [Google::Protobuf::FieldDescriptorProto::Label]
672
+ # The label of the field.
673
+ # @!method type
674
+ # @return [Google::Protobuf::FieldDescriptorProto::Type]
675
+ # If type_name is set, this need not be set. If both this and
676
+ # type_name are set, this must be one of TYPE_ENUM, TYPE_MESSAGE or
677
+ # TYPE_GROUP.
678
+ # @!method type_name
679
+ # @return [String]
680
+ # For message and enum types, this is the name of the type. If the
681
+ # name starts with a '.', it is fully-qualified. Otherwise,
682
+ # C++-like scoping rules are used to find the type (i.e. first the
683
+ # nested types within this message are searched, then within the
684
+ # parent, on up to the root namespace).
685
+ # @!method default_value
686
+ # @return [String]
687
+ # For numeric types, contains the original text representation of
688
+ # the value. For booleans, "true" or "false". For strings, contains
689
+ # the default text contents (not escaped in any way). For bytes,
690
+ # contains the C escaped value. All bytes >= 128 are escaped.
691
+ # @!method oneof_index
692
+ # @return [Integer]
693
+ # If set, gives the index of a oneof in the containing type's
694
+ # oneof_decl list. This field is a member of that oneof.
695
+ # @!method json_name
696
+ # @return [String]
697
+ # JSON name of this field. The value is set by protocol compiler. If
698
+ # the user has set a "json_name" option on this field, that option's
699
+ # value will be used. Otherwise, it's deduced from the field's name
700
+ # by converting it to camelCase.
701
+ # @!method options
702
+ # @return [Google::Protobuf::FieldOptions] the options of this field.
703
+ def_delegators(
704
+ :descriptor,
705
+ :name,
706
+ :number,
707
+ :label,
708
+ :type,
709
+ :type_name,
710
+ :default_value,
711
+ :oneof_index,
712
+ :json_name,
713
+ :options
714
+ )
715
+ end
716
+ end
717
+ end