1
- import { deref , ListSchema , MapSchema , StructureSchema } from "@smithy/core/schema" ;
1
+ import { NormalizedSchema } from "@smithy/core/schema" ;
2
2
import { copyDocumentWithTransform , parseEpochTimestamp } from "@smithy/core/serde" ;
3
- import {
4
- Codec ,
5
- MemberSchema ,
6
- Schema ,
7
- SchemaRef ,
8
- SerdeContext ,
9
- ShapeDeserializer ,
10
- ShapeSerializer ,
11
- TraitsSchema ,
12
- } from "@smithy/types" ;
3
+ import { Codec , Schema , SchemaRef , SerdeContext , ShapeDeserializer , ShapeSerializer } from "@smithy/types" ;
13
4
14
5
import { cbor } from "./cbor" ;
15
6
import { dateToTag } from "./parseCborBody" ;
@@ -22,6 +13,7 @@ export class CborCodec implements Codec<Uint8Array, Uint8Array> {
22
13
serializer . setSerdeContext ( this . serdeContext ! ) ;
23
14
return serializer ;
24
15
}
16
+
25
17
public createDeserializer ( ) : CborShapeDeserializer {
26
18
const deserializer = new CborShapeDeserializer ( ) ;
27
19
deserializer . setSerdeContext ( this . serdeContext ! ) ;
@@ -46,17 +38,19 @@ export class CborShapeSerializer implements ShapeSerializer<Uint8Array> {
46
38
if ( _ instanceof Date ) {
47
39
return dateToTag ( _ ) ;
48
40
}
49
- const schema = deref ( ( schemaRef as MemberSchema ) ?. [ 0 ] ?? schemaRef ) ;
50
41
if ( _ instanceof Uint8Array ) {
51
42
return _ ;
52
43
}
53
- const sparse = ( schema as TraitsSchema ) ?. traits ?. sparse ;
44
+
45
+ const ns = NormalizedSchema . of ( schemaRef ) ;
46
+ const sparse = ! ! ns . getMergedTraits ( ) . sparse ;
47
+
54
48
if ( Array . isArray ( _ ) ) {
55
49
if ( ! sparse ) {
56
50
return _ . filter ( ( item ) => item != null ) ;
57
51
}
58
52
} else if ( _ && typeof _ === "object" ) {
59
- if ( ! sparse ) {
53
+ if ( ! sparse || ns . isStructSchema ( ) ) {
60
54
for ( const [ k , v ] of Object . entries ( _ ) ) {
61
55
if ( v == null ) {
62
56
delete _ [ k ] ;
@@ -65,6 +59,7 @@ export class CborShapeSerializer implements ShapeSerializer<Uint8Array> {
65
59
return _ ;
66
60
}
67
61
}
62
+
68
63
return _ ;
69
64
} ) ;
70
65
}
@@ -88,15 +83,20 @@ export class CborShapeDeserializer implements ShapeDeserializer {
88
83
return this . readValue ( schema , data ) ;
89
84
}
90
85
91
- private readValue ( schema : Schema , value : any ) : any {
92
- if ( typeof schema === "string" ) {
93
- if ( schema === "time" || schema === "epoch-seconds" || schema === "date-time" ) {
86
+ private readValue ( _schema : Schema , value : any ) : any {
87
+ const ns = NormalizedSchema . of ( _schema ) ;
88
+ const schema = ns . getSchema ( ) ;
89
+
90
+ if ( typeof schema === "number" ) {
91
+ if ( ns . isTimestampSchema ( ) ) {
92
+ // format is ignored.
94
93
return parseEpochTimestamp ( value ) ;
95
94
}
96
- if ( schema === "blob" || schema === "streaming-blob" ) {
95
+ if ( ns . isBlobSchema ( ) ) {
97
96
return value ;
98
97
}
99
98
}
99
+
100
100
switch ( typeof value ) {
101
101
case "undefined" :
102
102
case "boolean" :
@@ -116,38 +116,44 @@ export class CborShapeDeserializer implements ShapeDeserializer {
116
116
if ( value instanceof Date ) {
117
117
return value ;
118
118
}
119
- const traits =
120
- Array . isArray ( schema ) && schema . length >= 2
121
- ? {
122
- ...( deref ( ( schema as MemberSchema ) [ 0 ] ) as TraitsSchema ) ?. traits ,
123
- ...( schema as MemberSchema ) [ 1 ] ,
124
- }
125
- : ( deref ( schema ) as TraitsSchema ) ?. traits ;
126
-
127
- if ( Array . isArray ( value ) ) {
119
+ if ( ns . isDocumentSchema ( ) ) {
120
+ return value ;
121
+ }
122
+
123
+ if ( ns . isListSchema ( ) ) {
128
124
const newArray = [ ] ;
125
+ const memberSchema = ns . getValueSchema ( ) ;
126
+ const sparse = ns . isListSchema ( ) && ! ! ns . getMergedTraits ( ) . sparse ;
127
+
129
128
for ( const item of value ) {
130
- newArray . push ( this . readValue ( schema instanceof ListSchema ? deref ( schema . valueSchema ) : void 0 , item ) ) ;
131
- if ( ! traits ?. sparse ) {
132
- if ( newArray [ newArray . length - 1 ] == null ) {
133
- newArray . pop ( ) ;
134
- }
129
+ newArray . push ( this . readValue ( memberSchema , item ) ) ;
130
+ if ( ! sparse && newArray [ newArray . length - 1 ] == null ) {
131
+ newArray . pop ( ) ;
135
132
}
136
133
}
137
134
return newArray ;
138
135
}
139
136
140
137
const newObject = { } as any ;
141
- for ( const key of Object . keys ( value ) ) {
142
- const targetSchema =
143
- schema instanceof StructureSchema
144
- ? deref ( schema . members [ key ] ?. [ 0 ] )
145
- : schema instanceof MapSchema
146
- ? deref ( schema . valueSchema )
147
- : void 0 ;
148
- newObject [ key ] = this . readValue ( targetSchema , value [ key ] ) ;
149
- if ( ! traits ?. sparse && newObject [ key ] == null ) {
150
- delete newObject [ key ] ;
138
+
139
+ if ( ns . isMapSchema ( ) ) {
140
+ const sparse = ns . getMergedTraits ( ) . sparse ;
141
+ const targetSchema = ns . getValueSchema ( ) ;
142
+
143
+ for ( const key of Object . keys ( value ) ) {
144
+ newObject [ key ] = this . readValue ( targetSchema , value [ key ] ) ;
145
+
146
+ if ( newObject [ key ] == null && ! sparse ) {
147
+ delete newObject [ key ] ;
148
+ }
149
+ }
150
+ } else if ( ns . isStructSchema ( ) ) {
151
+ for ( const key of Object . keys ( value ) ) {
152
+ const targetSchema = ns . getMemberSchema ( key ) ;
153
+ if ( targetSchema === undefined ) {
154
+ continue ;
155
+ }
156
+ newObject [ key ] = this . readValue ( targetSchema , value [ key ] ) ;
151
157
}
152
158
}
153
159
return newObject ;
0 commit comments