@@ -13,6 +13,14 @@ import type {
1313 SchemaRef ,
1414 SchemaTraits ,
1515 SchemaTraitsObject ,
16+ StaticErrorSchema ,
17+ StaticListSchema ,
18+ StaticMapSchema ,
19+ StaticOperationSchema ,
20+ StaticSchema ,
21+ StaticSchemaId ,
22+ StaticSimpleSchema ,
23+ StaticStructureSchema ,
1624 StreamingBlobSchema ,
1725 StringSchema ,
1826 TimestampDefaultSchema ,
@@ -22,11 +30,16 @@ import type {
2230import type { IdempotencyTokenBitMask , TraitBitVector } from "@smithy/types/src/schema/traits" ;
2331
2432import { deref } from "../deref" ;
25- import { ListSchema } from "./ListSchema" ;
26- import { MapSchema } from "./MapSchema" ;
33+ import type { ErrorSchema } from "./ErrorSchema" ;
34+ import { error } from "./ErrorSchema" ;
35+ import { list , ListSchema } from "./ListSchema" ;
36+ import { map , MapSchema } from "./MapSchema" ;
37+ import type { OperationSchema } from "./OperationSchema" ;
38+ import { op } from "./OperationSchema" ;
2739import { Schema } from "./Schema" ;
2840import type { SimpleSchema } from "./SimpleSchema" ;
29- import { StructureSchema } from "./StructureSchema" ;
41+ import { sim } from "./SimpleSchema" ;
42+ import { struct , StructureSchema } from "./StructureSchema" ;
3043import { translateTraits } from "./translateTraits" ;
3144
3245/**
@@ -67,13 +80,15 @@ export class NormalizedSchema implements INormalizedSchema {
6780 let schema = ref ;
6881 this . _isMemberSchema = false ;
6982
70- while ( Array . isArray ( _ref ) ) {
83+ while ( isMemberSchema ( _ref ) ) {
7184 traitStack . push ( _ref [ 1 ] ) ;
7285 _ref = _ref [ 0 ] ;
7386 schema = deref ( _ref ) ;
7487 this . _isMemberSchema = true ;
7588 }
7689
90+ if ( isStaticSchema ( schema ) ) schema = hydrate ( schema ) ;
91+
7792 if ( traitStack . length > 0 ) {
7893 this . memberTraits = { } ;
7994 for ( let i = traitStack . length - 1 ; i >= 0 ; -- i ) {
@@ -96,7 +111,8 @@ export class NormalizedSchema implements INormalizedSchema {
96111 this . schema = deref ( schema ) as Exclude < ISchema , MemberSchema | INormalizedSchema > ;
97112
98113 if ( this . schema && typeof this . schema === "object" ) {
99- this . traits = this . schema ?. traits ?? { } ;
114+ // excluded by the checked hydrate call above.
115+ this . traits = ( this . schema as Exclude < typeof this . schema , StaticSchema > ) ?. traits ?? { } ;
100116 } else {
101117 this . traits = 0 ;
102118 }
@@ -120,7 +136,7 @@ export class NormalizedSchema implements INormalizedSchema {
120136 if ( sc instanceof NormalizedSchema ) {
121137 return sc ;
122138 }
123- if ( Array . isArray ( sc ) ) {
139+ if ( isMemberSchema ( sc ) ) {
124140 const [ ns , traits ] = sc ;
125141 if ( ns instanceof NormalizedSchema ) {
126142 Object . assign ( ns . getMergedTraits ( ) , translateTraits ( traits ) ) ;
@@ -331,7 +347,7 @@ export class NormalizedSchema implements INormalizedSchema {
331347 if ( this . isStructSchema ( ) && struct . memberNames . includes ( memberName ) ) {
332348 const i = struct . memberNames . indexOf ( memberName ) ;
333349 const memberSchema = struct . memberList [ i ] ;
334- return member ( Array . isArray ( memberSchema ) ? memberSchema : [ memberSchema , 0 ] , memberName ) ;
350+ return member ( isMemberSchema ( memberSchema ) ? memberSchema : [ memberSchema , 0 ] , memberName ) ;
335351 }
336352 if ( this . isDocumentSchema ( ) ) {
337353 return member ( [ 15 satisfies DocumentSchema , 0 ] , memberName ) ;
@@ -409,3 +425,42 @@ function member(memberSchema: NormalizedSchema | [SchemaRef, SchemaTraits], memb
409425 const internalCtorAccess = NormalizedSchema as any ;
410426 return new internalCtorAccess ( memberSchema , memberName ) ;
411427}
428+
429+ /**
430+ * @internal
431+ * @returns a class instance version of a static schema.
432+ */
433+ export function hydrate ( ss : StaticSimpleSchema ) : SimpleSchema ;
434+ export function hydrate ( ss : StaticListSchema ) : ListSchema ;
435+ export function hydrate ( ss : StaticMapSchema ) : MapSchema ;
436+ export function hydrate ( ss : StaticStructureSchema ) : StructureSchema ;
437+ export function hydrate ( ss : StaticErrorSchema ) : ErrorSchema ;
438+ export function hydrate ( ss : StaticOperationSchema ) : OperationSchema ;
439+ export function hydrate (
440+ ss : StaticSchema
441+ ) : SimpleSchema | ListSchema | MapSchema | StructureSchema | ErrorSchema | OperationSchema ;
442+ export function hydrate (
443+ ss : StaticSchema
444+ ) : SimpleSchema | ListSchema | MapSchema | StructureSchema | ErrorSchema | OperationSchema {
445+ const [ id , ...rest ] = ss ;
446+ return (
447+ {
448+ [ 0 satisfies StaticSchemaId . Simple ] : sim ,
449+ [ 1 satisfies StaticSchemaId . List ] : list ,
450+ [ 2 satisfies StaticSchemaId . Map ] : map ,
451+ [ 3 satisfies StaticSchemaId . Struct ] : struct ,
452+ [ - 3 satisfies StaticSchemaId . Error ] : error ,
453+ [ 9 satisfies StaticSchemaId . Operation ] : op ,
454+ } [ id ] as Function
455+ ) . call ( null , ...rest ) ;
456+ }
457+
458+ /**
459+ * @internal
460+ */
461+ const isMemberSchema = ( sc : SchemaRef ) : sc is MemberSchema => Array . isArray ( sc ) && sc . length === 2 ;
462+
463+ /**
464+ * @internal
465+ */
466+ export const isStaticSchema = ( sc : SchemaRef ) : sc is StaticSchema => Array . isArray ( sc ) && sc . length >= 5 ;
0 commit comments