Skip to content

Commit 9b8805c

Browse files
committed
chore: schema code size optimization
1 parent e586a10 commit 9b8805c

File tree

9 files changed

+112
-171
lines changed

9 files changed

+112
-171
lines changed
Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SchemaRef, SchemaTraits } from "@smithy/types";
22

33
import { TypeRegistry } from "../TypeRegistry";
4+
import { Schema } from "./Schema";
45
import { StructureSchema } from "./StructureSchema";
56

67
/**
@@ -13,29 +14,8 @@ import { StructureSchema } from "./StructureSchema";
1314
*/
1415
export class ErrorSchema extends StructureSchema {
1516
public static symbol = Symbol.for("@smithy/core/schema::ErrorSchema");
17+
public ctor!: any;
1618
protected symbol = ErrorSchema.symbol;
17-
18-
public constructor(
19-
public name: string,
20-
public traits: SchemaTraits,
21-
public memberNames: string[],
22-
public memberList: SchemaRef[],
23-
/**
24-
* Constructor for a modeled service exception class that extends Error.
25-
*/
26-
public ctor: any
27-
) {
28-
super(name, traits, memberNames, memberList);
29-
}
30-
31-
public static [Symbol.hasInstance](lhs: unknown): lhs is ErrorSchema {
32-
const isPrototype = ErrorSchema.prototype.isPrototypeOf(lhs as any);
33-
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
34-
const err = lhs as ErrorSchema;
35-
return err.symbol === ErrorSchema.symbol;
36-
}
37-
return isPrototype;
38-
}
3919
}
4020

4121
/**
@@ -50,15 +30,19 @@ export class ErrorSchema extends StructureSchema {
5030
* @param memberList - list of schemaRef corresponding to each
5131
* @param ctor - class reference for the existing Error extending class.
5232
*/
53-
export function error(
33+
export const error = (
5434
namespace: string,
5535
name: string,
56-
traits: SchemaTraits = {},
36+
traits: SchemaTraits,
5737
memberNames: string[],
5838
memberList: SchemaRef[],
5939
ctor: any
60-
): ErrorSchema {
61-
const schema = new ErrorSchema(namespace + "#" + name, traits, memberNames, memberList, ctor);
62-
TypeRegistry.for(namespace).register(name, schema);
63-
return schema;
64-
}
40+
): ErrorSchema =>
41+
Schema.assign(new ErrorSchema(), {
42+
name,
43+
namespace,
44+
traits,
45+
memberNames,
46+
memberList,
47+
ctor,
48+
});
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ListSchema as IListSchema, SchemaRef, SchemaTraits } from "@smithy/types";
22

3-
import { TypeRegistry } from "../TypeRegistry";
43
import { Schema } from "./Schema";
54

65
/**
@@ -10,38 +9,22 @@ import { Schema } from "./Schema";
109
* @alpha
1110
*/
1211
export class ListSchema extends Schema implements IListSchema {
13-
public static symbol = Symbol.for("@smithy/core/schema::ListSchema");
12+
public static symbol = Symbol.for("@smithy/l");
13+
public name!: string;
14+
public traits!: SchemaTraits;
15+
public valueSchema!: SchemaRef;
1416
protected symbol = ListSchema.symbol;
15-
16-
public constructor(
17-
public name: string,
18-
public traits: SchemaTraits,
19-
public valueSchema: SchemaRef
20-
) {
21-
super(name, traits);
22-
}
23-
24-
public static [Symbol.hasInstance](lhs: unknown): lhs is ListSchema {
25-
const isPrototype = ListSchema.prototype.isPrototypeOf(lhs as any);
26-
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
27-
const list = lhs as ListSchema;
28-
return list.symbol === ListSchema.symbol;
29-
}
30-
return isPrototype;
31-
}
3217
}
3318

3419
/**
3520
* Factory for ListSchema.
3621
*
3722
* @internal
3823
*/
39-
export function list(namespace: string, name: string, traits: SchemaTraits = {}, valueSchema: SchemaRef): ListSchema {
40-
const schema = new ListSchema(
41-
namespace + "#" + name,
24+
export const list = (namespace: string, name: string, traits: SchemaTraits, valueSchema: SchemaRef): ListSchema =>
25+
Schema.assign(new ListSchema(), {
26+
name,
27+
namespace,
4228
traits,
43-
typeof valueSchema === "function" ? valueSchema() : valueSchema
44-
);
45-
TypeRegistry.for(namespace).register(name, schema);
46-
return schema;
47-
}
29+
valueSchema,
30+
});
Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
11
import type { MapSchema as IMapSchema, SchemaRef, SchemaTraits } from "@smithy/types";
22

