gapic-generator 0.1.3 → 0.2.2

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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +49 -0
  3. data/lib/gapic/formatting_utils.rb +160 -0
  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 +2 -4
  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 +170 -0
  15. data/lib/gapic/presenters/message_presenter.rb +73 -0
  16. data/lib/gapic/presenters/method_presenter.rb +298 -0
  17. data/lib/gapic/presenters/package_presenter.rb +72 -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 +298 -0
  21. data/lib/gapic/resource_lookup.rb +8 -1
  22. data/lib/gapic/schema/api.rb +24 -0
  23. data/lib/gapic/schema/wrappers.rb +30 -8
  24. data/templates/default/gem/gemspec.erb +1 -1
  25. data/templates/default/gem/gitignore.erb +2 -0
  26. data/templates/default/gem/readme.erb +2 -2
  27. data/templates/default/gem/rubocop.erb +2 -0
  28. data/templates/default/helpers/default_helper.rb +2 -8
  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/service/client/_client.erb +7 -1
  33. data/templates/default/service/client/_paths.erb +1 -0
  34. data/templates/default/service/client/method/def/_options_defaults.erb +1 -1
  35. data/templates/default/service/client/method/def/_response_normal.erb +1 -1
  36. metadata +17 -14
  37. data/templates/default/helpers/presenter_helper.rb +0 -24
  38. data/templates/default/helpers/presenters/field_presenter.rb +0 -146
  39. data/templates/default/helpers/presenters/file_presenter.rb +0 -53
  40. data/templates/default/helpers/presenters/gem_presenter.rb +0 -140
  41. data/templates/default/helpers/presenters/message_presenter.rb +0 -66
  42. data/templates/default/helpers/presenters/method_presenter.rb +0 -293
  43. data/templates/default/helpers/presenters/package_presenter.rb +0 -65
  44. data/templates/default/helpers/presenters/resource_presenter.rb +0 -92
  45. data/templates/default/helpers/presenters/sample_presenter.rb +0 -74
  46. data/templates/default/helpers/presenters/service_presenter.rb +0 -276
@@ -14,20 +14,27 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- class EnumValuePresenter
18
- def initialize value
19
- @value = value
20
- end
17
+ module Gapic
18
+ module Presenters
19
+ ##
20
+ # A presenter for proto enum values.
21
+ #
22
+ class EnumValuePresenter
23
+ def initialize value
24
+ @value = value
25
+ end
21
26
 
22
- def name
23
- @value.name
24
- end
27
+ def name
28
+ @value.name
29
+ end
25
30
 
26
- def doc_description
27
- @value.docs_leading_comments
28
- end
31
+ def doc_description
32
+ @value.docs_leading_comments
33
+ end
29
34
 
30
- def number
31
- @value.number
35
+ def number
36
+ @value.number
37
+ end
38
+ end
32
39
  end
33
40
  end
@@ -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,170 @@
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 services
41
+ @services ||= begin
42
+ files = @api.generate_files
43
+ files.map(&:services).flatten.map { |s| ServicePresenter.new @api, s }
44
+ end
45
+ end
46
+
47
+ def proto_files
48
+ @proto_files ||= begin
49
+ files = @api.files
50
+ files = files.reject { |f| blacklist_protos.include? f.name }
51
+ files = files.reject { |f| f.messages.empty? && f.enums.empty? }
52
+ files.map { |f| FilePresenter.new @api, f }
53
+ end
54
+ end
55
+
56
+ def address
57
+ name.split("-").map(&:camelize)
58
+ end
59
+
60
+ def name
61
+ gem_config :name
62
+ end
63
+
64
+ def namespace
65
+ gem_config(:namespace) ||
66
+ fix_namespace(@api, name.split("-").map(&:camelize).join("::"))
67
+ end
68
+
69
+ def title
70
+ gem_config(:title) ||
71
+ namespace.split("::").join(" ")
72
+ end
73
+
74
+ def version
75
+ gem_config(:version) ||
76
+ "0.0.1"
77
+ end
78
+
79
+ def version_require
80
+ ruby_file_path @api, version_name_full
81
+ end
82
+
83
+ def version_file_path
84
+ "#{version_require}.rb"
85
+ end
86
+
87
+ def version_name_full
88
+ "#{namespace}::VERSION"
89
+ end
90
+
91
+ def authors
92
+ gem_config(:authors) ||
93
+ ["Google LLC"]
94
+ end
95
+
96
+ def email
97
+ gem_config(:email) ||
98
+ "googleapis-packages@google.com"
99
+ end
100
+
101
+ def description
102
+ gem_config(:description) ||
103
+ "#{name} is the official client library for the #{title} API."
104
+ end
105
+
106
+ def summary
107
+ gem_config(:summary) ||
108
+ "API Client library for the #{title} API"
109
+ end
110
+
111
+ def homepage
112
+ gem_config(:homepage) ||
113
+ "https://github.com/googleapis/googleapis"
114
+ end
115
+
116
+ def env_prefix
117
+ (gem_config(:env_prefix) || name.split("-").last).upcase
118
+ end
119
+
120
+ def iam_dependency?
121
+ @api.files.map(&:name).any? { |f| f.start_with? "google/iam/v1/" }
122
+ end
123
+
124
+ def library_documentation_url
125
+ gem_config(:library_documentation_url) || "https://googleapis.dev/ruby/#{name}/latest"
126
+ end
127
+
128
+ def product_documentation_url
129
+ gem_config :product_documentation_url
130
+ end
131
+
132
+ def api_id
133
+ gem_config :api_id
134
+ end
135
+
136
+ def free_tier?
137
+ gem_config(:free_tier) ? true : false
138
+ end
139
+
140
+ def yard_strict?
141
+ gem_config(:yard_strict) ? true : false
142
+ end
143
+
144
+ def entrypoint_require
145
+ packages.first.package_require
146
+ end
147
+
148
+ private
149
+
150
+ def gem_config key
151
+ return unless @api.configuration[:gem]
152
+
153
+ @api.configuration[:gem][key]
154
+ end
155
+
156
+ def blacklist_protos
157
+ blacklist = gem_config :blacklist
158
+
159
+ return default_blacklist_protos if blacklist.nil?
160
+ return default_blacklist_protos if blacklist[:protos].nil?
161
+
162
+ default_blacklist_protos[:protos]
163
+ end
164
+
165
+ def default_blacklist_protos
166
+ ["google/api/http.proto", "google/protobuf/descriptor.proto"]
167
+ end
168
+ end
169
+ end
170
+ 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