remove list of unions

This commit is contained in:
Matt Hill
2023-03-28 01:02:14 -06:00
parent 53ef640ed7
commit 60a6a27682
5 changed files with 48 additions and 106 deletions

View File

@@ -1,5 +1,3 @@
import { typeFromProps } from "../../util";
export class IBuilder<A> { export class IBuilder<A> {
protected constructor(readonly a: A) { } protected constructor(readonly a: A) { }

View File

@@ -1,6 +1,5 @@
import { BuilderExtract, IBuilder } from "./builder"; import { BuilderExtract, IBuilder } from "./builder";
import { Config } from "./config"; import { Config } from "./config";
import { Variants } from "./variants";
import { import {
InputSpec, InputSpec,
UniqueBy, UniqueBy,
@@ -107,47 +106,6 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
...value, ...value,
}); });
} }
static union<
A extends {
name: string;
description: string | null;
warning: string | null;
default: Record<string, unknown>[];
range: string;
spec: {
select: {
name: string;
description: string | null;
warning: string | null;
}
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;
const variants = previousVariants.build() as BuilderExtract<
A["spec"]["variants"]
>;
const spec = {
...restSpec,
variants,
};
const value = {
spec,
...rest,
};
return new List({
type: "list" as const,
subtype: "union" as const,
...value,
});
}
public validator() { public validator() {
return guardAll(this.a); return guardAll(this.a);

View File

@@ -7,7 +7,6 @@ import {
ValueSpec, ValueSpec,
ValueSpecList, ValueSpecList,
ValueSpecNumber, ValueSpecNumber,
ValueSpecOf,
ValueSpecString, ValueSpecString,
} from "../config-types"; } from "../config-types";
import { guardAll } from "../../util"; import { guardAll } from "../../util";
@@ -146,8 +145,10 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
name: string; name: string;
description: string | null; description: string | null;
warning: string | null; warning: string | null;
selectKey: string;
variants: Variants<{ [key: string]: { name: string; spec: InputSpec } }>; variants: Variants<{ [key: string]: { name: string; spec: InputSpec } }>;
default: string; nullable: boolean;
default: string | null;
} }
>(a: A) { >(a: A) {
const { variants: previousVariants, ...rest } = a; const { variants: previousVariants, ...rest } = a;

View File

@@ -66,13 +66,13 @@ export interface ValueSpecBoolean extends WithStandalone {
export interface ValueSpecUnion extends WithStandalone { export interface ValueSpecUnion extends WithStandalone {
type: "union"; type: "union";
variants: { [key: string]: { name: string; spec: InputSpec } }; selectKey: string;
default: null | string; nullable: boolean
variants: Record<string, { name: string; spec: InputSpec }>;
} }
export interface ValueSpecFile extends WithStandalone { export interface ValueSpecFile extends WithStandalone {
type: "file"; type: "file";
placeholder: null | string;
nullable: boolean; nullable: boolean;
extensions: string[]; extensions: string[];
} }
@@ -89,11 +89,10 @@ export interface WithStandalone {
} }
export interface SelectBase { export interface SelectBase {
values: { [value: string]: string }; values: Record<string, string>;
} }
/** no lists of booleans, lists*/ export type ListValueSpecType = "string" | "number" | "object";
export type ListValueSpecType = "string" | "number" | "object" | "union";
/** represents a spec for the values of a list */ /** represents a spec for the values of a list */
export type ListValueSpecOf<T extends ListValueSpecType> = T extends "string" export type ListValueSpecOf<T extends ListValueSpecType> = T extends "string"
@@ -102,8 +101,6 @@ export type ListValueSpecOf<T extends ListValueSpecType> = T extends "string"
? ListValueSpecNumber ? ListValueSpecNumber
: T extends "object" : T extends "object"
? ListValueSpecObject ? ListValueSpecObject
: T extends "union"
? ListValueSpecUnion
: never; : never;
/** represents a spec for a list */ /** represents a spec for a list */
@@ -163,19 +160,4 @@ export type UniqueBy =
| { any: readonly UniqueBy[] | UniqueBy[] } | { any: readonly UniqueBy[] | UniqueBy[] }
| { all: readonly UniqueBy[] | UniqueBy[] }; | { all: readonly UniqueBy[] | UniqueBy[] };
export interface ListValueSpecUnion {
select: {
name: string
description: null | string
warning: null | string
}
variants: { [key: string]: { name: string; spec: InputSpec } };
/** a handlebars template for labeling each union list item */
displayAs: null | string;
/** indicates whether duplicates can be permitted in the list */
uniqueBy: UniqueBy;
/** the default variant when creating a new union instance in the list*/
default: null | string;
}
export type DefaultString = string | { charset: string; len: number }; export type DefaultString = string | { charset: string; len: number };

View File

@@ -228,17 +228,17 @@ export default async function makeFileContent(
convertInputSpec(value.spec.spec) convertInputSpec(value.spec.spec)
); );
return `List.obj({ return `List.obj({
name: ${JSON.stringify(value.name || null)}, name: ${JSON.stringify(value.name || null)},
range: ${JSON.stringify(value.range || null)}, range: ${JSON.stringify(value.range || null)},
spec: { spec: {
spec: ${specName}, spec: ${specName},
displayAs: ${JSON.stringify(value?.spec?.["display-as"] || null)}, displayAs: ${JSON.stringify(value?.spec?.["display-as"] || null)},
uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)}, uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)},
}, },
default: ${JSON.stringify(value.default || null)}, default: ${JSON.stringify(value.default || null)},
description: ${JSON.stringify(value.description || null)}, description: ${JSON.stringify(value.description || null)},
warning: ${JSON.stringify(value.warning || null)}, warning: ${JSON.stringify(value.warning || null)},
})`; })`;
} }
case "union": { case "union": {
const variants = newConst( const variants = newConst(
@@ -248,34 +248,37 @@ export default async function makeFileContent(
value.spec["variant-names"] || {} value.spec["variant-names"] || {}
) )
); );
return `List.union(
{ return `List.obj({
name:${JSON.stringify(value.name || null)}, name:${JSON.stringify(value.name || null)},
range:${JSON.stringify(value.range || null)}, range:${JSON.stringify(value.range || null)},
spec: {
spec: { spec: {
select: { ${value?.spec?.tag?.id || 'type'}: {
"name": ${JSON.stringify( type: "union",
value?.spec?.tag?.["name"] || null name: ${JSON.stringify(
)}, value?.spec?.tag?.name || null
"description": ${JSON.stringify(
value?.spec?.tag?.["description"] || null
)},
"warning": ${JSON.stringify(
value?.spec?.tag?.["warning"] || null
)},
},
variants: ${variants},
displayAs: ${JSON.stringify(
value?.spec?.["display-as"] || null
)}, )},
uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)}, description: ${JSON.stringify(
default: ${JSON.stringify(value?.spec?.default || null)}, value?.spec?.tag?.description || null
}, )},
default: ${JSON.stringify(value.default || null)}, warning: ${JSON.stringify(
description: ${JSON.stringify(value.description || null)}, value?.spec?.tag?.warning || null
warning: ${JSON.stringify(value.warning || null)}, )},
} selectKey: ${JSON.stringify(
)`; value?.spec?.tag?.id || null
)},
variants: ${variants},
nullable: false,
}
}
displayAs: ${JSON.stringify(value?.spec?.["display-as"] || null)},
uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)},
},
default: ${JSON.stringify(value.default || null)},
description: ${JSON.stringify(value.description || null)},
warning: ${JSON.stringify(value.warning || null)},
})`;
} }
} }
throw new Error(`Unknown subtype "${value.subtype}"`); throw new Error(`Unknown subtype "${value.subtype}"`);