Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-bulldogs-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/core": minor
---

add instanceof overrides for schema classes
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ export abstract class HttpBindingProtocol extends HttpProtocol {
...input,
};

for (const memberName of Object.keys(_input)) {
const memberNs = ns.getMemberSchema(memberName);
if (memberNs === undefined) {
continue;
}
for (const [memberName, memberNs] of ns.structIterator()) {
const memberTraits = memberNs.getMergedTraits();
const inputMember = (_input as any)[memberName] as any;

Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/submodules/schema/schemas/ErrorSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { StructureSchema } from "./StructureSchema";
* @alpha
*/
export class ErrorSchema extends StructureSchema {
public static symbol = Symbol.for("@smithy/core/schema::ErrorSchema");
protected symbol = ErrorSchema.symbol;

public constructor(
public name: string,
public traits: SchemaTraits,
Expand All @@ -24,6 +27,15 @@ export class ErrorSchema extends StructureSchema {
) {
super(name, traits, memberNames, memberList);
}

public static [Symbol.hasInstance](lhs: unknown): lhs is ErrorSchema {
const isPrototype = ErrorSchema.prototype.isPrototypeOf(lhs as any);
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
const err = lhs as ErrorSchema;
return err.symbol === ErrorSchema.symbol;
}
return isPrototype;
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/submodules/schema/schemas/ListSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@ import { Schema } from "./Schema";
* @alpha
*/
export class ListSchema extends Schema implements IListSchema {
public static symbol = Symbol.for("@smithy/core/schema::ListSchema");
protected symbol = ListSchema.symbol;

public constructor(
public name: string,
public traits: SchemaTraits,
public valueSchema: SchemaRef
) {
super(name, traits);
}

public static [Symbol.hasInstance](lhs: unknown): lhs is ListSchema {
const isPrototype = ListSchema.prototype.isPrototypeOf(lhs as any);
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
const list = lhs as ListSchema;
return list.symbol === ListSchema.symbol;
}
return isPrototype;
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/submodules/schema/schemas/MapSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { Schema } from "./Schema";
* @alpha
*/
export class MapSchema extends Schema implements IMapSchema {
public static symbol = Symbol.for("@smithy/core/schema::MapSchema");
protected symbol = MapSchema.symbol;

public constructor(
public name: string,
public traits: SchemaTraits,
Expand All @@ -19,6 +22,15 @@ export class MapSchema extends Schema implements IMapSchema {
) {
super(name, traits);
}

public static [Symbol.hasInstance](lhs: unknown): lhs is MapSchema {
const isPrototype = MapSchema.prototype.isPrototypeOf(lhs as any);
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
const map = lhs as MapSchema;
return map.symbol === MapSchema.symbol;
}
return isPrototype;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { StructureSchema } from "./StructureSchema";
* @alpha
*/
export class NormalizedSchema implements INormalizedSchema {
public static symbol = Symbol.for("@smithy/core/schema::NormalizedSchema");
protected symbol = NormalizedSchema.symbol;

private readonly name: string;
private readonly traits: SchemaTraits;

Expand Down Expand Up @@ -84,11 +87,22 @@ export class NormalizedSchema implements INormalizedSchema {

if (this._isMemberSchema && !memberName) {
throw new Error(
`@smithy/core/schema - NormalizedSchema member schema ${this.getName(true)} must initialize with memberName argument.`
`@smithy/core/schema - NormalizedSchema member schema ${this.getName(
true
)} must initialize with memberName argument.`
);
}
}

public static [Symbol.hasInstance](lhs: unknown): lhs is NormalizedSchema {
const isPrototype = NormalizedSchema.prototype.isPrototypeOf(lhs as any);
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
const ns = lhs as NormalizedSchema;
return ns.symbol === NormalizedSchema.symbol;
}
return isPrototype;
}

/**
* Static constructor that attempts to avoid wrapping a NormalizedSchema within another.
*/
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/submodules/schema/schemas/SimpleSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@ import { Schema } from "./Schema";
* @alpha
*/
export class SimpleSchema extends Schema implements TraitsSchema {
public static symbol = Symbol.for("@smithy/core/schema::SimpleSchema");
protected symbol = SimpleSchema.symbol;

public constructor(
public name: string,
public schemaRef: SchemaRef,
public traits: SchemaTraits
) {
super(name, traits);
}

public static [Symbol.hasInstance](lhs: unknown): lhs is SimpleSchema {
const isPrototype = SimpleSchema.prototype.isPrototypeOf(lhs as any);
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
const sim = lhs as SimpleSchema;
return sim.symbol === SimpleSchema.symbol;
}
return isPrototype;
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/submodules/schema/schemas/StructureSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { Schema } from "./Schema";
* @alpha
*/
export class StructureSchema extends Schema implements IStructureSchema {
public static symbol = Symbol.for("@smithy/core/schema::StructureSchema");
protected symbol = StructureSchema.symbol;

public members: Record<string, [SchemaRef, SchemaTraits]> = {};

public constructor(
Expand All @@ -24,6 +27,15 @@ export class StructureSchema extends Schema implements IStructureSchema {
: [memberList[i], 0];
}
}

public static [Symbol.hasInstance](lhs: unknown): lhs is StructureSchema {
const isPrototype = StructureSchema.prototype.isPrototypeOf(lhs as any);
if (!isPrototype && typeof lhs === "object" && lhs !== null) {
const struct = lhs as StructureSchema;
return struct.symbol === StructureSchema.symbol;
}
return isPrototype;
}
}

/**
Expand Down
26 changes: 26 additions & 0 deletions packages/core/src/submodules/schema/schemas/schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe("schemas", () => {
expect(error("ack", "Error", 0, [], [], Error)).toEqual(schema);
expect(TypeRegistry.for("ack").getSchema(schema.name)).toEqual(schema);
});

it("has an instanceOf operator", () => {
const object = { ...schema };
expect(ErrorSchema.prototype.isPrototypeOf(object)).toBe(false);
expect(object).toBeInstanceOf(ErrorSchema);
});
});
describe(ListSchema.name, () => {
const schema = new ListSchema("ack#List", 0, 0);
Expand All @@ -62,6 +68,11 @@ describe("schemas", () => {
expect(list("ack", "List", 0, 0)).toEqual(schema);
expect(TypeRegistry.for("ack").getSchema(schema.name)).toEqual(schema);
});
it("has an instanceOf operator", () => {
const object = { ...schema };
expect(ListSchema.prototype.isPrototypeOf(object)).toBe(false);
expect(object).toBeInstanceOf(ListSchema);
});
});
describe(MapSchema.name, () => {
const schema = new MapSchema("ack#Map", 0, 0, 1);
Expand All @@ -76,6 +87,11 @@ describe("schemas", () => {
expect(map("ack", "Map", 0, 0, 1)).toEqual(schema);
expect(TypeRegistry.for("ack").getSchema(schema.name)).toEqual(schema);
});
it("has an instanceOf operator", () => {
const object = { ...schema };
expect(MapSchema.prototype.isPrototypeOf(object)).toBe(false);
expect(object).toBeInstanceOf(MapSchema);
});
});
describe(OperationSchema.name, () => {
const schema = new OperationSchema("ack#Operation", 0, "unit", "unit");
Expand Down Expand Up @@ -119,6 +135,11 @@ describe("schemas", () => {
expect(sim("ack", "Simple", 0, 0)).toEqual(schema);
expect(TypeRegistry.for("ack").getSchema(schema.name)).toEqual(schema);
});
it("has an instanceOf operator", () => {
const object = { ...schema };
expect(SimpleSchema.prototype.isPrototypeOf(object)).toBe(false);
expect(object).toBeInstanceOf(SimpleSchema);
});
});
describe(StructureSchema.name, () => {
const schema = new StructureSchema("ack#Structure", 0, ["a", "b", "c"], [0, 1, 2]);
Expand All @@ -136,5 +157,10 @@ describe("schemas", () => {
expect(struct("ack", "Structure", 0, ["a", "b", "c"], [0, 1, 2])).toEqual(schema);
expect(TypeRegistry.for("ack").getSchema(schema.name)).toEqual(schema);
});
it("has an instanceOf operator", () => {
const object = { ...schema };
expect(StructureSchema.prototype.isPrototypeOf(object)).toBe(false);
expect(object).toBeInstanceOf(StructureSchema);
});
});
});