aws-sdk-code-generator 0.3.0.pre → 0.4.0.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f47bca9edb4689814cec21486fbc9924716462ea8dc0cf69e05d82b11c95d3b
4
- data.tar.gz: 7173e29d52ac6118968e30d35e750cfe2a702e9c4a320041c46ab534f6c5d018
3
+ metadata.gz: b4bb64ca30811529d0c5c4149e3d46b7eabef0d3068672ca06ed6d9fc97369f5
4
+ data.tar.gz: c2e56887b3686a58569da5fa643a8cb69bcca35c4b0f24dbdd67e48c3d3675c8
5
5
  SHA512:
6
- metadata.gz: c009d5dec4abd1d587254c564eeb88c89bd08d687070ae539ea4ea0fc64d747edc8b28641f7544ea9860363aa7467a631fae896d815cde99cc3502c7b7b844e2
7
- data.tar.gz: b195875c8e9dd16ba6373784bd786dc79663a6e0f421c2835462cd1bccc407e2168d5a98947bffa6fc31ea1eb65ba27fca67047c83ef2a9d65987e4dbd3902d0
6
+ metadata.gz: 3a6b20c93b4c58ef3bfac9e2d48551540106fa907deb17b72bb00876c807a2dbf05079b38634534a18a563a6f0b6affd98564a94a16370ba4e3d7efcf34a1034
7
+ data.tar.gz: 63b927d59a16241bc32e5ec51d647ae6dcc952c562e9eedccf50451bf62a64065d3833be2b8b0d7ffef6114300068b426bf3a96617c0c97f8bb9932b3ff45605
@@ -26,8 +26,227 @@ module AwsSdkCodeGenerator
26
26
  @service.module_name
27
27
  end
28
28
 
