gapic-generator 0.1.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +55 -0
  3. data/lib/gapic/formatting_utils.rb +63 -12
  4. data/lib/gapic/generator/version.rb +1 -1
  5. data/lib/gapic/generators/base_generator.rb +0 -8
  6. data/lib/gapic/generators/default_generator.rb +13 -14
  7. data/lib/gapic/helpers/filepath_helper.rb +45 -0
  8. data/lib/gapic/helpers/namespace_helper.rb +51 -0
  9. data/lib/gapic/presenters.rb +44 -0
  10. data/{templates/default/helpers → lib/gapic}/presenters/enum_presenter.rb +19 -14
  11. data/{templates/default/helpers → lib/gapic}/presenters/enum_value_presenter.rb +19 -12
  12. data/lib/gapic/presenters/field_presenter.rb +154 -0
  13. data/lib/gapic/presenters/file_presenter.rb +59 -0
  14. data/lib/gapic/presenters/gem_presenter.rb +176 -0
  15. data/lib/gapic/presenters/message_presenter.rb +73 -0
  16. data/lib/gapic/presenters/method_presenter.rb +307 -0
  17. data/lib/gapic/presenters/package_presenter.rb +80 -0
  18. data/lib/gapic/presenters/resource_presenter.rb +99 -0
  19. data/lib/gapic/presenters/sample_presenter.rb +84 -0
  20. data/lib/gapic/presenters/service_presenter.rb +306 -0
  21. data/lib/gapic/resource_lookup.rb +23 -38
  22. data/lib/gapic/schema/api.rb +70 -0
  23. data/lib/gapic/schema/loader.rb +9 -2
  24. data/lib/gapic/schema/wrappers.rb +134 -24
  25. data/templates/default/gem/entrypoint.erb +8 -0
  26. data/templates/default/gem/gemspec.erb +2 -2
  27. data/templates/default/gem/readme.erb +17 -3
  28. data/templates/default/gem/rubocop.erb +13 -41
  29. data/templates/default/helpers/filepath_helper.rb +2 -21
  30. data/templates/default/helpers/namespace_helper.rb +2 -27
  31. data/templates/default/layouts/_ruby.erb +1 -3
  32. data/templates/default/lib/_package.erb +17 -0
  33. data/templates/default/lib/_service.erb +32 -0
  34. data/templates/default/package.erb +5 -5
  35. data/templates/default/service.erb +5 -7
  36. data/templates/default/service/_helpers.erb +3 -0
  37. data/templates/default/service/client/_client.erb +7 -17
  38. data/templates/default/service/client/_operations.erb +0 -4
  39. data/templates/default/service/client/_paths.erb +1 -0
  40. data/templates/default/service/client/method/def/_options_defaults.erb +1 -1
  41. data/templates/default/service/client/method/def/_response_normal.erb +1 -1
  42. data/templates/default/service/client/method/docs/_request_normal.erb +10 -5
  43. data/templates/default/service/client/method/docs/_request_streaming.erb +1 -1
  44. metadata +20 -15
  45. data/templates/default/helpers/presenter_helper.rb +0 -24
  46. data/templates/default/helpers/presenters/field_presenter.rb +0 -146
  47. data/templates/default/helpers/presenters/file_presenter.rb +0 -53
  48. data/templates/default/helpers/presenters/gem_presenter.rb +0 -140
  49. data/templates/default/helpers/presenters/message_presenter.rb +0 -66
  50. data/templates/default/helpers/presenters/method_presenter.rb +0 -293
  51. data/templates/default/helpers/presenters/package_presenter.rb +0 -65
  52. data/templates/default/helpers/presenters/resource_presenter.rb +0 -92
  53. data/templates/default/helpers/presenters/sample_presenter.rb +0 -74
  54. data/templates/default/helpers/presenters/service_presenter.rb +0 -276
  55. data/templates/default/service/client/_helpers.erb +0 -9
