22
33module Smithy
44 module Schema
5- # Registry that contains a map of Smithy shape ID to its shape representation
6- # TODO: Implement a method that takes a document and deserializes
7- # Do we include operation shapes in this registry?
5+ # A registry that contains a map of Smithy shape ID to its schema.
6+ # Also includes a way to find schema based on its shape type representation
87 class TypeRegistry
98 def initialize
109 @registry = { }
10+ @schema_by_types = { }
1111 end
1212
13+ # @api private
14+ attr_reader :schema_by_types
15+
1316 # @return [Hash<String, Shapes::Shape>]
1417 attr_accessor :registry
1518
@@ -18,28 +21,62 @@ def register(*shapes)
1821 raise ArgumentError , 'Expected an array of Shapes' unless shapes . all? ( Shapes ::Shape )
1922
2023 shapes . each do |s |
21- next if s . id . nil?
22-
2324 @registry [ s . id ] = s
25+
26+ case s . class
27+ when Shapes ::StructureShape
28+ @schema_by_types [ s . type ] = s if s . type
29+ when Shapes ::UnionShape
30+ s . member_types . values { |v | @schema_by_types [ v ] = s }
31+ end
2432 end
2533 end
2634
27- # @param [String] shape_id
35+ # Returns true if this type registry contains specific shape id.
36+ # @param [String] id
37+ # @return [Boolean]
38+ def schema_by_id? ( id )
39+ @registry . key? ( id )
40+ end
41+
42+ # Returns the shape schema registered for the given shape id.
43+ # @param [id] id
44+ # @return [Shapes::Shape, nil]
45+ def schema_by_id ( id )
46+ @registry [ id ]
47+ end
48+
49+ # Returns true if this type registry contains a schema associated
50+ # with the given typed shape.
51+ # @param [Class] type
2852 # @return [Boolean]
29- def shape? ( shape_id )
30- @registry . key? ( shape_id )
53+ def scheme_by_type? ( type )
54+ @schema_by_types . key? ( type )
55+ end
56+
57+ # Returns the shape schema registered for the given typed shape.
58+ # @param [Class] type
59+ # @return [Shapes::Shape, nil]
60+ def scheme_by_type ( type )
61+ @schema_by_types [ type ]
3162 end
3263
33- # @param [String] shape_id
34- # @return [Shapes::Shape]
35- def shape ( shape_id )
36- @registry [ shape_id ]
64+ # Deserializes a document into a typed shape from registry.
65+ # @param [Document] document
66+ # @return [Shapes::Structure] typed shape
67+ def convert_as_typed ( document )
68+ msg = 'Unable to convert given document since discriminator is not set'
69+ raise ArgumentError , msg unless document . discriminator
70+
71+ if schema_by_id? ( document . discriminator )
72+ document . as_typed ( schema_by_id ( document . discriminator ) )
73+ else
74+ msg = "Unable to find schema with #{ document . discriminator } in Registry"
75+ raise ArgumentError , msg
76+ end
3777 end
3878
3979 class << self
40- # TODO: Need thoughts on...
41- # * Smithy-Java only allows to compose 2 type registries at a time.
42- # Do we follow suit or allow unlimited number of registry to compose?
4380 # @param [Array<TypeRegistry>]
4481 # @return [TypeRegistry]
4582 def compose ( *type_registries )
0 commit comments