Skip to content

Commit 0b3bb17

Browse files
author
Matt Muller
committed
Give prelude unit a type of empty structure
1 parent 9662011 commit 0b3bb17

4 files changed

Lines changed: 17 additions & 12 deletions

File tree

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ def map(ref, values, target = nil)
5353
target
5454
end
5555

56-
def structure(ref, values, target = nil) # rubocop:disable Metrics/AbcSize
57-
return Schema::EmptyStructure.new if ref.shape == Prelude::Unit
58-
56+
def structure(ref, values, target = nil)
5957
target = ref.shape.type.new if target.nil?
6058
ref.shape.members.each do |member_name, member_ref|
6159
value = values[member_ref.member_name]

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ def map(ref, values, target = nil)
6868

6969
def structure(ref, values, target = nil) # rubocop:disable Metrics/AbcSize
7070
return if values.nil?
71-
return Smithy::Schema::EmptyStructure.new if ref.shape == Prelude::Unit
7271

7372
target = ref.shape.type.new if target.nil?
7473
ref.shape.members.each do |member_name, member_ref|
75-
key = member_ref.traits['smithy.api#jsonName'] || member_ref.member_name
76-
value = values[key]
74+
value = values[location_name(member_ref)]
7775
value = default(member_ref) if value.nil? && default?(member_ref.traits)
7876
next if value.nil?
7977

@@ -104,7 +102,7 @@ def union(ref, values, target = nil) # rubocop:disable Metrics/AbcSize
104102
return nil if key.nil?
105103

106104
ref.shape.members.each do |member_name, member_ref|
107-
name = member_ref.traits['smithy.api#jsonName'] || member_ref.member_name
105+
name = location_name(member_ref)
108106
next unless values.key?(name)
109107

110108
target = ref.shape.member_type(member_name) if target.nil?
@@ -113,20 +111,23 @@ def union(ref, values, target = nil) # rubocop:disable Metrics/AbcSize
113111
ref.shape.member_type(:unknown).new(key, value)
114112
end
115113

116-
def sanitize_union!(ref, values) # rubocop:disable Metrics/CyclomaticComplexity
114+
def sanitize_union!(ref, values)
117115
return unless values.size > 1
118116

119117
# __type should be ignored unless it's a jsonName for a member
120118
type_as_name = false
121119
ref.shape.members.each_value do |member_ref|
122-
name = member_ref.traits['smithy.api#jsonName'] || member_ref.member_name
123-
type_as_name = true if name == '__type'
120+
type_as_name = true if location_name(member_ref) == '__type'
124121
end
125122

126123
values.delete('__type') if values.key?('__type') && !type_as_name
127124
raise ArgumentError, "union value includes more than one key, received: #{values.keys}" if values.size > 1
128125
end
129126

127+
def location_name(ref)
128+
ref.traits['smithy.api#jsonName'] || ref.member_name
129+
end
130+
130131
def sparse?(shape)
131132
shape.traits.include?('smithy.api#sparse')
132133
end

gems/smithy-schema/lib/smithy-schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

3-
require_relative 'smithy-schema/shapes'
43
require_relative 'smithy-schema/structure'
54
require_relative 'smithy-schema/union'
5+
require_relative 'smithy-schema/shapes'
66

77
module Smithy
88
# Base module for Smithy schema classes.

gems/smithy-schema/lib/smithy-schema/shapes.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ class StringShape < Shape; end
201201

202202
# Represents a Structure shape.
203203
class StructureShape < Structure
204+
def initialize(options = {})
205+
super
206+
@type = options[:type]
207+
end
208+
204209
# @return [Class]
205210
attr_accessor :type
206211
end
@@ -302,7 +307,8 @@ module Prelude
302307
Timestamp = TimestampShape.new(id: 'smithy.api#Timestamp')
303308
Unit = StructureShape.new(
304309
id: 'smithy.api#Unit',
305-
traits: { 'smithy.api#unitType' => {} }
310+
traits: { 'smithy.api#unitType' => {} },
311+
type: Schema::EmptyStructure
306312
)
307313
end
308314
end

0 commit comments

Comments
 (0)