@@ -15,20 +15,26 @@ import { SimpleSchema } from "./SimpleSchema";
1515import { StructureSchema } from "./StructureSchema" ;
1616
1717/**
18- * Wraps SchemaRef values for easier handling.
19- * @internal
18+ * Wraps both class instances, numeric sentinel values, and member schema pairs.
19+ * Presents a consistent interface for interacting with polymorphic schema representations.
20+ *
21+ * @public
2022 */
2123export class NormalizedSchema implements INormalizedSchema {
22- public readonly name : string ;
23- public readonly traits : SchemaTraits ;
24+ private readonly name : string ;
25+ private readonly traits : SchemaTraits ;
2426
2527 private _isMemberSchema : boolean ;
26- private schema : Exclude < ISchema , MemberSchema > ;
28+ private schema : Exclude < ISchema , MemberSchema | INormalizedSchema > ;
2729 private memberTraits : SchemaTraits ;
2830 private normalizedTraits ?: SchemaTraitsObject ;
2931
32+ /**
33+ * @param ref - a polymorphic SchemaRef to be dereferenced/normalized.
34+ * @param memberName - optional memberName if this NormalizedSchema should be considered a member schema.
35+ */
3036 public constructor (
31- public readonly ref : SchemaRef ,
37+ private readonly ref : SchemaRef ,
3238 private memberName ?: string
3339 ) {
3440 const traitStack = [ ] as SchemaTraits [ ] ;
@@ -65,7 +71,7 @@ export class NormalizedSchema implements INormalizedSchema {
6571 return ;
6672 }
6773
68- this . schema = deref ( schema ) as Exclude < ISchema , MemberSchema > ;
74+ this . schema = deref ( schema ) as Exclude < ISchema , MemberSchema | INormalizedSchema > ;
6975
7076 if ( this . schema && typeof this . schema === "object" ) {
7177 this . traits = this . schema ?. traits ?? { } ;
@@ -83,6 +89,9 @@ export class NormalizedSchema implements INormalizedSchema {
8389 }
8490 }
8591
92+ /**
93+ * Static constructor that attempts to avoid wrapping a NormalizedSchema within another.
94+ */
8695 public static of ( ref : SchemaRef , memberName ?: string ) : NormalizedSchema {
8796 if ( ref instanceof NormalizedSchema ) {
8897 return ref ;
@@ -124,7 +133,13 @@ export class NormalizedSchema implements INormalizedSchema {
124133 return traits ;
125134 }
126135
127- private static memberFrom ( memberSchema : [ SchemaRef , SchemaTraits ] , memberName : string ) : NormalizedSchema {
136+ /**
137+ * Creates a normalized member schema from the given schema and member name.
138+ */
139+ private static memberFrom (
140+ memberSchema : NormalizedSchema | [ SchemaRef , SchemaTraits ] ,
141+ memberName : string
142+ ) : NormalizedSchema {
128143 if ( memberSchema instanceof NormalizedSchema ) {
129144 memberSchema . memberName = memberName ;
130145 memberSchema . _isMemberSchema = true ;
@@ -133,16 +148,23 @@ export class NormalizedSchema implements INormalizedSchema {
133148 return new NormalizedSchema ( memberSchema , memberName ) ;
134149 }
135150
136- public getSchema ( ) : Exclude < ISchema , MemberSchema > {
151+ /**
152+ * @returns the underlying non-normalized schema.
153+ */
154+ public getSchema ( ) : Exclude < ISchema , MemberSchema | INormalizedSchema > {
137155 if ( this . schema instanceof NormalizedSchema ) {
138156 return ( this . schema = this . schema . getSchema ( ) ) ;
139157 }
140158 if ( this . schema instanceof SimpleSchema ) {
141- return deref ( this . schema . schemaRef ) as Exclude < ISchema , MemberSchema > ;
159+ return deref ( this . schema . schemaRef ) as Exclude < ISchema , MemberSchema | INormalizedSchema > ;
142160 }
143- return deref ( this . schema ) as Exclude < ISchema , MemberSchema > ;
161+ return deref ( this . schema ) as Exclude < ISchema , MemberSchema | INormalizedSchema > ;
144162 }
145163
164+ /**
165+ * @param withNamespace - qualifies the name.
166+ * @returns e.g. `MyShape` or `com.namespace#MyShape`.
167+ */
146168 public getName ( withNamespace = false ) : string | undefined {
147169 if ( ! withNamespace ) {
148170 if ( this . name && this . name . includes ( "#" ) ) {
@@ -153,6 +175,10 @@ export class NormalizedSchema implements INormalizedSchema {
153175 return this . name || undefined ;
154176 }
155177
178+ /**
179+ * @returns the member name if the schema is a member schema.
180+ * @throws Error when the schema isn't a member schema.
181+ */
156182 public getMemberName ( ) : string {
157183 if ( ! this . isMemberSchema ( ) ) {
158184 throw new Error ( `@smithy/core/schema - cannot get member name on non-member schema: ${ this . getName ( true ) } ` ) ;
@@ -168,6 +194,9 @@ export class NormalizedSchema implements INormalizedSchema {
168194 return this . getSchema ( ) === ( "unit" as const ) ;
169195 }
170196
197+ /**
198+ * boolean methods on this class help control flow in shape serialization and deserialization.
199+ */
171200 public isListSchema ( ) : boolean {
172201 const inner = this . getSchema ( ) ;
173202 if ( typeof inner === "number" ) {
@@ -230,6 +259,10 @@ export class NormalizedSchema implements INormalizedSchema {
230259 return this . getSchema ( ) === SCHEMA . STREAMING_BLOB ;
231260 }
232261
262+ /**
263+ * @returns own traits merged with member traits, where member traits of the same trait key take priority.
264+ * This method is cached.
265+ */
233266 public getMergedTraits ( ) : SchemaTraitsObject {
234267 if ( this . normalizedTraits ) {
235268 return this . normalizedTraits ;
@@ -241,14 +274,26 @@ export class NormalizedSchema implements INormalizedSchema {
241274 return this . normalizedTraits ;
242275 }
243276
277+ /**
278+ * @returns only the member traits. If the schema is not a member, this returns empty.
279+ */
244280 public getMemberTraits ( ) : SchemaTraitsObject {
245281 return NormalizedSchema . translateTraits ( this . memberTraits ) ;
246282 }
247283
284+ /**
285+ * @returns only the traits inherent to the shape or member target shape if this schema is a member.
286+ * If there are any member traits they are excluded.
287+ */
248288 public getOwnTraits ( ) : SchemaTraitsObject {
249289 return NormalizedSchema . translateTraits ( this . traits ) ;
250290 }
251291
292+ /**
293+ * @returns the map's key's schema. Returns a dummy Document schema if this schema is a Document.
294+ *
295+ * @throws Error if the schema is not a Map or Document.
296+ */
252297 public getKeySchema ( ) : NormalizedSchema {
253298 if ( this . isDocumentSchema ( ) ) {
254299 return NormalizedSchema . memberFrom ( [ SCHEMA . DOCUMENT , 0 ] , "key" ) ;
@@ -263,6 +308,12 @@ export class NormalizedSchema implements INormalizedSchema {
263308 return NormalizedSchema . memberFrom ( [ ( schema as MapSchema ) . keySchema , 0 ] , "key" ) ;
264309 }
265310
311+ /**
312+ * @returns the schema of the map's value or list's member.
313+ * Returns a dummy Document schema if this schema is a Document.
314+ *
315+ * @throws Error if the schema is not a Map, List, nor Document.
316+ */
266317 public getValueSchema ( ) : NormalizedSchema {
267318 const schema = this . getSchema ( ) ;
268319
@@ -295,12 +346,21 @@ export class NormalizedSchema implements INormalizedSchema {
295346 throw new Error ( `@smithy/core/schema - the schema ${ this . getName ( true ) } does not have a value member.` ) ;
296347 }
297348
298- public getMemberSchema ( member : string ) : NormalizedSchema | undefined {
349+ /**
350+ * @returns the NormalizedSchema for the given member name. The returned instance will return true for `isMemberSchema()`
351+ * and will have the member name given.
352+ * @param member - which member to retrieve and wrap.
353+ *
354+ * @throws Error if member does not exist or the schema is neither a document nor structure.
355+ * Note that errors are assumed to be structures and unions are considered structures for these purposes.
356+ */
357+ public getMemberSchema ( member : string ) : NormalizedSchema {
299358 if ( this . isStructSchema ( ) ) {
300359 const struct = this . getSchema ( ) as StructureSchema ;
301360 if ( ! ( member in struct . members ) ) {
302- // indicates the member is not recognized.
303- return undefined ;
361+ throw new Error (
362+ `@smithy/core/schema - the schema ${ this . getName ( true ) } does not have a member with name=${ member } .`
363+ ) ;
304364 }
305365 return NormalizedSchema . memberFrom ( struct . members [ member ] , member ) ;
306366 }
@@ -310,6 +370,9 @@ export class NormalizedSchema implements INormalizedSchema {
310370 throw new Error ( `@smithy/core/schema - the schema ${ this . getName ( true ) } does not have members.` ) ;
311371 }
312372
373+ /**
374+ * @returns a map of member names to member schemas (normalized).
375+ */
313376 public getMemberSchemas ( ) : Record < string , NormalizedSchema > {
314377 const { schema } = this ;
315378 const struct = schema as StructureSchema ;
@@ -326,6 +389,9 @@ export class NormalizedSchema implements INormalizedSchema {
326389 return { } ;
327390 }
328391
392+ /**
393+ * @returns a last-resort human-readable name for the schema if it has no other identifiers.
394+ */
329395 private getSchemaName ( ) : string {
330396 const schema = this . getSchema ( ) ;
331397 if ( typeof schema === "number" ) {
0 commit comments