-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support RBS for aws-sdk-core and service gems (#2961)
- Loading branch information
Showing
57 changed files
with
1,657 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/rbs.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module AwsSdkCodeGenerator | ||
module RBS | ||
class << self | ||
def to_type(shape_ref, api) | ||
_, shape = Api.resolve(shape_ref, api) | ||
case shape['type'] | ||
when 'blob' then Api.streaming?(shape_ref, api) ? '::IO' : '::String' | ||
when 'boolean' then 'bool' | ||
when 'byte' then '::Integer' | ||
when 'character' then '::String' | ||
when 'double' then '::Float' | ||
when 'float' then '::Float' | ||
when 'integer' then '::Integer' | ||
when 'list' then "::Array[#{to_type(shape['member'], api)}]" | ||
when 'long' then '::Integer' | ||
when 'map' then "::Hash[#{to_type(shape['key'], api)}, #{to_type(shape['value'], api)}]" | ||
when 'string' | ||
if shape['enum'] | ||
"(#{shape['enum'].map { |e| "\"#{e}\"" }.join(" | ")})" | ||
elsif Api.streaming?(shape_ref, api) | ||
'::IO' | ||
else | ||
'::String' | ||
end | ||
when 'structure' | ||
if shape['document'] | ||
'untyped' | ||
else | ||
"Types::#{shape_ref['shape']}" | ||
end | ||
when 'timestamp' then '::Time' | ||
else | ||
raise "unhandled type #{shape['type'].inspect}" | ||
end | ||
end | ||
end | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/rbs/error_list.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module AwsSdkCodeGenerator | ||
module RBS | ||
class ErrorList | ||
include Enumerable | ||
|
||
def initialize(api:, module_name:) | ||
@api = api | ||
@module_name = module_name | ||
@errors = @api['shapes'].inject([]) do |es, (name, shape)| | ||
if error_struct?(shape) | ||
members = shape["members"].map do |member_name, member_body| | ||
MethodSignature.new( | ||
method_name: Underscore.underscore(member_name), | ||
overloads: ["() -> #{Docstring.ucfirst(member_body['type'] ||'::String')}"] | ||
) | ||
end | ||
es << { | ||
name: name, | ||
members: members, | ||
} | ||
end | ||
es | ||
end | ||
end | ||
|
||
def error_struct?(shape) | ||
shape['type'] == 'structure' && !!!shape['event'] && | ||
(shape['error'] || shape['exception']) | ||
end | ||
|
||
def to_a | ||
@errors | ||
end | ||
end | ||
end | ||
end |
159 changes: 159 additions & 0 deletions
159
...d_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/rbs/keyword_argument_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
# frozen_string_literal: true | ||
|
||
module AwsSdkCodeGenerator | ||
module RBS | ||
# similar to SyntaxExampleHash | ||
class KeywordArgumentBuilder | ||
include Helper | ||
|
||
attr_reader :newline | ||
|
||
def initialize(api:, shape:, newline:) | ||
@api = api | ||
@shape = shape | ||
@newline = newline | ||
end | ||
|
||
def format(indent: '') | ||
members_str = struct_members(@shape, indent, [], keyword: true) | ||
result = [] | ||
result << '' if newline | ||
result << members_str if !members_str.empty? | ||
result << indent if newline | ||
result.join(joint) | ||
end | ||
|
||
def struct(struct_shape, i, visited) | ||
members_str = struct_members(struct_shape, i, visited, keyword: false) | ||
result = ["{"] | ||
result << members_str if struct_shape['members']&.empty?&.! | ||
result << "#{i}}" | ||
result.join(joint) | ||
end | ||
|
||
def struct_members(struct_shape, i, visited, keyword:) | ||
lines = [] | ||
unless struct_shape['members'].nil? | ||
n = 0 | ||
struct_shape['members'].each do |member_name, member_ref| | ||
next if member_ref['documented'] === false | ||
more_indent = newline ? " " : "" | ||
if @api['shapes'][member_ref['shape']]['eventstream'] === true | ||
# FIXME: "input_event_stream_hander: EventStreams::#{member_ref['shape']}.new" | ||
lines << "#{i}#{more_indent}input_event_stream_hander: untyped," | ||
else | ||
lines << "#{i}#{more_indent}#{struct_member(struct_shape, member_name, member_ref, i, visited, keyword: keyword)}" | ||
end | ||
end | ||
end | ||
if lines.empty? | ||
"" | ||
else | ||
lines.join(joint).chomp(",") | ||
end | ||
end | ||
|
||
def struct_member(struct, member_name, member_ref, i, visited, keyword:) | ||
required = (struct['required'] || []).include?(member_name) | ||
if keyword | ||
"#{required ? '' : '?'}#{underscore(member_name)}: #{ref_value(member_ref, i + more_indent, visited)}," | ||
else | ||
"#{underscore(member_name)}: #{ref_value(member_ref, i + more_indent, visited)}#{required ? '' : '?'}," | ||
end | ||
end | ||
|
||
def ref_value(ref, i, visited) | ||
if visited.include?(ref['shape']) | ||
return "untyped" | ||
else | ||
visited = visited + [ref['shape']] | ||
end | ||
|
||
s = shape(ref) | ||
case s['type'] | ||
when 'structure' | ||
if ref['shape'] == 'AttributeValue' | ||
'untyped' | ||
else | ||
struct(s, i, visited) | ||
end | ||
when 'blob' | ||
if ref['streaming'] | ||
"::String | ::StringIO | ::File" # input only | ||
else | ||
"::String" | ||
end | ||
when 'list' then list(s, i, visited) | ||
when 'map' then map(s, i, visited) | ||
when 'boolean' then "bool" | ||
when 'integer', 'long' then '::Integer' | ||
when 'float', 'double' then '::Float' | ||
when 'byte' then '::Integer' | ||
when 'character' then '::String' | ||
when 'string' then string(ref) | ||
when 'timestamp' then '::Time' | ||
else raise "unsupported shape #{s['type'].inspect}" | ||
end | ||
end | ||
|
||
def list(list_shape, i, visited) | ||
member_ref = list_shape['member'] | ||
if complex?(member_ref) | ||
complex_list(member_ref, i, visited) | ||
else | ||
scalar_list(member_ref, i, visited) | ||
end | ||
end | ||
|
||
def scalar_list(member_ref, i, visited) | ||
"Array[#{ref_value(member_ref, i, visited)}]" | ||
end | ||
|
||
def complex_list(member_ref, i, visited) | ||
newline_indent = newline ? "\n#{i}" : "" | ||
"Array[#{newline_indent}#{more_indent}#{ref_value(member_ref, i + more_indent, visited)},#{newline_indent}]" | ||
end | ||
|
||
def complex?(ref) | ||
s = shape(ref) | ||
if s['type'] == 'structure' | ||
!ddb_av?(ref) | ||
else | ||
s['type'] == 'list' || s['type'] == 'map' | ||
end | ||
end | ||
|
||
def ddb_av?(ref) | ||
s = shape(ref) | ||
case s['type'] | ||
when 'list' then ddb_av?(s['member']) | ||
when 'structure' then ref['shape'] == 'AttributeValue' | ||
else false | ||
end | ||
end | ||
|
||
def map(map_shape, i, visited) | ||
key = string(map_shape['key']) | ||
value = ref_value(map_shape['value'], i + more_indent, visited) | ||
"Hash[#{key}, #{value}]" | ||
end | ||
|
||
def string(ref) | ||
string_shape = shape(ref) | ||
if string_shape['enum'] | ||
"(#{string_shape['enum'].map { |s| "\"#{s}\"" }.join(" | ")})" | ||
else ref['shape'] | ||
"::String" | ||
end | ||
end | ||
|
||
def more_indent | ||
newline ? " " : "" | ||
end | ||
|
||
def joint | ||
newline ? "\n" : " " | ||
end | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/rbs/method_signature.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module AwsSdkCodeGenerator | ||
module RBS | ||
class MethodSignature < Struct.new(:method_name, :overloads, keyword_init: true) | ||
def signature | ||
"def #{method_name}: #{overloads.join("\n #{" " * method_name.length}| ")}" | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.