Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions gems/smithy-cbor/lib/smithy-cbor/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,26 @@ def blob(value)
end

def list(ref, values)
return if values.nil?

shape = ref.shape
values.collect do |value|
next if value.nil? && !sparse?(shape.traits)

value.nil? ? nil : shape(shape.member, value)
shape(shape.member, value)
end
end

def map(ref, values)
return if values.nil?

shape = ref.shape
values.each.with_object({}) do |(key, value), data|
next if value.nil? && !sparse?(shape.traits)

data[key] = value.nil? ? nil : shape(shape.value, value)
data[key] = shape(shape.value, value)
end
end

def structure(ref, values)
return if values.nil?

ref.shape.members.each_with_object({}) do |(member_name, member_ref), data|
value = values[member_name]
next if value.nil?
Expand All @@ -64,6 +66,8 @@ def structure(ref, values)
end

def union(ref, values) # rubocop:disable Metrics/AbcSize
return if values.nil?

data = {}
if values.is_a?(Schema::Union)
_name, member_ref = ref.shape.member_by_type(values.class)
Expand All @@ -77,10 +81,6 @@ def union(ref, values) # rubocop:disable Metrics/AbcSize
end
data
end

def sparse?(traits)
traits.include?('smithy.api#sparse')
end
end
end
end
1 change: 1 addition & 0 deletions gems/smithy-client/lib/smithy-client/param_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def each_base_class(shape_class, &)
end

add(UnionShape, Hash) { |h, _| h.dup }
add(UnionShape, Schema::Union)
end
end
end
20 changes: 9 additions & 11 deletions gems/smithy-json/lib/smithy-json/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ def float(value)
def list(ref, values)
return if values.nil?

shape = ref.shape
values.collect do |value|
next if value.nil? && !sparse?(ref.shape)

shape(ref.shape.member, value)
shape(shape.member, value)
end
end

def map(ref, values)
values.each.with_object({}) do |(key, value), data|
next if value.nil? && !sparse?(ref.shape)
return if values.nil?

data[key] = shape(ref.shape.value, value)
shape = ref.shape
values.each.with_object({}) do |(key, value), data|
data[key] = shape(shape.value, value)
end
end

Expand All @@ -87,7 +87,9 @@ def timestamp(ref, value)
end
end

def union(ref, values)
def union(ref, values) # rubocop:disable Metrics/AbcSize
return if values.nil?

data = {}
if values.is_a?(Smithy::Schema::Union)
_name, member_ref = ref.shape.member_by_type(values.class)
Expand All @@ -102,10 +104,6 @@ def union(ref, values)
data
end

def sparse?(shape)
shape.traits.include?('smithy.api#sparse')
end

def location_name(ref)
return ref.member_name unless @json_name

Expand Down
136 changes: 97 additions & 39 deletions gems/smithy-schema/lib/smithy-schema/shapes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,6 @@ def []=(key, value)
end
end

# Represents an aggregate shape that has members.
class Structure < Shape
def initialize(options = {})
super
@members = {}
end

# @return [Hash<Symbol, ShapeRef>]
attr_accessor :members

# @return [ShapeRef]
def add_member(name, shape_ref)
@members[name] = shape_ref
end

# @param [Symbol] name
# @return [Boolean]
def member?(name)
@members.key?(name)
end

# @param [Symbol] name
# @return [ShapeRef, nil]
def member(name)
@members[name]
end
end

# Represents a slim variation of the Service shape.
class ServiceShape < Shape
include Enumerable
Expand Down Expand Up @@ -170,13 +142,63 @@ class BooleanShape < Shape; end
class DocumentShape < Shape; end

# Represents an Enum shape.
class EnumShape < Structure; end
class EnumShape < Shape
def initialize(options = {})
super
@members = {}
end

# @return [Hash<Symbol, ShapeRef>]
attr_accessor :members

# @return [ShapeRef]
def add_member(name, shape_ref)
@members[name] = shape_ref
end

# @param [Symbol] name
# @return [Boolean]
def member?(name)
@members.key?(name)
end

# @param [Symbol] name
# @return [ShapeRef, nil]
def member(name)
@members[name]
end
end

# Represents the following shapes: Byte, Short, Integer, Long, BigInteger.
class IntegerShape < Shape; end

# Represents an IntEnum shape.
class IntEnumShape < Structure; end
class IntEnumShape < Shape
def initialize(options = {})
super
@members = {}
end

# @return [Hash<Symbol, ShapeRef>]
attr_accessor :members

# @return [ShapeRef]
def add_member(name, shape_ref)
@members[name] = shape_ref
end

# @param [Symbol] name
# @return [Boolean]
def member?(name)
@members.key?(name)
end

