Skip to content
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

JSON Type compact encoding #595

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"arg": "^5.0.2",
"hyperdyperid": "^1.2.0",
"sonic-forest": "^1.0.0",
"thingies": "^1.20.0"
"thingies": "^1.21.0"
},
"devDependencies": {
"@types/benchmark": "^2.1.2",
Expand Down
112 changes: 112 additions & 0 deletions src/json-type/schema-compact/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import type {Expr} from '../../json-expression';
import type * as verbose from '../schema/schema';

export type AnyCompactSchema = [kind: 'any', options?: Omit<verbose.AnySchema, 'kind'>];

export type BooleanCompactSchema = [kind: 'bool', options?: Omit<verbose.BooleanSchema, 'kind'>];

export type NumberCompactSchema = [kind: 'num', options?: Omit<verbose.NumberSchema, 'kind'>];

export type StringCompactSchema = [kind: 'str', options?: Omit<verbose.StringSchema, 'kind'>];

export type BinaryCompactSchema<T extends TCompactType> = [
kind: 'bin',
type: T,
options?: Omit<verbose.BinarySchema, 'kind' | 'type'>,
];

export type ArrayCompactSchema<T extends TCompactType> = [
kind: 'arr',
type: T,
options?: Omit<verbose.ArraySchema, 'kind' | 'types'>,
];

export type ConstCompactSchema<V = any> = [
kind: 'con',
value: V,
options?: Omit<verbose.ConstSchema, 'kind' | 'value'>,
];

export type TupleCompactSchema<T extends TCompactType[]> = [
kind: 'tup',
types: T,
options?: Omit<verbose.TupleSchema, 'kind' | 'types'>,
];

export type ObjectCompactSchema<Fields extends ObjectFieldCompactSchema<string, any>[]> = [
kind: 'obj',
fields: Fields,
options?: Omit<verbose.ObjectSchema, 'kind' | 'fields'>,
];

export type ObjectFieldCompactSchema<
K extends string = string,
V extends TCompactType = TCompactType,
Opts extends Omit<verbose.ObjectFieldSchema, 'kind' | 'key' | 'type'> = Omit<verbose.ObjectFieldSchema, 'kind' | 'key' | 'type'>
> = [
kind: 'field',
key: K,
type: V,
options?: Opts,
];

export type ObjectOptionalFieldCompactSchema<K extends string = string, V extends TCompactType = TCompactType> =
ObjectFieldCompactSchema<K, V, Omit<verbose.ObjectFieldSchema, 'kind' | 'key' | 'type' | 'optional'> & {optional: true}>;
// export type ObjectOptionalFieldCompactSchema<K extends string = string, V extends TCompactType = TCompactType> = [
// kind: 'field',
// key: K,
// type: V,
// options?: Omit<verbose.ObjectFieldSchema, 'kind' | 'key' | 'type' | 'optional'> & {optional: true},
// ];

export type MapCompactSchema<T extends TCompactType> = [
kind: 'map',
type: T,
options?: Omit<verbose.MapSchema, 'kind' | 'type'>,
];

export type RefCompactSchema<T extends TCompactType> = [
kind: 'ref',
ref: T,
options?: Omit<verbose.RefSchema, 'kind' | 'ref'>,
];

export type OrCompactSchema<T extends TCompactType[]> = [
kind: 'or',
types: T,
discriminator: Expr,
options?: Omit<verbose.OrSchema, 'kind' | 'types' | 'discriminator'>,
];

export type FunctionCompactSchema<Req extends TCompactType, Res extends TCompactType> = [
kind: 'fn',
req: Req,
res: Res,
options?: Omit<verbose.FunctionSchema, 'kind' | 'req' | 'res'>,
];

export type FunctionStreamingCompactSchema<Req extends TCompactType, Res extends TCompactType> = [
kind: 'fn$',
req: Req,
res: Res,
options?: Omit<verbose.FunctionStreamingSchema, 'kind' | 'req' | 'res'>,
];

export type TCompactType =
| AnyCompactSchema
| BooleanCompactSchema
| NumberCompactSchema
| StringCompactSchema
| BinaryCompactSchema<any>
| ArrayCompactSchema<any>
| ConstCompactSchema
| TupleCompactSchema<any>
| ObjectCompactSchema<any>
| ObjectFieldCompactSchema<string, any>
| ObjectOptionalFieldCompactSchema<string, any>
| MapCompactSchema<any>
| RefCompactSchema<any>
| OrCompactSchema<any>
| FunctionCompactSchema<any, any>
| FunctionStreamingCompactSchema<any, any>
;
20 changes: 20 additions & 0 deletions src/json-type/schema-compact/toCompact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {hasKeys} from 'thingies/es2020/hasKeys';
import type * as compact from './schema';
import type * as verbose from '../schema/schema';

export const toCompact = (obj: verbose.Schema): compact.TCompactType => {
switch (obj.kind) {
case 'any':
case 'bool':
case 'num':
case 'str': {
const {kind, ...rest} = obj;
return hasKeys(rest) ? [kind, rest] as compact.TCompactType : [kind] as compact.TCompactType;
}
case 'bin': {
const {kind, type, ...rest} = obj;
return (hasKeys(rest) ? [kind, type, rest] : [kind, type]) as compact.BinaryCompactSchema<any>;
}
}
throw new Error('UNKNOWN_KIND');
};
33 changes: 33 additions & 0 deletions src/json-type/schema-compact/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type * as verbose from '../schema/schema';
import type * as compact from './schema';

// prettier-ignore
export type VerboseToCompact<T extends verbose.TType> = T extends verbose.AnySchema
? compact.AnyCompactSchema
: T extends verbose.BooleanSchema
? compact.BooleanCompactSchema
: T extends verbose.NumberSchema
? compact.NumberCompactSchema
: T extends verbose.StringSchema
? compact.StringCompactSchema
: T extends verbose.BinarySchema<infer V>
? compact.BinaryCompactSchema<VerboseToCompact<V>>
: T extends verbose.ArraySchema<infer V>
? compact.ArrayCompactSchema<VerboseToCompact<V>>
: T extends verbose.ConstSchema<infer V>
? compact.ConstCompactSchema<V>
: T extends verbose.TupleSchema<infer V>
? compact.TupleCompactSchema<{[K in keyof V]: VerboseToCompact<V[K]>}>
: T extends verbose.ObjectSchema<infer F>
? compact.ObjectCompactSchema<{[K in keyof F]: VerboseToCompactFields<F[K]>}>
: T extends verbose.ObjectFieldSchema<infer K, infer V>
? compact.ObjectFieldCompactSchema<K, VerboseToCompact<V>>
: T extends verbose.ObjectOptionalFieldSchema<infer K, infer V>
? compact.ObjectOptionalFieldCompactSchema<K, VerboseToCompact<V>>
: never;

type VerboseToCompactFields<T extends verbose.ObjectFieldSchema<any, any>> = T extends verbose.ObjectOptionalFieldSchema<infer K, infer V>
? compact.ObjectOptionalFieldCompactSchema<K, VerboseToCompact<V>>
: T extends verbose.ObjectFieldSchema<infer K, infer V>
? compact.ObjectFieldCompactSchema<K, VerboseToCompact<V>>
: never;
8 changes: 8 additions & 0 deletions src/json-type/type/classes/AnyType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {BinaryJsonEncoder} from '@jsonjoy.com/json-pack/lib/types';
import {CapacityEstimatorCodegenContext} from '../../codegen/capacity/CapacityEstimatorCodegenContext';
import {AbstractType} from './AbstractType';
import type * as jsonSchema from '../../../json-schema';
import type * as compact from '../../schema-compact/schema';
import type * as ts from '../../typescript/types';
import type {TypeExportContext} from '../../system/TypeExportContext';
import type * as jtd from '../../jtd/types';
Expand Down Expand Up @@ -117,4 +118,11 @@ export class AnyType extends AbstractType<schema.AnySchema> {
const form: jtd.JtdEmptyForm = {nullable: true};
return form;
}

public toCompact(): compact.AnyCompactSchema {
const {kind, ...opts} = this.getSchema();
const res: compact.AnyCompactSchema = [kind];
if (Object.keys(opts).length > 0) res.push(opts);
return res;
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,11 @@ thingies@^1.20.0:
resolved "https://registry.yarnpkg.com/thingies/-/thingies-1.20.0.tgz#27bf93397c39c3ff36601197e8cf78f43b7b2319"
integrity sha512-WvXY4CjHp/Uim2Ri0daqu6jkNTHJTk1H8NvuMQiOL0mgtdkqoSH5fkENy2M6XnvsLOp5iwyPcbmokoBjVb4lnQ==

thingies@^1.21.0:
version "1.21.0"
resolved "https://registry.yarnpkg.com/thingies/-/thingies-1.21.0.tgz#e80fbe58fd6fdaaab8fad9b67bd0a5c943c445c1"
integrity sha512-hsqsJsFMsV+aD4s3CWKk85ep/3I9XzYV/IXaSouJMYIoDlgyi11cBhsqYe9/geRfB0YIikBQg6raRaM+nIMP9g==

[email protected]:
version "1.0.5"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.5.tgz#8683e0b902bb9c20c4f726e3c0b69f36518c07cc"
Expand Down
Loading