Skip to content

Commit 1af1cbe

Browse files
authored
Improve code generation to avoid unused variable warnings (#3184)
1 parent 4bd249e commit 1af1cbe

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/endpoint_provider_class.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class EndpointProviderClass < View
77
# @option options [required, Service] :service
88
# @option options [required, Hash] :endpoint_rules
99
def initialize(options)
10+
@assigned_variables = []
1011
@service = options.fetch(:service)
1112
@endpoint_rules = @service.endpoint_rules
1213
# Used to collect metrics in the generated endpoint provider
@@ -33,10 +34,6 @@ def module_name
3334

3435
def endpoint_rules_code
3536
res = StringIO.new
36-
# map parameters first
37-
@endpoint_rules["parameters"].each do |k,_v|
38-
res << indent("#{underscore(k)} = parameters.#{underscore(k)}\n", 3)
39-
end
4037

4138
# map rules
4239
@endpoint_rules["rules"].each do |rule|
@@ -164,6 +161,7 @@ def conditions(conditions, level)
164161

165162
def condition(condition)
166163
if condition['assign']
164+
@assigned_variables << condition['assign']
167165
"(#{underscore(condition['assign'])} = #{fn(condition)})"
168166
else
169167
fn(condition)
@@ -173,7 +171,11 @@ def condition(condition)
173171
def str(s)
174172
if s.is_a?(Hash)
175173
if s['ref']
176-
underscore(s['ref'])
174+
if @assigned_variables.include?(s['ref'])
175+
underscore(s['ref'])
176+
else
177+
"parameters.#{underscore(s['ref'])}"
178+
end
177179
elsif s['fn']
178180
fn(s)
179181
else
@@ -195,7 +197,12 @@ def template_str(string, wrap=true)
195197

196198
def template_replace(value)
197199
indexes = value.split("#")
198-
res = underscore(indexes.shift)
200+
variable = indexes.shift
201+
res = if @assigned_variables.include?(variable)
202+
underscore(variable)
203+
else
204+
"parameters.#{underscore(variable)}"
205+
end
199206
res += indexes.map do |index|
200207
"['#{index}']"
201208
end.join("")
@@ -210,7 +217,11 @@ def fn(fn)
210217
def fn_arg(arg)
211218
if arg.is_a?(Hash)
212219
if arg['ref']
213-
underscore(arg['ref'])
220+
if @assigned_variables.include?(arg['ref'])
221+
underscore(arg['ref'])
222+
else
223+
"parameters.#{underscore(arg['ref'])}"
224+
end
214225
elsif arg['fn']
215226
fn(arg)
216227
else

0 commit comments

Comments
 (0)