# @param [Symbol] name
# @return [ShapeRef, nil]
def member(name)
@members[name]
end
end

# Represents both Float and Double shapes.
class FloatShape < Shape; end
Expand All @@ -200,41 +222,77 @@ class MapShape < Shape
class StringShape < Shape; end

# Represents a Structure shape.
class StructureShape < Structure
class StructureShape < Shape
def initialize(options = {})
super
@type = options[:type]
@members = {}
end

# @return [Hash<Symbol, ShapeRef>]
attr_accessor :members

# @return [Class]
attr_accessor :type

# @return [ShapeRef]
def add_member(name, shape_ref)
@members[name] = shape_ref
end

# @param [Symbol] name
# @return [Boolean]
def member?(name)
@members.key?(name)
end

# @param [Symbol] name
# @return [ShapeRef, nil]
def member(name)
@members[name]
end
end

# Represents a Timestamp shape.
class TimestampShape < Shape; end

# Represents both Union and EventStream shapes.
class UnionShape < Structure
class UnionShape < Shape
def initialize(options = {})
super
@members = {}
@member_types = {}
@members_by_type = {}
end

# @return [Class]
attr_accessor :type
# @return [Hash<Symbol, ShapeRef>]
attr_accessor :members

# @return [Hash<Symbol, Class>]
attr_reader :member_types

# @return [Hash<Class, ShapeRef>]
# @return [Hash<Class, [String, ShapeRef]>]
attr_reader :members_by_type

# @return [Class]
attr_accessor :type

# @return [ShapeRef]
def add_member(name, type, shape_ref)
@member_types[name] = type
@members_by_type[type] = [name, shape_ref]
super(name, shape_ref)
@members[name] = shape_ref
end

# @param [Symbol] name
# @return [Boolean]
def member?(name)
@members.key?(name)
end

# @param [Symbol] name
# @return [ShapeRef, nil]
def member(name)
@members[name]
end

# @param [Symbol] name
Expand Down Expand Up @@ -307,9 +365,9 @@ module Prelude
Timestamp = TimestampShape.new(id: 'smithy.api#Timestamp')
Unit = StructureShape.new(
id: 'smithy.api#Unit',
traits: { 'smithy.api#unitType' => {} },
type: Schema::EmptyStructure
traits: { 'smithy.api#unitType' => {} }
)
Unit.type = Schema::EmptyStructure
end
end
end
Expand Down
33 changes: 20 additions & 13 deletions gems/smithy-schema/sig/smithy-schema/shapes.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ module Smithy
def []=: (Symbol, Object) -> void
end

class Structure < Shape
attr_accessor members: Hash[Symbol, ShapeRef]
def add_member: (Symbol, ShapeRef) -> ShapeRef
def member?: (Symbol?) -> bool
def member: (Symbol) -> ShapeRef?
end

class ServiceShape < Shape
include Enumerable[Shapes::OperationShape]
attr_accessor name: String
Expand Down Expand Up @@ -56,13 +49,21 @@ module Smithy
class DocumentShape < Shape
end

class EnumShape < Structure
class EnumShape < Shape
attr_accessor members: Hash[Symbol, ShapeRef]
def add_member: (Symbol, ShapeRef) -> ShapeRef
def member?: (Symbol?) -> bool
def member: (Symbol) -> ShapeRef?
end

class IntegerShape < Shape
end

class IntEnumShape < Structure
class IntEnumShape < Shape
attr_accessor members: Hash[Symbol, ShapeRef]
def add_member: (Symbol, ShapeRef) -> ShapeRef
def member?: (Symbol?) -> bool
def member: (Symbol) -> ShapeRef?
end

class FloatShape < Shape
Expand All @@ -80,19 +81,25 @@ module Smithy
class StringShape < Shape
end

class StructureShape < Structure
class StructureShape < Shape
attr_accessor members: Hash[Symbol, ShapeRef]
attr_accessor type: Class
def add_member: (Symbol, ShapeRef) -> ShapeRef
def member?: (Symbol?) -> bool
def member: (Symbol) -> ShapeRef?
end

class TimestampShape < Shape
end

class UnionShape < Structure
attr_accessor type: Class
class UnionShape < Shape
attr_accessor members: Hash[Symbol, ShapeRef]
attr_accessor member_types: Hash[Symbol, Class]
attr_accessor members_by_type: Hash[Class, [Symbol, ShapeRef]]

attr_accessor type: Class
def add_member: (Symbol, Class, ShapeRef) -> ShapeRef
def member?: (Symbol?) -> bool
def member: (Symbol) -> ShapeRef?
def member_type?: (Symbol) -> bool
def member_type: (Symbol) -> Class?
def member_by_type?: (Class) -> bool
Expand Down
Loading