@@ -0,0 +1,154 @@
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 "active_support/inflector"
18
+ require "gapic/helpers/namespace_helper"
19
+
20
+ module Gapic
21
+ module Presenters
22
+ ##
23
+ # A presenter for proto fields.
24
+ #
25
+ class FieldPresenter
26
+ include Gapic::Helpers::NamespaceHelper
27
+
28
+ def initialize api, message, field
29
+ @api = api
30
+ @message = message
31
+ @field = field
32
+ end
33
+
34
+ def name
35
+ @field.name
36
+ end
37
+
38
+ def doc_types
39
+ field_doc_types @field, false
40
+ end
41
+
42
+ def doc_attribute_type
43
+ mode = @field.output_only? ? "r" : "rw"
44
+ "@!attribute [#{mode}] #{@field.name}"
45
+ end
46
+
47
+ def output_doc_types
48
+ field_doc_types @field, true
49
+ end
50
+
51
+ def doc_description
52
+ @field.docs_leading_comments
53
+ end
54
+
55
+ def default_value
56
+ single = default_singular_value
57
+ return "[#{single}]" if @field.repeated? && !@field.map?
58
+ single
59
+ end
60
+
61
+ def as_kwarg value: nil
62
+ "#{name}: #{value || name}"
63
+ end
64
+
65
+ # TODO: remove, only used in tests
66
+ def type_name
67
+ @field.type_name
68
+ end
69
+
70
+ def type_name_full
71
+ return nil if type_name.blank?
72
+ ruby_namespace @api, type_name
73
+ end
74
+
75
+ def message?
76
+ @field.message?
77
+ end
78
+
79
+ def enum?
80
+ @field.enum?
81
+ end
82
+
83
+ def repeated?
84
+ @field.repeated?
85
+ end
86
+
87
+ def map?
88
+ @field.map?
89
+ end
90
+
91
+ protected
92
+
93
+ def field_doc_types field, output
94
+ return field_map_type field.message, output if field.map?
95
+ base_type =
96
+ if field.message?
97
+ type = message_ruby_type field.message
98
+ output ? type : "#{type}, Hash"
99
+ elsif field.enum?
100
+ # TODO: handle when arg message is nil and enum is the type
101
+ message_ruby_type field.enum
102
+ else
103
+ case field.type
104
+ when 1, 2 then "Float"
105
+ when 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "Integer"
106
+ when 9, 12 then "String"
107
+ when 8 then "Boolean"
108
+ else
109
+ "Object"
110
+ end
111
+ end
112
+ field.repeated? ? "Array<#{base_type}>" : base_type
113
+ end
114
+
115
+ def field_map_type entry_message, output
116
+ key_field = value_field = nil
117
+ entry_message.fields.each do |field|
118
+ key_field = field if field.name == "key"
119
+ value_field = field if field.name == "value"
120
+ end
121
+ class_name = output ? "Google::Protobuf::Map" : "Hash"
122
+ if key_field && value_field
123
+ key_type = field_doc_types key_field, output
124
+ value_type = field_doc_types value_field, output
125
+ "#{class_name}{#{key_type} => #{value_type}}"
126
+ else
127
+ class_name
128
+ end
129
+ end
130
+
131
+ def default_singular_value
132
+ if @field.message?
133
+ "{}"
134
+ elsif @field.enum?
135
+ # TODO: select the first non-0 enum value
136
+ ":#{@field.enum.values.first.name}"
137
+ else
138
+ case @field.type
139
+ when 1, 2 then "3.5"
140
+ when 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "42"
141
+ when 9, 12 then "\"hello world\""
142
+ when 8 then "true"
143
+ else
144
+ "Object"
145
+ end
146
+ end
147
+ end
148
+
149
+ def message_ruby_type message
150
+ ruby_namespace @api, message.address.join(".")
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,59 @@
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/helpers/namespace_helper"
18
+
19
+ module Gapic
20
+ module Presenters
21
+ ##
22
+ # A presenter for proto files.
23
+ #
24
+ class FilePresenter
25
+ include Gapic::Helpers::NamespaceHelper
26
+
27
+ # @param file [Gapic::Schema::File] the file to present
28
+ def initialize api, file
29
+ @api = api
30
+ @file = file
31
+ end
32
+
33
+ def name
34
+ @file.name
35
+ end
36
+
37
+ def address
38
+ @file.address
39
+ end
40
+
41
+ def namespace
42
+ return @file.ruby_package if @file.ruby_package.present?
43
+ ruby_namespace_for_address address
44
+ end
45
+
46
+ def messages
47
+ @messages ||= @file.messages.map { |m| MessagePresenter.new @api, m }
48
+ end
49
+
50
+ def enums
51
+ @enums ||= @file.enums.map { |e| EnumPresenter.new e }
52
+ end
53
+
54
+ def docs_file_path
55
+ @file.name.gsub ".proto", ".rb"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 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/helpers/filepath_helper"
18
+ require "gapic/helpers/namespace_helper"
19
+
20
+ module Gapic
21
+ module Presenters
22
+ ##
23
+ # A presenter for gems.
24
+ #
25
+ class GemPresenter
26
+ include Gapic::Helpers::FilepathHelper
27
+ include Gapic::Helpers::NamespaceHelper
28
+
29
+ def initialize api
30
+ @api = api
31
+ end
32
+
33
+ def packages
34
+ @packages ||= begin
35
+ packages = @api.generate_files.map(&:package).uniq.sort
36
+ packages.map { |p| PackagePresenter.new @api, p }.delete_if(&:empty?)
37
+ end
38
+ end
39
+
40
+ def packages?
41
+ !packages.empty?
42
+ end
43
+
44
+ def services
45
+ @services ||= begin
46
+ files = @api.generate_files
47
+ files.map(&:services).flatten.map { |s| ServicePresenter.new @api, s }
48
+ end
49
+ end
50
+
51
+ def proto_files
52
+ @proto_files ||= begin
53
+ files = @api.files
54
+ files = files.reject { |f| blacklist_protos.include? f.name }
55
+ files = files.reject { |f| f.messages.empty? && f.enums.empty? }
56
+ files.map { |f| FilePresenter.new @api, f }
57
+ end
58
+ end
59
+
60
+ def address
61
+ name.split("-").map(&:camelize)
62
+ end
63
+
64
+ def name
65
+ gem_config :name
66
+ end
67
+
68
+ def namespace
69
+ gem_config(:namespace) ||
70
+ fix_namespace(@api, name.split("-").map(&:camelize).join("::"))
71
+ end
72
+
73
+ def title
74
+ gem_config(:title) ||
75
+ namespace.split("::").join(" ")
76
+ end
77
+
78
+ def version
79
+ gem_config(:version) ||
80
+ "0.0.1"
81
+ end
82
+
83
+ def version_require
84
+ ruby_file_path @api, version_name_full
85
+ end
86
+
87
+ def version_file_path
88
+ "#{version_require}.rb"
89
+ end
90
+
91
+ def version_name_full
92
+ "#{namespace}::VERSION"
93
+ end
94
+
95
+ def authors
96
+ gem_config(:authors) ||
97
+ ["Google LLC"]
98
+ end
99
+
100
+ def email
101
+ gem_config(:email) ||
102
+ "googleapis-packages@google.com"
103
+ end
104
+
105
+ def description
106
+ gem_config(:description) ||
107
+ "#{name} is the official client library for the #{title} API."
108
+ end
109
+
110
+ def summary
111
+ gem_config(:summary) ||
112
+ "API Client library for the #{title} API"
113
+ end
114
+
115
+ def homepage
116
+ gem_config(:homepage) ||
117
+ "https://github.com/googleapis/googleapis"
118
+ end
119
+
120
+ def env_prefix
121
+ (gem_config(:env_prefix) || name.split("-").last).upcase
122
+ end
123
+
124
+ def iam_dependency?
125
+ @api.files.map(&:name).any? { |f| f.start_with? "google/iam/v1/" }
126
+ end
127
+
128
+ def library_documentation_url
129
+ gem_config(:library_documentation_url) || "https://googleapis.dev/ruby/#{name}/latest"
130
+ end
131
+
132
+ def product_documentation_url
133
+ gem_config :product_documentation_url
134
+ end
135
+
136
+ def api_id
137
+ gem_config :api_id
138
+ end
139
+
140
+ def free_tier?
141
+ # Default to false unless the config is explicitly set to "true"
142
+ gem_config(:free_tier) == "true"
143
+ end
144
+
145
+ def yard_strict?
146
+ # Default to true unless the config is explicitly set to "false"
147
+ gem_config(:yard_strict) != "false"
148
+ end
149
+
150
+ def entrypoint_require
151
+ packages.first.package_require
152
+ end
153
+
154
+ private
155
+
156
+ def gem_config key
157
+ return unless @api.configuration[:gem]
158
+
159
+ @api.configuration[:gem][key]
160
+ end
161
+
162
+ def blacklist_protos
163
+ blacklist = gem_config :blacklist
164
+
165
+ return default_blacklist_protos if blacklist.nil?
166
+ return default_blacklist_protos if blacklist[:protos].nil?
167
+
168
+ default_blacklist_protos[:protos]
169
+ end
170
+
171
+ def default_blacklist_protos
172
+ ["google/api/http.proto", "google/protobuf/descriptor.proto"]
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,73 @@
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 "active_support/inflector"
18
+
19
+ require "gapic/helpers/namespace_helper"
20
+
21
+ module Gapic
22
+ module Presenters
23
+ ##
24
+ # A presenter for proto messages.
25
+ #
26
+ class MessagePresenter
27
+ include Gapic::Helpers::NamespaceHelper
28
+
29
+ def initialize api, message
30
+ @api = api
31
+ @message = message
32
+ end
33
+
34
+ def name
35
+ @message.name
36
+ end
37
+
38
+ def doc_types
39
+ type_name_full
40
+ end
41
+
42
+ def doc_description
43
+ @message.docs_leading_comments
44
+ end
45
+
46
+ def default_value
47
+ "{}"
48
+ end
49
+
50
+ def type_name_full
51
+ message_ruby_type @message
52
+ end
53
+
54
+ def fields
55
+ @fields = @message.fields.map { |f| FieldPresenter.new @api, @message, f }
56
+ end
57
+
58
+ def nested_enums
59
+ @nested_enums ||= @message.nested_enums.map { |e| EnumPresenter.new e }
60
+ end
61
+
62
+ def nested_messages
63
+ @nested_messages ||= @message.nested_messages.map { |m| MessagePresenter.new @api, m }
64
+ end
65
+
66
+ protected
67
+
68
+ def message_ruby_type message
69
+ ruby_namespace @api, message.address.join(".")
70
+ end
71
+ end
72
+ end
73
+ end