3-
import { TypeRegistry } from "../TypeRegistry";
43
import { Schema } from "./Schema";
54

65
/**
76
* A schema with a key schema and value schema.
87
* @alpha
98
*/
109
export class MapSchema extends Schema implements IMapSchema {
11-
public static symbol = Symbol.for("@smithy/core/schema::MapSchema");
10+
public static symbol = Symbol.for("@smithy/m");
11+
public name!: string;
12+
public traits!: SchemaTraits;
13+
/**
14+
* This is expected to be StringSchema, but may have traits.
15+
*/
16+
public keySchema!: SchemaRef;
17+
public valueSchema!: SchemaRef;
1218
protected symbol = MapSchema.symbol;
13-
14-
public constructor(
15-
public name: string,
16-
public traits: SchemaTraits,
17-
/**
18-
* This is expected to be StringSchema, but may have traits.
19-
*/
20-
public keySchema: SchemaRef,
21-
public valueSchema: SchemaRef
22-
) {
23-
super(name, traits);
24-
}
25-
26-
public static [Symbol.hasInstance](lhs: unknown): lhs is MapSchema {
27-
const isPrototype = MapSchema.prototype.isPrototypeOf(lhs as any);
28-
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
29-
const map = lhs as MapSchema;
30-
return map.symbol === MapSchema.symbol;
31-
}
32-
return isPrototype;
33-
}
3419
}
3520

3621
/**
3722
* Factory for MapSchema.
3823
* @internal
3924
*/
40-
export function map(
25+
export const map = (
4126
namespace: string,
4227
name: string,
43-
traits: SchemaTraits = {},
28+
traits: SchemaTraits,
4429
keySchema: SchemaRef,
4530
valueSchema: SchemaRef
46-
): MapSchema {
47-
const schema = new MapSchema(
48-
namespace + "#" + name,
31+
): MapSchema =>
32+
Schema.assign(new MapSchema(), {
33+
name,
34+
namespace,
4935
traits,
5036
keySchema,
51-
typeof valueSchema === "function" ? valueSchema() : valueSchema
52-
);
53-
TypeRegistry.for(namespace).register(name, schema);
54-
return schema;
55-
}
37+
valueSchema,
38+
});

