Skip to content

Commit 94d4416

Browse files
authored
Introduces autoloading strategy for service gems (#3105)
1 parent b27b3bc commit 94d4416

File tree

41 files changed

+432
-209
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+432
-209
lines changed

.github/workflows/benchmark.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ jobs:
5252

5353
- name: Configure AWS credentials
5454
uses: aws-actions/configure-aws-credentials@v4
55+
continue-on-error: true
5556
with:
5657
role-to-assume: arn:aws:iam::373952703873:role/BenchmarkReporter
5758
role-session-name: benchmark-reporter
5859
aws-region: us-east-1
5960

6061
- name: Upload benchmark report
62+
if: ${{ env.AWS_ACCESS_KEY_ID != '' }}
6163
run: bundle exec rake benchmark:upload-report
6264

6365
- name: Put benchmark metrics
66+
if: ${{ env.AWS_ACCESS_KEY_ID != '' }}
6467
run: bundle exec rake benchmark:put-metrics

build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/helper.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,18 @@ def wrap_string(str, width, indent = '')
175175
str.gsub(/(.{1,#{width}})(\s+|\Z)/, "#{indent}\\1\n").chomp
176176
end
177177

178+
def gem_lib_path(gem_name)
179+
File.expand_path(
180+
File.join(
181+
__dir__,
182+
"../../../../gems/#{gem_name}/lib/"
183+
)
184+
)
185+
end
186+
178187
module_function :deep_copy, :operation_streaming?, :downcase_first, :wrap_string, :apig_prefix,
179-
:eventstream_output?, :eventstream_input?, :operation_eventstreaming?, :pascal_case
188+
:eventstream_output?, :eventstream_input?, :operation_eventstreaming?, :pascal_case,
189+
:gem_lib_path
180190

181191
end
182192
end

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,31 @@ def generated_src_warning
2323
GENERATED_SRC_WARNING
2424
end
2525

26+
# @return [String]
2627
def module_name
2728
@service.module_name
2829
end
2930

31+
# @return [Boolean]
32+
def customization_file_exists?
33+
File.exist?(
34+
File.join(
35+
Helper.gem_lib_path(gem_name), "#{customization_file_path}.rb"
36+
)
37+
)
38+
end
39+
40+
# @return [String]
41+
def customization_file_path
42+
"#{gem_name}/customizations/errors"
43+
end
44+
45+
private
46+
47+
# @return [String]
48+
def gem_name
49+
"aws-sdk-#{module_name.split('::').last.downcase}"
50+
end
3051
end
3152
end
3253
end

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,30 @@ def identifiers?
106106
@identifiers.size > 0
107107
end
108108

109+
# @return [Boolean]
110+
def customization_file_exists?
111+
File.exist?(
112+
File.join(
113+
Helper.gem_lib_path(gem_name), "#{resource_customization}.rb"
114+
)
115+
)
116+
end
117+
118+
# @return [String]
119+
def resource_customization
120+
"#{gem_name}/customizations/#{underscored_name}"
121+
end
122+
109123
private
110124

125+
def gem_name
126+
"aws-sdk-#{module_name.split('::').last.downcase}"
127+
end
128+
129+
def underscored_name
130+
class_name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
131+
end
132+
111133
def build_associations(options)
112134
ResourceAssociation.build_list(
113135
class_name: options.fetch(:class_name),

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ def documentation?
5353
actions? || associations?
5454
end
5555

56+
# @return [Boolean]
57+
def customization_file_exists?
58+
File.exist?(
59+
File.join(
60+
Helper.gem_lib_path(gem_name), "#{customization_file_path}.rb"
61+
)
62+
)
63+
end
64+
65+
# @return [String]
66+
def customization_file_path
67+
"#{gem_name}/customizations/resource"
68+
end
69+
70+
private
71+
72+
def gem_name
73+
"aws-sdk-#{module_name.split('::').last.downcase}"
74+
end
5675
end
5776
end
5877
end

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

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ServiceModule < View
1111
def initialize(options)
1212
@service = options.fetch(:service)
1313
@prefix = options.fetch(:prefix)
14-
@codegenerated_plugins = options.fetch(:codegenerated_plugins)
14+
@codegenerated_plugins = options.fetch(:codegenerated_plugins) || []
1515
end
1616

1717
# @return [String|nil]
@@ -60,55 +60,65 @@ def require_core_guard?
6060
@service.included_in_core?
6161
end
6262

63-
# @return [Array<String>]
64-
def relative_requires
65-
paths = Set.new
66-
paths << "#{@prefix}/types"
67-
paths << "#{@prefix}/client_api"
63+
# @return [Array<Hash>] list of autoload path hashes with :path, :class_name and
64+
# :is_plugin keys.
65+
def autoloads
66+
paths = []
67+
paths << auto_load("#{@prefix}/types", :Types)
68+
paths << auto_load("#{@prefix}/client_api", :ClientApi)
6869

6970
# these must be required before the client
70-
if @codegenerated_plugins
71-
paths += @codegenerated_plugins.map { | p| p.path }
71+
paths += @codegenerated_plugins.map do |p|
72+
auto_load(p.path, p.class_name.split('::').last, true)
7273
end
7374

74-
paths << "#{@prefix}/client"
75-
paths << "#{@prefix}/errors"
76-
paths << "#{@prefix}/waiters" if @service.waiters
77-
paths << "#{@prefix}/resource"
75+
paths << auto_load("#{@prefix}/client", :Client)
76+
paths << auto_load("#{@prefix}/errors", :Errors)
77+
paths << auto_load("#{@prefix}/waiters", :Waiters) if @service.waiters
78+
paths << auto_load("#{@prefix}/resource", :Resource)
7879

7980
unless @service.legacy_endpoints?
80-
paths << "#{@prefix}/endpoint_parameters"
81-
paths << "#{@prefix}/endpoint_provider"
82-
paths << "#{@prefix}/endpoints"
81+
paths << auto_load("#{@prefix}/endpoint_parameters", :EndpointParameters)
82+
paths << auto_load("#{@prefix}/endpoint_provider", :EndpointProvider)
83+
paths << auto_load("#{@prefix}/endpoints", :Endpoints)
8384
end
8485

8586
if @service.resources && @service.resources['resources']
8687
@service.resources['resources'].keys.each do |resource_name|
8788
path = "#{@prefix}/#{underscore(resource_name)}"
88-
if paths.include?(path)
89-
raise "resource path conflict for `#{resource_name}'"
90-
else
91-
paths << path
92-
end
89+
paths << auto_load(path, resource_name)
9390
end
9491
end
95-
paths << "#{@prefix}/customizations"
9692
if @service.api['metadata']['protocolSettings'] &&
97-
@service.api['metadata']['protocolSettings']['h2'] == 'eventstream'
98-
paths << "#{@prefix}/async_client"
99-
paths << "#{@prefix}/event_streams"
93+
@service.api['metadata']['protocolSettings']['h2'] == 'eventstream'
94+
paths << auto_load("#{@prefix}/async_client", :AsyncClient)
95+
paths << auto_load("#{@prefix}/event_streams", :EventStreams)
10096
elsif eventstream_shape?
101-
paths << "#{@prefix}/event_streams"
97+
paths << auto_load("#{@prefix}/event_streams", :EventStreams)
10298
end
103-
paths.to_a
99+
100+
paths
101+
end
102+
103+
def auto_load(path, class_name, is_plugin = false)
104+
{
105+
file_path: path,
106+
class_name: class_name,
107+
is_plugin: is_plugin
108+
}
109+
end
110+
111+
def service_identifier
112+
@prefix
104113
end
105114

106115
def example_var_name
107116
underscore(name)
108117
end
109118

110119
def example_operation_name
111-
raise "no operations found for the service" if @service.api['operations'].empty?
120+
raise 'no operations found for the service' if @service.api['operations'].empty?
121+
112122
underscore(@service.api['operations'].keys.first)
113123
end
114124

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,20 @@ def eventstreams
8080
end
8181
end
8282

83+
# @return [Array<String>]
84+
def types_customizations
85+
Dir.glob(File.join(Helper.gem_lib_path(gem_name), "#{gem_name}/customizations/types", '*.rb')).map do |file|
86+
filename = File.basename(file, '.rb')
87+
"#{gem_name}/customizations/types/#{filename}"
88+
end
89+
end
90+
8391
private
8492

93+
def gem_name
94+
"aws-sdk-#{module_name.split('::').last.downcase}"
95+
end
96+
8597
def struct_members(shape)
8698
return if shape['members'].nil?
8799
members = shape['members'].map do |member_name, member_ref|

build_tools/aws-sdk-code-generator/templates/client_api_module.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{{#generated_src_warning}}
44
{{generated_src_warning}}
55
{{/generated_src_warning}}
6+
67
module {{module_name}}
78
# @api private
89
module ClientApi

build_tools/aws-sdk-code-generator/templates/errors_module.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@ module {{module_name}}
6868
{{/errors}}
6969
end
7070
end
71+
{{#customization_file_exists?}}
72+
73+
# Load customizations if they exist
74+
require '{{customization_file_path}}'
75+
{{/customization_file_exists?}}

build_tools/aws-sdk-code-generator/templates/resource_class.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,8 @@ module {{module_name}}
304304
{{/batch_actions?}}
305305
end
306306
end
307+
{{#customization_file_exists?}}
308+
309+
# Load customizations if they exist
310+
require '{{resource_customization}}'
311+
{{/customization_file_exists?}}

0 commit comments

Comments
 (0)