1
1
# frozen_string_literal: true
2
2
3
- require_relative 'document_utilities '
3
+ require_relative 'document_utils '
4
4
5
5
module Smithy
6
6
module Schema
7
7
# TODO: need to address the following and more
8
8
# * documentation
9
9
class Document
10
- include DocumentUtilities
11
- def initialize ( data , schema = nil )
12
- @data = format_data ( data , schema )
13
- @discriminator = Extractor . discriminator ( data , schema )
14
- @schema = schema
10
+ def initialize ( data , options = { } )
11
+ @data = set_data ( data , options )
12
+ @discriminator = extract_discriminator ( data , options )
15
13
end
16
14
17
- attr_reader :data , :discriminator , :schema
15
+ attr_reader :data , :discriminator
18
16
19
17
def []( key )
20
18
return unless @data . is_a? ( Hash ) && @data . key? ( key )
21
19
22
20
@data [ key ]
23
21
end
24
22
25
- def as_typed ( schema )
23
+ def as_typed ( schema , opts = { } )
26
24
error_message = 'Invalid schema or document data'
27
25
raise ArgumentError , error_message unless valid_schema? ( schema ) && @data . is_a? ( Hash )
28
26
29
27
type = schema . type . new
30
- Applier . apply ( schema , @data , type )
28
+ DocumentUtils . apply ( @data , schema , type , opts )
31
29
end
32
30
33
31
private
@@ -36,22 +34,49 @@ def discriminator?(data)
36
34
data . is_a? ( Hash ) && data . key? ( '__type' )
37
35
end
38
36
39
- def format_data ( data , schema )
37
+ def extract_discriminator ( data , opts )
38
+ return if data . nil?
39
+
40
+ return unless discriminator? ( data ) || ( schema = opts [ :schema ] )
41
+
42
+ if discriminator? ( data )
43
+ data [ '__type' ]
44
+ else
45
+ error_message = "Expected a structure schema, given #{ schema . class } instead"
46
+ raise error_message unless valid_schema? ( schema )
47
+
48
+ schema . id
49
+ end
50
+ end
51
+
52
+ def set_data ( data , options )
40
53
return if data . nil?
41
54
42
55
case data
43
56
when Smithy ::Schema ::Structure
44
- if schema . nil? || !schema . is_a? ( Shapes ::StructureShape )
45
- raise ArgumentError , 'Unable to convert as document with given schema'
57
+ schema = options [ :schema ]
58
+ if schema . nil? || !valid_schema? ( schema )
59
+ raise ArgumentError , "Unable to convert to document with given schema: #{ schema } "
46
60
end
47
61
48
- Extractor . extract ( schema , data )
62
+ options = options . except ( :schema )
63
+ # case 1 - extract data from runtime shape, schema is required to know to properly extract
64
+ DocumentUtils . extract ( data , schema , options )
65
+
49
66
else
50
- data = data . except ( '__type' ) if discriminator? ( data )
51
- data # TODO: add some validation if schema exists
67
+ if discriminator? ( data )
68
+ # case 2 - extract typed data from parsed JSON
69
+ # Open question - if there is a schema given, should we validate that these
70
+ # two pieces work together?
71
+ data . except ( '__type' )
72
+ else
73
+ # case 3 - untyped data, we will need consolidate timestamps and such
74
+ DocumentUtils . format ( data )
75
+ end
52
76
end
53
77
end
54
78
79
+ # TODO: probably need to check if given runtime shape is a type of schema
55
80
def valid_schema? ( schema )
56
81
schema . is_a? ( Shapes ::StructureShape ) && !schema . type . nil?
57
82
end
0 commit comments