Skip to content

Commit ec0341b

Browse files
committed
V4 improvements
1 parent 027515b commit ec0341b

File tree

11 files changed

+79
-62
lines changed

11 files changed

+79
-62
lines changed

gems/smithy-cbor/lib/smithy-cbor/codec.rb

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,25 @@ module CBOR
88
# Codec that serializes and deserializes in CBOR format.
99
# TODO:
1010
# * Update implementation to handle event streams
11-
# * Allow user to pass in their preferred type to deserialize
12-
# If it fails, resort to deserializing type on the shape.
1311
class Codec
14-
include Schema::Shapes
15-
1612
# @param [Hash] options
1713
def initialize(options = {})
1814
@options = options
1915
end
2016

2117
# @param [Shape] shape
2218
# @param [Object] data
23-
# @return [String, nil] the encoded bytes in CBOR format
19+
# @return [String, nil]
2420
def serialize(shape, data)
2521
Serializer.new(@options).serialize(shape, data)
2622
end
2723

2824
# @param [Shape] shape
2925
# @param [String] bytes
30-
# @param [Struct] type
31-
# @return [Object, Hash]
32-
def deserialize(shape, bytes, type = nil)
33-
Deserializer.new(@options).deserialize(shape, bytes, type)
26+
# @param [Object] target
27+
# @return [Object]
28+
def deserialize(shape, bytes, target = nil)
29+
Deserializer.new(@options).deserialize(shape, bytes, target)
3430
end
3531
end
3632
end

gems/smithy-cbor/lib/smithy-cbor/deserializer.rb

+23-23
Original file line numberDiff line numberDiff line change
@@ -12,78 +12,78 @@ def initialize(options = {})
1212
@options = options
1313
end
1414

15-
def deserialize(shape, bytes, type)
15+
def deserialize(shape, bytes, target)
1616
return {} if bytes.empty? || shape == Prelude::Unit
1717

18-
shape(shape, CBOR.decode(bytes), type)
18+
shape(shape, CBOR.decode(bytes), target)
1919
end
2020

2121
private
2222

23-
def shape(shape, value, type = nil)
23+
def shape(shape, value, target = nil)
2424
return nil if value.nil?
2525

2626
case shape
27-
when StructureShape then structure(shape, value, type)
28-
when UnionShape then union(shape, value, type)
29-
when ListShape then list(shape, value, type)
30-
when MapShape then map(shape, value, type)
27+
when ListShape then list(shape, value, target)
28+
when MapShape then map(shape, value, target)
29+
when StructureShape then structure(shape, value, target)
30+
when UnionShape then union(shape, value, target)
3131
else value
3232
end
3333
end
3434

35-
def list(shape, values, type = nil)
36-
type = [] if type.nil?
35+
def list(shape, values, target = nil)
36+
target = [] if target.nil?
3737
values.each do |value|
3838
next if value.nil? && !sparse?(shape)
3939

40-
type <<
40+
target <<
4141
if value.nil?
4242
nil
4343
else
4444
shape(shape.member.shape, value)
4545
end
4646
end
47-
type
47+
target
4848
end
4949

50-
def map(shape, values, type = nil)
51-
type = {} if type.nil?
50+
def map(shape, values, target = nil)
51+
target = {} if target.nil?
5252
values.each do |key, value|
5353
next if value.nil? && !sparse?(shape)
5454

55-
type[key] =
55+
target[key] =
5656
if value.nil?
5757
nil
5858
else
5959
shape(shape.value.shape, value)
6060
end
6161
end
62-
type
62+
target
6363
end
6464

65-
def structure(shape, values, type = nil)
65+
def structure(shape, values, target = nil)
6666
return Schema::EmptyStructure.new if shape == Prelude::Unit
6767

68-
type = shape.type.new if type.nil?
68+
target = shape.type.new if target.nil?
6969
values.each do |key, value|
7070
next unless shape.name_by_member_name?(key)
7171

