Skip to content

Commit

Permalink
Merge pull request #64 from UCNot/form-deserializer
Browse files Browse the repository at this point in the history
URI parameters deserializer
  • Loading branch information
surol authored Jul 26, 2023
2 parents 21d58e5 + 61919fd commit 8876371
Show file tree
Hide file tree
Showing 98 changed files with 3,242 additions and 1,407 deletions.
5 changes: 3 additions & 2 deletions src/compiler/deserialization/bigint.ucrx.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ export class BigIntUcrxClass extends UcrxClass<UcrxSignature.Args, UcBigInt, UcB
}

constructor(
{ baseUcrx }: UcrxLib,
lib: UcrxLib,
schema: UcBigInt.Schema,
{ string = 'parse', number = 'parse' }: UcBigInt.Variant = {},
) {
super({
lib,
schema,
baseClass: baseUcrx,
baseClass: lib.baseUcrx,
});

UcrxCore.big.overrideIn(this, {
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/deserialization/boolean.ucrx.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ export class BooleanUcrxClass extends UcrxClass<UcrxSignature.Args, UcBoolean, U
};
}

constructor({ baseUcrx }: UcrxLib, schema: UcBoolean.Schema) {
constructor(lib: UcrxLib, schema: UcBoolean.Schema) {
super({
lib,
schema,
baseClass: baseUcrx,
baseClass: lib.baseUcrx,
});

UcrxCore.bol.overrideIn(this, {
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/deserialization/integer.ucrx.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ export class IntegerUcrxClass extends UcrxClass<UcrxSignature.Args, UcInteger, U
}

constructor(
{ baseUcrx }: UcrxLib,
lib: UcrxLib,
schema: UcInteger.Schema,
{ string = 'parse' }: UcInteger.Variant = {},
) {
super({
lib,
schema,
baseClass: baseUcrx,
baseClass: lib.baseUcrx,
});

UcrxCore.num.overrideIn(this, {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/deserialization/list.ucrx.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class ListUcrxClass<
const isMatrix = itemClass.isMemberOverridden(UcrxCore.and);

super({
lib,
schema,
typeName:
(single === 'reject' || single === 'accept' ? 'List' : 'MultiValue')
Expand Down
7 changes: 3 additions & 4 deletions src/compiler/deserialization/map.ucrx.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export class MapUcrxClass<
};
}

readonly #lib: UcrxLib;
readonly #entries: EsSymbol | undefined;
readonly #extra: EsSymbol | undefined;
readonly #collector: MapUcrxClass$Collector;
Expand All @@ -64,6 +63,7 @@ export class MapUcrxClass<
variant?: UcMap.Variant,
) {
super({
lib,
typeName: MapUcrxClass$typeName(lib, schema),
schema,
baseClass: lib.baseUcrx,
Expand All @@ -72,8 +72,6 @@ export class MapUcrxClass<
},
});

this.#lib = lib;

this.#entries = this.#declareEntries();
this.#extra = this.#declareExtra();
this.#collector = this.#declareCollector(variant);
Expand Down Expand Up @@ -411,7 +409,7 @@ export class MapUcrxClass<
}

entryUcrxFor(key: string | null, schema: UcSchema): UcrxClass {
const entryClass = MapUcrxEntry.ucrxClass(this.#lib, this.schema, key, schema);
const entryClass = MapUcrxEntry.ucrxClass(this.lib, this.schema, key, schema);

return this.#collector.rxs ? entryClass.associate(MultiEntryUcrxClass$associate) : entryClass;
}
Expand Down Expand Up @@ -517,6 +515,7 @@ class MultiEntryUcrxClass<

constructor(baseClass: UcrxClass<TArgs, T, TSchema>) {
super({
lib: baseClass.lib,
schema: baseClass.schema,
baseClass,
typeName: `${baseClass.typeName}$Entry`,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/deserialization/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './ucd-handler-feature.js';
export * from './ucd-lib.js';
export * from './ucd-models.js';
export * from './ucd-support-defaults.js';
export * from './ucd-support-inset.js';
export * from './ucd-support-non-finite.js';
export * from './ucd-support-primitives.js';
export * from './unknown.ucrx.class.js';
9 changes: 3 additions & 6 deletions src/compiler/deserialization/number.ucrx.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ export class NumberUcrxClass extends UcrxClass<UcrxSignature.Args, UcNumber, UcN
};
}

constructor(
{ baseUcrx }: UcrxLib,
schema: UcNumber.Schema,
{ string = 'parse' }: UcNumber.Variant = {},
) {
constructor(lib: UcrxLib, schema: UcNumber.Schema, { string = 'parse' }: UcNumber.Variant = {}) {
super({
lib,
schema,
baseClass: baseUcrx,
baseClass: lib.baseUcrx,
});

UcrxCore.num.overrideIn(this, {
Expand Down
9 changes: 3 additions & 6 deletions src/compiler/deserialization/string.ucrx.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ export class StringUcrxClass extends UcrxClass<UcrxSignature.Args, UcString, UcS
};
}

constructor(
{ baseUcrx }: UcrxLib,
schema: UcString.Schema,
{ raw = 'escape' }: UcString.Variant = {},
) {
constructor(lib: UcrxLib, schema: UcString.Schema, { raw = 'escape' }: UcString.Variant = {}) {
super({
lib,
schema,
baseClass: baseUcrx,
baseClass: lib.baseUcrx,
});
if (raw !== 'parse') {
UcrxCore.raw.overrideIn(this, {
Expand Down
30 changes: 15 additions & 15 deletions src/compiler/deserialization/ucd-compiler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from '@jest/globals';
import { EsBundleFormat } from 'esgen';
import { SPEC_MODULE } from '../../impl/module-names.js';
import { UcModel, UcSchema } from '../../schema/uc-schema.js';
import { UcSchema } from '../../schema/uc-schema.js';
import { ucdSupportTimestampFormat } from '../../spec/timestamp.format.js';
import { UcdCompiler } from './ucd-compiler.js';
import { ucdSupportDefaults } from './ucd-support-defaults.js';
Expand All @@ -11,7 +11,7 @@ describe('UcdCompiler', () => {
it('enables feature in object form', async () => {
const compiler = new UcdCompiler({
models: {
readTimestamp: ['sync', Number],
readTimestamp: { model: Number, mode: 'sync' },
},
features: [
{
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('UcdCompiler', () => {
};
const compiler = new UcdCompiler({
models: {
readTimestamp: ['sync', schema],
readTimestamp: { model: schema, mode: 'sync' },
},
});

Expand All @@ -88,7 +88,7 @@ describe('UcdCompiler', () => {
};
const compiler = new UcdCompiler({
models: {
readTimestamp: ['sync', schema],
readTimestamp: { model: schema, mode: 'sync' },
},
});

Expand All @@ -109,7 +109,7 @@ describe('UcdCompiler', () => {
};
const compiler = new UcdCompiler({
models: {
readTimestamp: ['sync', schema],
readTimestamp: { model: schema, mode: 'sync' },
},
});

Expand All @@ -130,8 +130,8 @@ describe('UcdCompiler', () => {
};

await expect(
new UcdCompiler<{ readTimestamp: UcModel<number> }>({
models: { readTimestamp: schema },
new UcdCompiler({
models: { readTimestamp: { model: schema } },
}).bootstrap(),
).rejects.toThrow(
new ReferenceError(
Expand All @@ -151,8 +151,8 @@ describe('UcdCompiler', () => {
};

await expect(
new UcdCompiler<{ readTimestamp: UcModel<number> }>({
models: { readTimestamp: schema },
new UcdCompiler({
models: { readTimestamp: { model: schema } },
}).bootstrap(),
).rejects.toThrow(
new ReferenceError(
Expand All @@ -166,7 +166,7 @@ describe('UcdCompiler', () => {
describe('ES2015', () => {
it('compiles async module', async () => {
const compiler = new UcdCompiler({
models: { readValue: ['async', Number] },
models: { readValue: { model: Number, mode: 'async' } },
});
const text = await compiler.generate();

Expand All @@ -191,7 +191,7 @@ export async function readValue(
});
it('compiles sync module', async () => {
const compiler = new UcdCompiler({
models: { readValue: ['sync', Number] },
models: { readValue: { model: Number, mode: 'sync' } },
});
const text = await compiler.generate();

Expand All @@ -215,7 +215,7 @@ export function readValue(
});
it('compiles universal module', async () => {
const compiler = new UcdCompiler({
models: { readValue: Number },
models: { readValue: { model: Number } },
});
const text = await compiler.generate();

Expand Down Expand Up @@ -243,7 +243,7 @@ export function readValue(
describe('IIFE', () => {
it('creates async factory', async () => {
const compiler = new UcdCompiler({
models: { readValue: ['async', Number] },
models: { readValue: { model: Number, mode: 'async' } },
});
const text = await compiler.generate({ format: EsBundleFormat.IIFE });

Expand All @@ -268,7 +268,7 @@ export function readValue(
});
it('creates sync factory', async () => {
const compiler = new UcdCompiler({
models: { readValue: ['sync', Number] },
models: { readValue: { model: Number, mode: 'sync' } },
});
const text = await compiler.generate({ format: EsBundleFormat.IIFE });

Expand All @@ -293,7 +293,7 @@ export function readValue(
});
it('creates universal factory', async () => {
const compiler = new UcdCompiler({
models: { readValue: Number },
models: { readValue: { model: Number } },
});
const text = await compiler.generate({ format: EsBundleFormat.IIFE });

Expand Down
23 changes: 10 additions & 13 deletions src/compiler/deserialization/ucd-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import {
esline,
} from 'esgen';
import { capitalize } from 'httongue';
import { UcPresentationName } from '../../schema/uc-presentations.js';
import { UcSchema } from '../../schema/uc-schema.js';
import { UcdHandlerRegistry } from '../impl/ucd-handler-registry.js';
import { UccConfig } from '../processor/ucc-config.js';
import { UccFeature } from '../processor/ucc-feature.js';
import { UcrxLib } from '../rx/ucrx-lib.js';
import { UcrxProcessor } from '../rx/ucrx-processor.js';
import { UcrxClass, UcrxSignature } from '../rx/ucrx.class.js';
import { UcdFunction } from './ucd-function.js';
import { UcdHandlerFeature } from './ucd-handler-feature.js';
import { UcdLib } from './ucd-lib.js';
import { UcdExports, UcdModels, isUcdModelConfig } from './ucd-models.js';
import { UcdExports, UcdModels } from './ucd-models.js';
import { ucdSupportDefaults } from './ucd-support-defaults.js';

/**
Expand Down Expand Up @@ -53,11 +53,12 @@ export class UcdCompiler<
* @param options - Compiler options.
*/
constructor(options: UcdCompiler.Options<TModels>) {
const { models, validate = true, features } = options;
const { models, presentations, validate = true, features } = options;

super({
names: validate ? ['validator', 'deserializer'] : ['deserializer'],
models: Object.values(models).map(entry => (isUcdModelConfig(entry) ? entry[1] : entry)),
processors: validate ? ['validator', 'deserializer'] : ['deserializer'],
presentations,
models: Object.values(models).map(({ model }) => model),
features,
});

Expand Down Expand Up @@ -92,7 +93,7 @@ export class UcdCompiler<

// Stop registering default handlers.
// Start registering custom ones.
defaultConfig.configure();
defaultConfig.configure(undefined, {});

this.#entities.makeDefault();
this.#formats.makeDefault();
Expand Down Expand Up @@ -343,19 +344,15 @@ export class UcdCompiler<
export namespace UcdCompiler {
export type Any = UcdCompiler<UcdModels>;

export interface Options<out TModels extends UcdModels> extends Omit<UcrxLib.Options, 'methods'> {
export interface Options<out TModels extends UcdModels = UcdModels>
extends Omit<UcrxLib.Options, 'methods'> {
readonly models: TModels;
readonly presentations?: UcPresentationName | UcPresentationName[] | undefined;
readonly validate?: boolean | undefined;
readonly features?:
| UccFeature<UcdCompiler.Any>
| readonly UccFeature<UcdCompiler.Any>[]
| undefined;
readonly inset?: EsSnippet | undefined;
readonly exportDefaults?: boolean | undefined;

createDeserializer?<T, TSchema extends UcSchema<T>>(
this: void,
options: UcdFunction.Options<T, TSchema>,
): UcdFunction<T, TSchema>;
}
}
Loading

0 comments on commit 8876371

Please sign in to comment.