1- import { deref , ListSchema , MapSchema , StructureSchema } from "@smithy/core/schema" ;
1+ import { NormalizedSchema } from "@smithy/core/schema" ;
22import { 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" ;
134
145import { cbor } from "./cbor" ;
156import { dateToTag } from "./parseCborBody" ;
@@ -46,17 +37,19 @@ export class CborShapeSerializer implements ShapeSerializer<Uint8Array> {
4637 if ( _ instanceof Date ) {
4738 return dateToTag ( _ ) ;
4839 }
49- const schema = deref ( ( schemaRef as MemberSchema ) ?. [ 0 ] ?? schemaRef ) ;
5040 if ( _ instanceof Uint8Array ) {
5141 return _ ;
5242 }
53- const sparse = ( schema as TraitsSchema ) ?. traits ?. sparse ;
43+
44+ const ns = NormalizedSchema . of ( schemaRef ) ;
45+ const sparse = ! ! ns . getMergedTraits ( ) . sparse ;
46+
5447 if ( Array . isArray ( _ ) ) {
5548 if ( ! sparse ) {
5649 return _ . filter ( ( item ) => item != null ) ;
5750 }
5851 } else if ( _ && typeof _ === "object" ) {
59- if ( ! sparse ) {
52+ if ( ! sparse || ns . isStructSchema ( ) ) {
6053 for ( const [ k , v ] of Object . entries ( _ ) ) {
6154 if ( v == null ) {
6255 delete _ [ k ] ;
@@ -65,6 +58,7 @@ export class CborShapeSerializer implements ShapeSerializer<Uint8Array> {
6558 return _ ;
6659 }
6760 }
61+
6862 return _ ;
6963 } ) ;
7064 }
@@ -88,15 +82,20 @@ export class CborShapeDeserializer implements ShapeDeserializer {
8882 return this . readValue ( schema , data ) ;
8983 }
9084
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.
9492 return parseEpochTimestamp ( value ) ;
9593 }
96- if ( schema === "blob" || schema === "streaming-blob" ) {
94+ if ( ns . isBlobSchema ( ) ) {
9795 return value ;
9896 }
9997 }
98+
10099 switch ( typeof value ) {
101100 case "undefined" :
102101 case "boolean" :
@@ -116,38 +115,36 @@ export class CborShapeDeserializer implements ShapeDeserializer {
116115 if ( value instanceof Date ) {
117116 return value ;
118117 }
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 ;
126118
127119 if ( Array . isArray ( value ) ) {
128120 const newArray = [ ] ;
121+ const memberSchema = ns . getValueSchema ( ) ;
122+ const sparse = ns . isListSchema ( ) && ! ! ns . getMergedTraits ( ) . sparse ;
123+
129124 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 ( ) ;
135128 }
136129 }
137130 return newArray ;
138131 }
139132
140133 const newObject = { } as any ;
141134 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 ] ) ;
151148 }
152149 }
153150 return newObject ;
0 commit comments