29
- def endpoint_rules_encoded
30
- Base64.encode64(JSON.dump(@endpoint_rules))
29
+ def endpoint_rules_code
30
+ res = StringIO.new
31
+ # map parameters first
32
+ @endpoint_rules["parameters"].each do |k,_v|
33
+ res << indent("#{underscore(k)} = parameters.#{underscore(k)}\n", 3)
34
+ end
35
+
36
+ # map rules
37
+ @endpoint_rules["rules"].each do |rule|
38
+ case rule["type"]
39
+ when "endpoint"
40
+ res << endpoint_rule(rule, 3)
41
+ when "error"
42
+ res << error_rule(rule, 3)
43
+ when "tree"
44
+ res << tree_rule(rule, 3)
45
+ else
46
+ raise "Unknown rule type: #{rule['type']}"
47
+ end
48
+ end
49
+
50
+ # if no rules match, raise an error
51
+ res << indent("raise ArgumentError, 'No endpoint could be resolved'\n", 3)
52
+
53
+ res.string
54
+ end
55
+
56
+ private
57
+
58
+ def endpoint_rule(rule, levels=3)
59
+ res = StringIO.new
60
+ if rule['conditions'] && !rule['conditions'].empty?
61
+ res << conditions(rule['conditions'], levels)
62
+ res << endpoint(rule['endpoint'], levels+1)
63
+ res << indent("end\n", levels)
64
+ else
65
+ res << endpoint(rule['endpoint'], levels)
66
+ end
67
+ res.string
68
+ end
69
+
70
+ def endpoint(endpoint, levels)
71
+ res = StringIO.new
72
+ res << "return Aws::Endpoints::Endpoint.new(url: #{str(endpoint['url'])}"
73
+ if endpoint['headers']
74
+ res << ", headers: #{templated_hash_to_s(endpoint['headers'])}"
75
+ end
76
+ if endpoint['properties']
77
+ res << ", properties: #{templated_hash_to_s(endpoint['properties'])}"
78
+ end
79
+ res << ")\n"
80
+ indent(res.string, levels)
81
+ end
82
+
83
+ def templated_hash_to_s(hash)
84
+ template_hash_values(hash).to_s.gsub('\#{', '#{') # unescape references
85
+ end
86
+
87
+ def template_hash_values(hash)
88
+ hash.inject({}) { |h, (k, v)| h[k] = template_hash_value(v); h }
89
+ end
90
+
91
+ def template_hash_value(value)
92
+ case value
93
+ when Hash
94
+ template_hash_values(value)
95
+ when Array
96
+ value.map { |v| template_hash_value(v) }
97
+ when String
98
+ template_str(value, false)
99
+ else
100
+ value
101
+ end
102
+ end
103
+
104
+ def error_rule(rule, levels=3)
105
+ res = StringIO.new
106
+ if rule['conditions'] && !rule['conditions'].empty?
107
+ res << conditions(rule['conditions'], levels)
108
+ res << error(rule['error'], levels+1)
109
+ res << indent("end\n", levels)
110
+ else
111
+ res << error(rule['error'], levels)
112
+ end
113
+ res.string
114
+ end
115
+
116
+ def error(error, levels)
117
+ indent("raise ArgumentError, #{str(error)}\n", levels)
118
+ end
119
+
120
+ def tree_rule(rule, levels=3)
121
+ res = StringIO.new
122
+ if rule['conditions'] && !rule['conditions'].empty?
123
+ res << conditions(rule['conditions'], levels)
124
+ res << tree_rules(rule['rules'], levels+1)
125
+ res << indent("end\n", levels)
126
+ else
127
+ res << tree_rules(rule['rules'], levels)
128
+ end
129
+ res.string
130
+ end
131
+
132
+ def tree_rules(rules, levels)
133
+ res = StringIO.new
134
+ rules.each do |rule|
135
+ case rule["type"]
136
+ when "endpoint"
137
+ res << endpoint_rule(rule, levels)
138
+ when "error"
139
+ res << error_rule(rule, levels)
140
+ when "tree"
141
+ res << tree_rule(rule, levels)
142
+ else
143
+ raise "Unknown rule type: #{rule['type']}"
144
+ end
145
+ end
146
+ res.string
147
+ end
148
+
149
+ def conditions(conditions, level)
150
+ res = StringIO.new
151
+ cnd_str = conditions.map { |c| condition(c) }.join(" && ")
152
+ res << indent("if #{cnd_str}\n", level)
153
+ res.string
154
+ end
155
+
156
+ def condition(condition)
157
+ if condition['assign']
158
+ "(#{underscore(condition['assign'])} = #{fn(condition)})"
159
+ else
160
+ fn(condition)
161
+ end
162
+ end
163
+
164
+ def str(s)
165
+ if s.is_a?(Hash)
166
+ if s['ref']
167
+ underscore(s['ref'])
168
+ elsif s['fn']
169
+ fn(s)
170
+ else
171
+ raise "Unknown string type: #{s}"
172
+ end
173
+ else
174
+ template_str(s)
175
+ end
176
+ end
177
+
178
+ def template_str(string, wrap=true)
179
+ string.scan(/\{.+?\}/).each do |capture|
180
+ value = capture[1..-2] # strips curly brackets
181
+ string = string.gsub(capture, '#{' + template_replace(value) + '}')
182
+ end
183
+ string = string.gsub('"', '\"')
184
+ wrap ? "\"#{string}\"" : string
185
+ end
186
+
187
+ def template_replace(value)
188
+ indexes = value.split("#")
189
+ res = underscore(indexes.shift)
190
+ res += indexes.map do |index|
191
+ "['#{index}']"
192
+ end.join("")
193
+ res
194
+ end
195
+
196
+ def fn(fn)
197
+ args = fn['argv'].map {|arg| fn_arg(arg)}.join(", ")
198
+ "#{fn_name(fn['fn'])}(#{args})"
199
+ end
200
+
201
+ def fn_arg(arg)
202
+ if arg.is_a?(Hash)
203
+ if arg['ref']
204
+ underscore(arg['ref'])
205
+ elsif arg['fn']
206
+ fn(arg)
207
+ else
208
+ raise "Unexpected argument type: #{arg}"
209
+ end
210
+ elsif arg.is_a?(String)
211
+ template_str(arg)
212
+ else
213
+ arg
214
+ end
215
+ end
216
+
217
+ def fn_name(fn)
218
+ case fn
219
+ when 'isSet'
220
+ "Aws::Endpoints::Matchers.set?"
221
+ when 'not'
222
+ "Aws::Endpoints::Matchers.not"
223
+ when 'getAttr'
224
+ "Aws::Endpoints::Matchers.attr"
225
+ when 'substring'
226
+ "Aws::Endpoints::Matchers.substring"
227
+ when 'stringEquals'
228
+ "Aws::Endpoints::Matchers.string_equals?"
229
+ when 'booleanEquals'
230
+ "Aws::Endpoints::Matchers.boolean_equals?"
231
+ when 'uriEncode'
232
+ "Aws::Endpoints::Matchers.uri_encode"
233
+ when 'parseURL'
234
+ "Aws::Endpoints::Matchers.parse_url"
235
+ when 'isValidHostLabel'
236
+ "Aws::Endpoints::Matchers.valid_host_label?"
237
+ when 'aws.partition'
238
+ "Aws::Endpoints::Matchers.aws_partition"
239
+ when 'aws.parseArn'
240
+ "Aws::Endpoints::Matchers.aws_parse_arn"
241
+ when 'aws.isVirtualHostableS3Bucket'
242
+ "Aws::Endpoints::Matchers.aws_virtual_hostable_s3_bucket?"
243
+ else
244
+ raise "Function not found: #{@fn}"
245
+ end
246
+ end
247
+
248
+ def indent(s, levels=3)
249
+ (" " * levels) + s
31
250
  end
32
251
  end
33
252
  end
@@ -5,26 +5,8 @@
5
5
  {{/generated_src_warning}}
6
6
  module {{module_name}}
7
7
  class EndpointProvider
8
- def initialize(rule_set = nil)
9
- @@rule_set ||= begin
10
- endpoint_rules = Aws::Json.load(Base64.decode64(RULES))
11
- Aws::Endpoints::RuleSet.new(
12
- version: endpoint_rules['version'],
13
- service_id: endpoint_rules['serviceId'],
14
- parameters: endpoint_rules['parameters'],
15
- rules: endpoint_rules['rules']
16
- )
17
- end
18
- @provider = Aws::Endpoints::RulesProvider.new(rule_set || @@rule_set)
19
- end
20
-
21
8
  def resolve_endpoint(parameters)
22
- @provider.resolve_endpoint(parameters)
9
+ {{{ endpoint_rules_code }}}
23
10
  end
24
-
25
- # @api private
26
- RULES = <<-JSON
27
- {{endpoint_rules_encoded}}
28
- JSON
29
11
  end
30
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-sdk-code-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.pre
4
+ version: 0.4.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amazon Web Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-20 00:00:00.000000000 Z
11
+ date: 2023-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kramdown