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" ;
@@ -46,17 +37,19 @@ export class CborShapeSerializer implements ShapeSerializer<Uint8Array> {
46
37
if ( _ instanceof Date ) {
47
38
return dateToTag ( _ ) ;
48
39
}
49
- const schema = deref ( ( schemaRef as MemberSchema ) ?. [ 0 ] ?? schemaRef ) ;
50
40
if ( _ instanceof Uint8Array ) {
51
41
return _ ;
52
42
}
53
- const sparse = ( schema as TraitsSchema ) ?. traits ?. sparse ;
43
+
44
+ const ns = NormalizedSchema . of ( schemaRef ) ;
45
+ const sparse = ! ! ns . getMergedTraits ( ) . sparse ;
46
+
54
47
if ( Array . isArray ( _ ) ) {
55
48
if ( ! sparse ) {
56
49
return _ . filter ( ( item ) => item != null ) ;
57
50
}
58
51
} else if ( _ && typeof _ === "object" ) {
59
- if ( ! sparse ) {
52
+ if ( ! sparse || ns . isStructSchema ( ) ) {
60
53
for ( const [ k , v ] of Object . entries ( _ ) ) {
61
54
if ( v == null ) {
62
55
delete _ [ k ] ;
@@ -65,6 +58,7 @@ export class CborShapeSerializer implements ShapeSerializer<Uint8Array> {
65
58
return _ ;
66
59
}
67
60
}
61
+
68
62
return _ ;
69
63
} ) ;
70
64
}
@@ -88,15 +82,20 @@ export class CborShapeDeserializer implements ShapeDeserializer {
88
82
return this . readValue ( schema , data ) ;
89
83
}
90
84
91
- private readValue ( schema : Schema , value : any ) : any {
92
- if ( typeof schema === "string" ) {
93
- if ( schema === "time" || schema === "epoch-seconds" || schema === "date-time" ) {
85
+ private readValue ( _schema : Schema , value : any ) : any {
86
+ const ns = NormalizedSchema . of ( _schema ) ;
87
+ const schema = ns . getSchema ( ) ;
88
+
89
+ if ( typeof schema === "number" ) {
90
+ if ( ns . isTimestampSchema ( ) ) {
91
+ // format is ignored.
94
92
return parseEpochTimestamp ( value ) ;
95
93
}
96
- if ( schema === "blob" || schema === "streaming-blob" ) {
94
+ if ( ns . isBlobSchema ( ) ) {
97
95
return value ;
98
96
}
99
97
}
98
+
100
99
switch ( typeof value ) {
101
100
case "undefined" :
102
101
case "boolean" :
@@ -116,38 +115,36 @@ export class CborShapeDeserializer implements ShapeDeserializer {
116
115
if ( value instanceof Date ) {
117
116
return value ;
118
117
}
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
118
127
119
if ( Array . isArray ( value ) ) {
128
120
const newArray = [ ] ;
121
+ const memberSchema = ns . getValueSchema ( ) ;
122
+ const sparse = ns . isListSchema ( ) && ! ! ns . getMergedTraits ( ) . sparse ;
123
+
129
124
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
- }
125
+ newArray . push ( this . readValue ( memberSchema , item ) ) ;
126
+ if ( ! sparse && newArray [ newArray . length - 1 ] == null ) {
127
+ newArray . pop ( ) ;
135
128
}
136
129
}
137
130
return newArray ;
138
131
}
139
132
140
133
const newObject = { } as any ;
141
134
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 ] ;
135
+ if ( ns . isMapSchema ( ) || ns . isDocumentSchema ( ) ) {
136
+ const targetSchema = ns . getValueSchema ( ) ;
137
+ newObject [ key ] = this . readValue ( targetSchema , value [ key ] ) ;
138
+
139
+ if ( ns . isMapSchema ( ) && newObject [ key ] == null && ! ns . getMergedTraits ( ) . sparse ) {
140
+ delete newObject [ key ] ;
141
+ }
142
+ } else if ( ns . isStructSchema ( ) ) {
143
+ const targetSchema = ns . getMemberSchema ( key ) ;
144
+ if ( targetSchema === undefined ) {
145
+ continue ;
146
+ }
147
+ newObject [ key ] = this . readValue ( targetSchema , value [ key ] ) ;
151
148
}
152
149
}
153
150
return newObject ;
0 commit comments