packages/core/src/submodules/schema/schemas/NormalizedSchema.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ export class NormalizedSchema implements INormalizedSchema {
3636
* @param ref - a polymorphic SchemaRef to be dereferenced/normalized.
3737
* @param memberName - optional memberName if this NormalizedSchema should be considered a member schema.
3838
*/
39-
public constructor(
40-
private readonly ref: SchemaRef,
41-
private memberName?: string
42-
) {
39+
public constructor(private readonly ref: SchemaRef, private memberName?: string) {
4340
const traitStack = [] as SchemaTraits[];
4441
let _ref = ref;
4542
let schema = ref;
@@ -380,7 +377,7 @@ export class NormalizedSchema implements INormalizedSchema {
380377
public hasMemberSchema(member: string): boolean {
381378
if (this.isStructSchema()) {
382379
const struct = this.getSchema() as StructureSchema;
383-
return member in struct.members;
380+
return struct.memberNames.includes(member);
384381
}
385382
return false;
386383
}
@@ -396,12 +393,14 @@ export class NormalizedSchema implements INormalizedSchema {
396393
public getMemberSchema(member: string): NormalizedSchema {
397394
if (this.isStructSchema()) {
398395
const struct = this.getSchema() as StructureSchema;
399-
if (!(member in struct.members)) {
396+
if (!struct.memberNames.includes(member)) {
400397
throw new Error(
401398
`@smithy/core/schema - the schema ${this.getName(true)} does not have a member with name=${member}.`
402399
);
403400
}
404-
return NormalizedSchema.memberFrom(struct.members[member], member);
401+
const i = struct.memberNames.indexOf(member);
402+
const memberSchema = struct.memberList[i];
403+
return NormalizedSchema.memberFrom(Array.isArray(memberSchema) ? memberSchema : [memberSchema, 0], member);
405404
}
406405
if (this.isDocumentSchema()) {
407406
return NormalizedSchema.memberFrom([SCHEMA.DOCUMENT, 0], member);
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { OperationSchema as IOperationSchema, SchemaRef, SchemaTraits } from "@smithy/types";
22

3-
import { TypeRegistry } from "../TypeRegistry";
43
import { Schema } from "./Schema";
54

65
/**
@@ -10,28 +9,29 @@ import { Schema } from "./Schema";
109
* @alpha
1110
*/
1211
export class OperationSchema extends Schema implements IOperationSchema {
13-
public constructor(
14-
public name: string,
15-
public traits: SchemaTraits,
16-
public input: SchemaRef,
17-
public output: SchemaRef
18-
) {
19-
super(name, traits);
20-
}
12+
public static symbol = Symbol.for("@smithy/o");
13+
public name!: string;
14+
public traits!: SchemaTraits;
15+
public input!: SchemaRef;
16+
public output!: SchemaRef;
17+
protected symbol = OperationSchema.symbol;
2118
}
2219

2320
/**
2421
* Factory for OperationSchema.
2522
* @internal
2623
*/
27-
export function op(
24+
export const op = (
2825
namespace: string,
2926
name: string,
30-
traits: SchemaTraits = {},
27+
traits: SchemaTraits,
3128
input: SchemaRef,
3229
output: SchemaRef
33-
): OperationSchema {
34-
const schema = new OperationSchema(namespace + "#" + name, traits, input, output);
35-
TypeRegistry.for(namespace).register(name, schema);
36-
return schema;
37-
}
30+
): OperationSchema =>
31+
Schema.assign(new OperationSchema(), {
32+
name,
33+
namespace,
34+
traits,
35+
input,
36+
output,
37+
});
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
import type { SchemaTraits, TraitsSchema } from "@smithy/types";
22

3+
import { TypeRegistry } from "../TypeRegistry";
4+
35
/**
46
* Abstract base for class-based Schema except NormalizedSchema.
57
*
68
* @alpha
79
*/
810
export abstract class Schema implements TraitsSchema {
9-
protected constructor(
10-
public name: string,
11-
public traits: SchemaTraits
12-
) {}
11+
public name!: string;
12+
public namespace!: string;
13+
public traits!: SchemaTraits;
14+
protected abstract symbol: symbol;
15+
16+
public static assign<T extends Schema>(instance: T, values: Omit<T, "getName" | "symbol">): T {
17+
const schema = Object.assign(instance, values);
18+
TypeRegistry.for(schema.namespace).register(schema.name, schema);
19+
return schema;
20+
}
21+
22+
public static [Symbol.hasInstance](lhs: unknown) {
23+
const isPrototype = this.prototype.isPrototypeOf(lhs as any);
24+
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
25+
const list = lhs as any;
26+
return list.symbol === (this as any).symbol;
27+
}
28+
return isPrototype;
29+
}
30+
31+
public getName(): string {
32+
return this.namespace + "#" + this.name;
33+
}
1334
}

packages/core/src/submodules/schema/schemas/SimpleSchema.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,22 @@ import { Schema } from "./Schema";
1010
* @alpha
1111
*/
1212
export class SimpleSchema extends Schema implements TraitsSchema {
13-
public static symbol = Symbol.for("@smithy/core/schema::SimpleSchema");
13+
public static symbol = Symbol.for("@smithy/s");
14+
public name!: string;
15+
public schemaRef!: SchemaRef;
16+
public traits!: SchemaTraits;
1417
protected symbol = SimpleSchema.symbol;
15-
16-
public constructor(
17-
public name: string,
18-
public schemaRef: SchemaRef,
19-
public traits: SchemaTraits
20-
) {
21-
super(name, traits);
22-
}
23-
24-
public static [Symbol.hasInstance](lhs: unknown): lhs is SimpleSchema {
25-
const isPrototype = SimpleSchema.prototype.isPrototypeOf(lhs as any);
26-
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
27-
const sim = lhs as SimpleSchema;
28-
return sim.symbol === SimpleSchema.symbol;
29-
}
30-
return isPrototype;
31-
}
3218
}
3319

3420
/**
3521
* Factory for simple schema class objects.
3622
*
3723
* @internal
3824
*/
39-
export function sim(namespace: string, name: string, schemaRef: SchemaRef, traits: SchemaTraits) {
40-
const schema = new SimpleSchema(namespace + "#" + name, schemaRef, traits);
41-
TypeRegistry.for(namespace).register(name, schema);
42-
return schema;
43-
}
25+
export const sim = (namespace: string, name: string, schemaRef: SchemaRef, traits: SchemaTraits) =>
26+
Schema.assign(new SimpleSchema(), {
27+
name,
28+
namespace,
29+
traits,
30+
schemaRef,
31+
});

0 commit comments

Comments
 (0)