Skip to content

feat: schema #1506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions .changeset/nice-deers-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@smithy/smithy-client": minor
"@smithy/types": minor
"@smithy/core": minor
---

implement schema framework
10 changes: 10 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
"require": "./dist-cjs/submodules/protocols/index.js",
"types": "./dist-types/submodules/protocols/index.d.ts"
},
"./schema": {
"module": "./dist-es/submodules/schema/index.js",
"node": "./dist-cjs/submodules/schema/index.js",
"import": "./dist-es/submodules/schema/index.js",
"require": "./dist-cjs/submodules/schema/index.js",
"types": "./dist-types/submodules/schema/index.d.ts"
},
"./serde": {
"module": "./dist-es/submodules/serde/index.js",
"node": "./dist-cjs/submodules/serde/index.js",
Expand All @@ -64,6 +71,7 @@
"@smithy/middleware-serde": "workspace:^",
"@smithy/protocol-http": "workspace:^",
"@smithy/types": "workspace:^",
"@smithy/util-base64": "workspace:^",
"@smithy/util-body-length-browser": "workspace:^",
"@smithy/util-middleware": "workspace:^",
"@smithy/util-stream": "workspace:^",
Expand All @@ -85,6 +93,8 @@
"./cbor.js",
"./protocols.d.ts",
"./protocols.js",
"./schema.d.ts",
"./schema.js",
"./serde.d.ts",
"./serde.js",
"dist-*/**"
Expand Down
7 changes: 7 additions & 0 deletions packages/core/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Do not edit:
* This is a compatibility redirect for contexts that do not understand package.json exports field.
*/
declare module "@smithy/core/schema" {
export * from "@smithy/core/dist-types/submodules/schema/index.d";
}
6 changes: 6 additions & 0 deletions packages/core/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

/**
* Do not edit:
* This is a compatibility redirect for contexts that do not understand package.json exports field.
*/
module.exports = require("./dist-cjs/submodules/schema/index.js");
164 changes: 164 additions & 0 deletions packages/core/src/submodules/cbor/CborCodec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { NormalizedSchema } from "@smithy/core/schema";
import { copyDocumentWithTransform, parseEpochTimestamp } from "@smithy/core/serde";
import { Codec, Schema, SchemaRef, SerdeContext, ShapeDeserializer, ShapeSerializer } from "@smithy/types";

import { cbor } from "./cbor";
import { dateToTag } from "./parseCborBody";

export class CborCodec implements Codec<Uint8Array, Uint8Array> {
private serdeContext?: SerdeContext;

public createSerializer(): CborShapeSerializer {
const serializer = new CborShapeSerializer();
serializer.setSerdeContext(this.serdeContext!);
return serializer;
}

public createDeserializer(): CborShapeDeserializer {
const deserializer = new CborShapeDeserializer();
deserializer.setSerdeContext(this.serdeContext!);
return deserializer;
}

public setSerdeContext(serdeContext: SerdeContext): void {
this.serdeContext = serdeContext;
}
}

export class CborShapeSerializer implements ShapeSerializer<Uint8Array> {
private serdeContext?: SerdeContext;
private value: unknown;

public setSerdeContext(serdeContext: SerdeContext) {
this.serdeContext = serdeContext;
}

public write(schema: Schema, value: unknown): void {
this.value = copyDocumentWithTransform(value, schema, (_: any, schemaRef: SchemaRef) => {
if (_ instanceof Date) {
return dateToTag(_);
}
if (_ instanceof Uint8Array) {
return _;
}

const ns = NormalizedSchema.of(schemaRef);
const sparse = !!ns.getMergedTraits().sparse;

if (Array.isArray(_)) {
if (!sparse) {
return _.filter((item) => item != null);
}
} else if (_ && typeof _ === "object") {
if (!sparse || ns.isStructSchema()) {
for (const [k, v] of Object.entries(_)) {
if (v == null) {
delete _[k];
}
}
return _;
}
}

return _;
});
}

public flush(): Uint8Array {
const buffer = cbor.serialize(this.value);
this.value = undefined;
return buffer as Uint8Array;
}
}

export class CborShapeDeserializer implements ShapeDeserializer {
private serdeContext?: SerdeContext;

public setSerdeContext(serdeContext: SerdeContext) {
this.serdeContext = serdeContext;
}

public read(schema: Schema, bytes: Uint8Array): any {
const data: any = cbor.deserialize(bytes);
return this.readValue(schema, data);
}

private readValue(_schema: Schema, value: any): any {
const ns = NormalizedSchema.of(_schema);
const schema = ns.getSchema();

if (typeof schema === "number") {
if (ns.isTimestampSchema()) {
// format is ignored.
return parseEpochTimestamp(value);
}
if (ns.isBlobSchema()) {
return value;
}
}

switch (typeof value) {
case "undefined":
case "boolean":
case "number":
case "string":
case "bigint":
case "symbol":
return value;
case "function":
case "object":
if (value === null) {
return null;
}
if ("byteLength" in (value as Uint8Array)) {
return value;
}
if (value instanceof Date) {
return value;
}
if (ns.isDocumentSchema()) {
return value;
}

if (ns.isListSchema()) {
const newArray = [];
const memberSchema = ns.getValueSchema();
const sparse = ns.isListSchema() && !!ns.getMergedTraits().sparse;

for (const item of value) {
newArray.push(this.readValue(memberSchema, item));
if (!sparse && newArray[newArray.length - 1] == null) {
newArray.pop();
}
}
return newArray;
}

const newObject = {} as any;

if (ns.isMapSchema()) {
const sparse = ns.getMergedTraits().sparse;
const targetSchema = ns.getValueSchema();

for (const key of Object.keys(value)) {
newObject[key] = this.readValue(targetSchema, value[key]);

if (newObject[key] == null && !sparse) {
delete newObject[key];
}
}
} else if (ns.isStructSchema()) {
for (const key of Object.keys(value)) {
const targetSchema = ns.getMemberSchema(key);
if (targetSchema === undefined) {
continue;
}
newObject[key] = this.readValue(targetSchema, value[key]);
}
}
return newObject;
default:
return value;
}
}
}
Loading
Loading