chore: Fix the variant

This commit is contained in:
BluJ
2023-03-27 13:14:36 -06:00
parent 2709c6ba0b
commit 281b752967
6 changed files with 98 additions and 87 deletions

View File

@@ -5,8 +5,9 @@ test: $(TS_FILES)
make clean:
rm -rf dist
# utils/test/output.ts: utils/test/config.json scripts/oldSpecToBuilder.ts
# cat utils/test/config.json | deno run scripts/oldSpecToBuilder.ts "../../mod" |deno fmt - > utils/test/output.ts
lib/util/artifacts/output.ts: lib/util/artifacts/makeOutput.ts lib/scripts/oldSpecToBuilder.ts
npm run buildOutput
bundle: fmt $(TS_FILES) .FORCE node_modules
npx tsc-multi

View File

@@ -35,10 +35,10 @@ import {
export class List<A extends ValueSpecList> extends IBuilder<A> {
static string<
A extends Description &
Default<string[]> & {
range: string;
spec: StringSpec;
}
Default<string[]> & {
range: string;
spec: StringSpec;
}
>(a: A) {
return new List({
type: "list" as const,
@@ -48,10 +48,10 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
}
static number<
A extends Description &
Default<number[]> & {
range: string;
spec: NumberSpec;
}
Default<number[]> & {
range: string;
spec: NumberSpec;
}
>(a: A) {
return new List({
type: "list" as const,
@@ -61,14 +61,14 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
}
static obj<
A extends Description &
Default<Record<string, unknown>[]> & {
range: string;
spec: {
spec: Config<InputSpec>;
displayAs: null | string;
uniqueBy: null | UniqueBy;
};
}
Default<Record<string, unknown>[]> & {
range: string;
spec: {
spec: Config<InputSpec>;
displayAs: null | string;
uniqueBy: null | UniqueBy;
};
}
>(a: A) {
const { spec: previousSpec, ...rest } = a;
const { spec: previousSpecSpec, ...restSpec } = previousSpec;
@@ -91,20 +91,18 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
}
static union<
A extends Description &
Default<string[]> & {
range: string;
spec: {
id: B;
name: string;
description: null | string;
warning: null | string;
variants: Variants<{ [key: string]: { name: string, spec: InputSpec } }>;
displayAs: null | string;
uniqueBy: UniqueBy;
default: string;
};
},
B extends string
Default<string[]> & {
range: string;
spec: {
name: string;
description: null | string;
warning: null | string;
variants: Variants<{ [key: string]: { name: string, spec: InputSpec } }>;
displayAs: null | string;
uniqueBy: UniqueBy;
default: string;
};
}
>(a: A) {
const { spec: previousSpec, ...rest } = a;
const { variants: previousVariants, ...restSpec } = previousSpec;

View File

@@ -39,16 +39,24 @@ import { Config } from ".";
```
*/
export class Variants<
A extends { [key: string]: InputSpec }
A extends {
[key: string]: {
name: string,
spec: InputSpec
}
}
> extends IBuilder<A> {
static of<
A extends {
[key: string]: Config<InputSpec>;
[key: string]: { name: string, spec: Config<InputSpec> };
}
>(a: A) {
const variants: { [K in keyof A]: BuilderExtract<A[K]> } = {} as any;
const variants: { [K in keyof A]: { name: string, spec: BuilderExtract<A[K]['spec']> } } = {} as any;
for (const key in a) {
variants[key] = a[key].build() as any;
const value = a[key]
variants[key] = {
name: value.name, spec: value.spec.build() as any
}
}
return new Variants(variants);
}

View File

@@ -171,7 +171,7 @@ export type Effects = {
method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "PATCH";
headers?: Record<string, string>;
body?: string;
},
}
): Promise<{
method: string;
ok: boolean;
@@ -281,7 +281,7 @@ export type Effects = {
*/
getSslCertificate: (
packageId: string,
algorithm?: "ecdsa" | "ed25519",
algorithm?: "ecdsa" | "ed25519"
) => [string, string, string];
/**
* @returns PEM encoded ssl key (ecdsa)

View File

@@ -107,7 +107,8 @@ export type ListValueSpecOf<T extends ListValueSpecType> = T extends "string"
/** represents a spec for a list */
export type ValueSpecList = ValueSpecListOf<ListValueSpecType>;
export interface ValueSpecListOf<T extends ListValueSpecType> extends WithStandalone {
export interface ValueSpecListOf<T extends ListValueSpecType>
extends WithStandalone {
type: "list";
subtype: T;
spec: ListValueSpecOf<T>;
@@ -124,7 +125,10 @@ export interface ValueSpecListOf<T extends ListValueSpecType> extends WithStanda
}
// sometimes the type checker needs just a little bit of help
export function isValueSpecListOf<S extends ListValueSpecType>(t: ValueSpecList, s: S): t is ValueSpecListOf<S> {
export function isValueSpecListOf<S extends ListValueSpecType>(
t: ValueSpecList,
s: S
): t is ValueSpecListOf<S> {
return t.subtype === s;
}

View File

@@ -12,55 +12,55 @@ type TypePointer = "pointer";
type TypeUnion = "union";
// prettier-ignore
type GuardDefaultNullable<A, Type> =
A extends { readonly default: unknown} ? Type :
A extends { readonly nullable: true} ? Type :
A extends {readonly nullable: false} ? Type | null | undefined :
Type
type GuardDefaultNullable<A, Type> =
A extends { readonly default: unknown } ? Type :
A extends { readonly nullable: true } ? Type :
A extends { readonly nullable: false } ? Type | null | undefined :
Type
// prettier-ignore
type GuardNumber<A> =
A extends {readonly type:TypeNumber} ? GuardDefaultNullable<A, number> :
unknown
type GuardNumber<A> =
A extends { readonly type: TypeNumber } ? GuardDefaultNullable<A, number> :
unknown
// prettier-ignore
type GuardString<A> =
A extends {readonly type:TypeString} ? GuardDefaultNullable<A, string> :
unknown
type GuardString<A> =
A extends { readonly type: TypeString } ? GuardDefaultNullable<A, string> :
unknown
// prettier-ignore
type GuardBoolean<A> =
A extends {readonly type:TypeBoolean} ? GuardDefaultNullable<A, boolean> :
unknown
type GuardBoolean<A> =
A extends { readonly type: TypeBoolean } ? GuardDefaultNullable<A, boolean> :
unknown
// prettier-ignore
type GuardObject<A> =
A extends {readonly type: TypeObject, readonly spec: infer B} ? (
B extends Record<string, unknown> ? {readonly [K in keyof B & string]: _<GuardAll<B[K]>>} :
{_error: "Invalid Spec"}
) :
unknown
type GuardObject<A> =
A extends { readonly type: TypeObject, readonly spec: infer B } ? (
B extends Record<string, unknown> ? { readonly [K in keyof B & string]: _<GuardAll<B[K]>> } :
{ _error: "Invalid Spec" }
) :
unknown
// prettier-ignore
export type GuardList<A> =
A extends {readonly type:TypeList, readonly subtype: infer B, spec?: {spec?: infer C }} ? ReadonlyArray<GuardAll<Omit<A, "type" | "spec"> & ({type: B, spec: C})>> :
A extends {readonly type:TypeList, readonly subtype: infer B, spec?: {}} ? ReadonlyArray<GuardAll<Omit<A, "type" > & ({type: B})>> :
unknown
export type GuardList<A> =
A extends { readonly type: TypeList, readonly subtype: infer B, spec?: { spec?: infer C } } ? ReadonlyArray<GuardAll<Omit<A, "type" | "spec"> & ({ type: B, spec: C })>> :
A extends { readonly type: TypeList, readonly subtype: infer B, spec?: {} } ? ReadonlyArray<GuardAll<Omit<A, "type"> & ({ type: B })>> :
unknown
// prettier-ignore
type GuardPointer<A> =
A extends {readonly type:TypePointer} ? (string | null) :
unknown
type GuardPointer<A> =
A extends { readonly type: TypePointer } ? (string | null) :
unknown
// prettier-ignore
type GuardSelect<A> =
A extends {readonly type:TypeSelect, readonly values: ArrayLike<infer B>} ? GuardDefaultNullable<A, B> :
unknown
type GuardSelect<A> =
A extends { readonly type: TypeSelect, readonly values: ArrayLike<infer B> } ? GuardDefaultNullable<A, B> :
unknown
// prettier-ignore
type GuardMultiselect<A> =
A extends {readonly type:TypeMultiselect, readonly values: ArrayLike<infer B>} ? GuardDefaultNullable<A, B> :
unknown
type GuardMultiselect<A> =
A extends { readonly type: TypeMultiselect, readonly values: ArrayLike<infer B> } ? GuardDefaultNullable<A, B> :
unknown
// prettier-ignore
type GuardUnion<A> =
A extends {readonly type:TypeUnion, readonly tag: {id: infer Id & string}, variants: infer Variants & Record<string, unknown>} ? {[K in keyof Variants]: {[keyType in Id & string]: K}&TypeFromProps<Variants[K]>}[keyof Variants] :
unknown
type GuardUnion<A> =
A extends { readonly type: TypeUnion, readonly tag: { id: infer Id & string }, variants: infer Variants & Record<string, unknown> } ? { [K in keyof Variants]: { [keyType in Id & string]: K } & TypeFromProps<Variants[K]> }[keyof Variants] :
unknown
type _<T> = T;
export type GuardAll<A> = GuardNumber<A> &
@@ -73,9 +73,9 @@ export type GuardAll<A> = GuardNumber<A> &
GuardSelect<A> &
GuardMultiselect<A>;
// prettier-ignore
export type TypeFromProps<A> =
A extends Record<string, unknown> ? {readonly [K in keyof A & string]: _<GuardAll<A[K]>>} :
unknown;
export type TypeFromProps<A> =
A extends Record<string, unknown> ? { readonly [K in keyof A & string]: _<GuardAll<A[K]>> } :
unknown;
const isType = matches.shape({ type: matches.string });
const recordString = matches.dictionary([matches.string, matches.unknown]);
@@ -155,23 +155,23 @@ export function matchNumberWithRange(range: string) {
leftValue === "*"
? (_) => true
: left === "["
? (x) => x >= Number(leftValue)
: (x) => x > Number(leftValue),
? (x) => x >= Number(leftValue)
: (x) => x > Number(leftValue),
leftValue === "*"
? "any"
: left === "["
? `greaterThanOrEqualTo${leftValue}`
: `greaterThan${leftValue}`
? `greaterThanOrEqualTo${leftValue}`
: `greaterThan${leftValue}`
)
.validate(
// prettier-ignore
rightValue === "*" ? (_) => true :
right === "]"? (x) => x <= Number(rightValue) :
(x) => x < Number(rightValue),
right === "]" ? (x) => x <= Number(rightValue) :
(x) => x < Number(rightValue),
// prettier-ignore
rightValue === "*" ? "any" :
right === "]" ? `lessThanOrEqualTo${rightValue}` :
`lessThan${rightValue}`
right === "]" ? `lessThanOrEqualTo${rightValue}` :
`lessThan${rightValue}`
);
}
function withIntegral(parser: matches.Parser<unknown, number>, value: unknown) {