7272
name = shape.name_by_member_name(key)
7373
member_shape = shape.member(name)
74-
type[name] = shape(member_shape.shape, value)
74+
target[name] = shape(member_shape.shape, value)
7575
end
76-
type
76+
target
7777
end
7878

79-
def union(shape, values, type = nil)
79+
def union(shape, values, target = nil)
8080
key, value = values.flatten
81-
return nil if key.nil? || key == ' __type'
81+
return nil if key.nil? || key == ' __target'
8282

8383
if shape.name_by_member_name?(key)
8484
member_name = shape.name_by_member_name(key)
85-
type = shape.member_type(member_name) if type.nil?
86-
type.new(shape(shape.member(member_name).shape, value))
85+
target = shape.member_type(member_name) if target.nil?
86+
target.new(shape(shape.member(member_name).shape, value))
8787
else
8888
shape.member_type(:unknown).new(key, value)
8989
end

gems/smithy-cbor/lib/smithy-cbor/serializer.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ def serialize(shape, data)
2020

2121
private
2222

23-
def shape(shape, values)
23+
def shape(shape, value)
2424
case shape
25-
when BlobShape then blob(values)
26-
when ListShape then list(shape, values)
27-
when MapShape then map(shape, values)
28-
when StructureShape then structure(shape, values)
29-
when UnionShape then union(shape, values)
30-
else values
25+
when BlobShape then blob(value)
26+
when ListShape then list(shape, value)
27+
when MapShape then map(shape, value)
28+
when StructureShape then structure(shape, value)
29+
when UnionShape then union(shape, value)
30+
else value
3131
end
3232
end
3333

gems/smithy-client/lib/smithy-client/plugins/protocol.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def call(context)
2424
class ParseHandler < Handler
2525
def call(context)
2626
output = @handler.call(context)
27-
output.error = context.config.protocol.parse_error(context) unless output.error
28-
output.data = context.config.protocol.parse_data(context) unless output.error
27+
output.error = context.config.protocol.parse_error(output) unless output.error
28+
output.data = context.config.protocol.parse_data(output) unless output.error
2929
output
3030
end
3131
end

gems/smithy-client/lib/smithy-client/rpc_v2_cbor/protocol.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def build_request(context)
1717
RequestBuilder.new(@options).build(context)
1818
end
1919

20-
def parse_error(context)
21-
ResponseParser.new(@options).parse_error(context)
20+
def parse_error(output)
21+
ResponseParser.new(@options).parse_error(output.context)
2222
end
2323

24-
def parse_data(context)
25-
ResponseParser.new(@options).parse_data(context)
24+
def parse_data(output)
25+
ResponseParser.new(@options).parse_data(output.context)
2626
end
2727

2828
def stub_data(service, operation, data)

gems/smithy-client/lib/smithy-client/rpc_v2_cbor/request_builder.rb

+22-14
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ def initialize(options = {})
1010
end
1111

1212
def build(context)
13-
context.request.headers['Smithy-Protocol'] = 'rpc-v2-cbor'
14-
context.request.http_method = 'POST'
15-
context.request.body = build_body(context)
16-
apply_content_type_header(context)
17-
apply_accept_header(context)
18-
build_url(context)
13+
apply_http_method(context)
14+
apply_headers(context)
15+
apply_body(context)
16+
apply_url_path(context)
1917
end
2018

2119
private
2220

23-
def build_body(context)
24-
@codec.serialize(context.operation.input, context.params)
21+
def apply_http_method(context)
22+
context.request.http_method = 'POST'
23+
end
24+
25+
def apply_headers(context)
26+
context.request.headers['Smithy-Protocol'] = 'rpc-v2-cbor'
27+
apply_content_type_header(context)
28+
apply_accept_header(context)
2529
end
2630

2731
def apply_content_type_header(context)
@@ -47,6 +51,16 @@ def apply_accept_header(context)
4751
context.request.headers['Accept'] ||= accept
4852
end
4953

