@@ -15,20 +15,26 @@ import { SimpleSchema } from "./SimpleSchema";
15
15
import { StructureSchema } from "./StructureSchema" ;
16
16
17
17
/**
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
20
22
*/
21
23
export class NormalizedSchema implements INormalizedSchema {
22
- public readonly name : string ;
23
- public readonly traits : SchemaTraits ;
24
+ private readonly name : string ;
25
+ private readonly traits : SchemaTraits ;
24
26
25
27
private _isMemberSchema : boolean ;
26
- private schema : Exclude < ISchema , MemberSchema > ;
28
+ private schema : Exclude < ISchema , MemberSchema | INormalizedSchema > ;
27
29
private memberTraits : SchemaTraits ;
28
30
private normalizedTraits ?: SchemaTraitsObject ;
29
31
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
+ */
30
36
public constructor (
31
- public readonly ref : SchemaRef ,
37
+ private readonly ref : SchemaRef ,
32
38
private memberName ?: string
33
39
) {
34
40
const traitStack = [ ] as SchemaTraits [ ] ;
@@ -65,7 +71,7 @@ export class NormalizedSchema implements INormalizedSchema {
65
71
return ;
66
72
}
67
73
68
- this . schema = deref ( schema ) as Exclude < ISchema , MemberSchema > ;
74
+ this . schema = deref ( schema ) as Exclude < ISchema , MemberSchema | INormalizedSchema > ;
69
75
70
76
if ( this . schema && typeof this . schema === "object" ) {
71
77
this . traits = this . schema ?. traits ?? { } ;
@@ -83,6 +89,9 @@ export class NormalizedSchema implements INormalizedSchema {
83
89
}
84
90
}
85
91
92
+ /**
93
+ * Static constructor that attempts to avoid wrapping a NormalizedSchema within another.
94
+ */
86
95
public static of ( ref : SchemaRef , memberName ?: string ) : NormalizedSchema {
87
96
if ( ref instanceof NormalizedSchema ) {
88
97
return ref ;
@@ -124,7 +133,13 @@ export class NormalizedSchema implements INormalizedSchema {
124
133
return traits ;
125
134
}
126
135
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 {
128
143
if ( memberSchema instanceof NormalizedSchema ) {
129
144
memberSchema . memberName = memberName ;
130
145
memberSchema . _isMemberSchema = true ;
@@ -133,16 +148,23 @@ export class NormalizedSchema implements INormalizedSchema {
133
148
return new NormalizedSchema ( memberSchema , memberName ) ;
134
149
}
135
150
136
- public getSchema ( ) : Exclude < ISchema , MemberSchema > {
151
+ /**
152
+ * @returns the underlying non-normalized schema.
153
+ */
154
+ public getSchema ( ) : Exclude < ISchema , MemberSchema | INormalizedSchema > {
137
155
if ( this . schema instanceof NormalizedSchema ) {
138
156
return ( this . schema = this . schema . getSchema ( ) ) ;
139
157
}
140
158
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 > ;
142
160
}
143
- return deref ( this . schema ) as Exclude < ISchema , MemberSchema > ;
161
+ return deref ( this . schema ) as Exclude < ISchema , MemberSchema | INormalizedSchema > ;
144
162
}
145
163
164
+ /**
165
+ * @param withNamespace - qualifies the name.
166
+ * @returns e.g. `MyShape` or `com.namespace#MyShape`.
167
+ */
146
168
public getName ( withNamespace = false ) : string | undefined {
147
169
if ( ! withNamespace ) {
148
170
if ( this . name && this . name . includes ( "#" ) ) {
@@ -153,6 +175,10 @@ export class NormalizedSchema implements INormalizedSchema {
153
175
return this . name || undefined ;
154
176
}
155
177
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
+ */
156
182
public getMemberName ( ) : string {
157
183
if ( ! this . isMemberSchema ( ) ) {
158
184
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 {
168
194
return this . getSchema ( ) === ( "unit" as const ) ;
169
195
}
170
196
197
+ /**
198
+ * boolean methods on this class help control flow in shape serialization and deserialization.
199
+ */
171
200
public isListSchema ( ) : boolean {
172
201
const inner = this . getSchema ( ) ;
173
202
if ( typeof inner === "number" ) {
@@ -230,6 +259,10 @@ export class NormalizedSchema implements INormalizedSchema {
230
259
return this . getSchema ( ) === SCHEMA . STREAMING_BLOB ;
231
260
}
232
261
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
+ */
233
266
public getMergedTraits ( ) : SchemaTraitsObject {
234
267
if ( this . normalizedTraits ) {
235
268
return this . normalizedTraits ;
@@ -241,14 +274,26 @@ export class NormalizedSchema implements INormalizedSchema {
241
274
return this . normalizedTraits ;
242
275
}
243
276
277
+ /**
278
+ * @returns only the member traits. If the schema is not a member, this returns empty.
279
+ */
244
280
public getMemberTraits ( ) : SchemaTraitsObject {
245
281
return NormalizedSchema . translateTraits ( this . memberTraits ) ;
246
282
}
247
283
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
+ */
248
288
public getOwnTraits ( ) : SchemaTraitsObject {
249
289
return NormalizedSchema . translateTraits ( this . traits ) ;
250
290
}
251
291
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
+ */
252
297
public getKeySchema ( ) : NormalizedSchema {
253
298
if ( this . isDocumentSchema ( ) ) {
254
299
return NormalizedSchema . memberFrom ( [ SCHEMA . DOCUMENT , 0 ] , "key" ) ;
@@ -263,6 +308,12 @@ export class NormalizedSchema implements INormalizedSchema {
263
308
return NormalizedSchema . memberFrom ( [ ( schema as MapSchema ) . keySchema , 0 ] , "key" ) ;
264
309
}
265
310
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
+ */
266
317
public getValueSchema ( ) : NormalizedSchema {
267
318
const schema = this . getSchema ( ) ;
268
319
@@ -295,12 +346,21 @@ export class NormalizedSchema implements INormalizedSchema {
295
346
throw new Error ( `@smithy/core/schema - the schema ${ this . getName ( true ) } does not have a value member.` ) ;
296
347
}
297
348
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 {
299
358
if ( this . isStructSchema ( ) ) {
300
359
const struct = this . getSchema ( ) as StructureSchema ;
301
360
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
+ ) ;
304
364
}
305
365
return NormalizedSchema . memberFrom ( struct . members [ member ] , member ) ;
306
366
}
@@ -310,6 +370,9 @@ export class NormalizedSchema implements INormalizedSchema {
310
370
throw new Error ( `@smithy/core/schema - the schema ${ this . getName ( true ) } does not have members.` ) ;
311
371
}
312
372
373
+ /**
374
+ * @returns a map of member names to member schemas (normalized).
375
+ */
313
376
public getMemberSchemas ( ) : Record < string , NormalizedSchema > {
314
377
const { schema } = this ;
315
378
const struct = schema as StructureSchema ;
@@ -326,6 +389,9 @@ export class NormalizedSchema implements INormalizedSchema {
326
389
return { } ;
327
390
}
328
391
392
+ /**
393
+ * @returns a last-resort human-readable name for the schema if it has no other identifiers.
394
+ */
329
395
private getSchemaName ( ) : string {
330
396
const schema = this . getSchema ( ) ;
331
397
if ( typeof schema === "number" ) {
0 commit comments