54+
def apply_body(context)
55+
context.request.body = @codec.serialize(context.operation.input, context.params)
56+
end
57+
58+
def apply_url_path(context)
59+
base = context.request.endpoint
60+
service_name = context.config.service.name
61+
base.path += "/service/#{service_name}/operation/#{context.operation.name}"
62+
end
63+
5064
def event_stream?(shape)
5165
shape.members.each_value do |member_shape|
5266
return true if event_stream_shape?(member_shape.shape)
@@ -57,12 +71,6 @@ def event_stream?(shape)
5771
def event_stream_shape?(shape)
5872
shape.traits.include?('smithy.api#streaming') && shape.is_a?(Schema::Shapes::UnionShape)
5973
end
60-
61-
def build_url(context)
62-
base = context.request.endpoint
63-
service_name = context.config.service.name
64-
base.path += "/service/#{service_name}/operation/#{context.operation.name}"
65-
end
6674
end
6775
end
6876
end

gems/smithy-client/lib/smithy-client/rpc_v2_cbor/response_stubber.rb

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def stub_error(error_code)
2323
resp.status_code = 400
2424
resp.headers['Smithy-Protocol'] = 'rpc-v2-cbor'
2525
resp.headers['Content-Type'] = 'application/cbor'
26+
resp.headers['X-Amzn-RequestId'] = 'stubbed-request-id'
2627
data = { '__type' => error_code, 'message' => 'stubbed-error-message' }
2728
resp.body = CBOR.encode(data)
2829
resp

gems/smithy-client/lib/smithy-client/stubbing/protocol.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module Stubbing
77
# @api private
88
class Protocol
99
def build_request(_context); end
10-
def parse_data(_context); end
11-
def parse_error(_context); end
10+
def parse_data(_output); end
11+
def parse_error(_output); end
1212

1313
def stub_data(_service, _operation, data)
1414
resp = HTTP::Response.new

gems/smithy/lib/smithy/templates/client/protocol_spec.erb

+8
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ module <%= module_name %>
8686
response = { status_code: <%= test['code'] %> }
8787
<% if test['headers'] -%>
8888
response[:headers] = <%= test['headers'] %>
89+
<% else -%>
90+
response[:headers] = {}
8991
<% end -%>
9092
<% if test['body'] -%>
9193
response[:body] = <%= test.stub_body %>
94+
<% else -%>
95+
response[:body] = nil
9296
<% end -%>
9397

9498
client.stub_responses(:<%= operation_tests.name %>, response)
@@ -118,9 +122,13 @@ module <%= module_name %>
118122
response = { status_code: <%= test['code'] %> }
119123
<% if test['headers'] -%>
120124
response[:headers] = <%= test['headers'] %>
125+
<% else -%>
126+
response[:headers] = {}
121127
<% end -%>
122128
<% if test['body'] -%>
123129
response[:body] = <%= test.stub_body %>
130+
<% else -%>
131+
response[:body] = nil
124132
<% end -%>
125133

126134
client.stub_responses(:<%= operation_tests.name %>, response)

gems/smithy/lib/smithy/views/client/protocol_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def additional_requires
121121
case test_case['bodyMediaType']
122122
when 'application/cbor'
123123
%w[base64 cbor_value_matcher]
124+
when 'application/json'
125+
%w[json]
124126
else
125127
[]
126128
end
@@ -161,6 +163,8 @@ def body_expect
161163
when 'application/cbor'
162164
'expect(Smithy::CBOR.decode(request.body.read)).' \
163165
"to match_cbor(Smithy::CBOR.decode(::Base64.decode64('#{test_case['body']}')))"
166+
when 'application/json'
167+
"expect(JSON.parse(request.body.read)).to eq(JSON.parse(#{test_case['body']}))"
164168
else
165169
"expect(request.body.read).to eq('#{test_case['body']}')"
166170
end

gems/smithy/lib/smithy/welds.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
require_relative 'welds/http_digest_auth'
99
require_relative 'welds/plugins'
1010
require_relative 'welds/rpc_v2_cbor'
11-
require_relative 'welds/rubocop'
11+
# require_relative 'welds/rubocop'
1212

1313
module Smithy
1414
# @api private

0 commit comments

Comments
 (0)