From 60a6a2768209eb2281e3e65d331579b54ba97a44 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Tue, 28 Mar 2023 01:02:14 -0600 Subject: [PATCH] remove list of unions --- lib/config/builder/builder.ts | 2 - lib/config/builder/list.ts | 42 ------------------- lib/config/builder/value.ts | 5 ++- lib/config/config-types.ts | 28 +++---------- scripts/oldSpecToBuilder.ts | 77 ++++++++++++++++++----------------- 5 files changed, 48 insertions(+), 106 deletions(-) diff --git a/lib/config/builder/builder.ts b/lib/config/builder/builder.ts index 8843660..56e1034 100644 --- a/lib/config/builder/builder.ts +++ b/lib/config/builder/builder.ts @@ -1,5 +1,3 @@ -import { typeFromProps } from "../../util"; - export class IBuilder { protected constructor(readonly a: A) { } diff --git a/lib/config/builder/list.ts b/lib/config/builder/list.ts index f8a7788..619be93 100644 --- a/lib/config/builder/list.ts +++ b/lib/config/builder/list.ts @@ -1,6 +1,5 @@ import { BuilderExtract, IBuilder } from "./builder"; import { Config } from "./config"; -import { Variants } from "./variants"; import { InputSpec, UniqueBy, @@ -107,47 +106,6 @@ export class List extends IBuilder { ...value, }); } - static union< - A extends { - name: string; - description: string | null; - warning: string | null; - default: Record[]; - 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() { return guardAll(this.a); diff --git a/lib/config/builder/value.ts b/lib/config/builder/value.ts index 763c425..cca028b 100644 --- a/lib/config/builder/value.ts +++ b/lib/config/builder/value.ts @@ -7,7 +7,6 @@ import { ValueSpec, ValueSpecList, ValueSpecNumber, - ValueSpecOf, ValueSpecString, } from "../config-types"; import { guardAll } from "../../util"; @@ -146,8 +145,10 @@ export class Value extends IBuilder { name: string; description: string | null; warning: string | null; + selectKey: string; variants: Variants<{ [key: string]: { name: string; spec: InputSpec } }>; - default: string; + nullable: boolean; + default: string | null; } >(a: A) { const { variants: previousVariants, ...rest } = a; diff --git a/lib/config/config-types.ts b/lib/config/config-types.ts index b9a18ec..b25fe18 100644 --- a/lib/config/config-types.ts +++ b/lib/config/config-types.ts @@ -66,13 +66,13 @@ export interface ValueSpecBoolean extends WithStandalone { export interface ValueSpecUnion extends WithStandalone { type: "union"; - variants: { [key: string]: { name: string; spec: InputSpec } }; - default: null | string; + selectKey: string; + nullable: boolean + variants: Record; } export interface ValueSpecFile extends WithStandalone { type: "file"; - placeholder: null | string; nullable: boolean; extensions: string[]; } @@ -89,11 +89,10 @@ export interface WithStandalone { } export interface SelectBase { - values: { [value: string]: string }; + values: Record; } -/** no lists of booleans, lists*/ -export type ListValueSpecType = "string" | "number" | "object" | "union"; +export type ListValueSpecType = "string" | "number" | "object"; /** represents a spec for the values of a list */ export type ListValueSpecOf = T extends "string" @@ -102,8 +101,6 @@ export type ListValueSpecOf = T extends "string" ? ListValueSpecNumber : T extends "object" ? ListValueSpecObject - : T extends "union" - ? ListValueSpecUnion : never; /** represents a spec for a list */ @@ -163,19 +160,4 @@ export type UniqueBy = | { any: 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 }; diff --git a/scripts/oldSpecToBuilder.ts b/scripts/oldSpecToBuilder.ts index 31a9423..30e05f6 100644 --- a/scripts/oldSpecToBuilder.ts +++ b/scripts/oldSpecToBuilder.ts @@ -228,17 +228,17 @@ export default async function makeFileContent( convertInputSpec(value.spec.spec) ); return `List.obj({ - name: ${JSON.stringify(value.name || null)}, - range: ${JSON.stringify(value.range || null)}, - spec: { - spec: ${specName}, - 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)}, - })`; + name: ${JSON.stringify(value.name || null)}, + range: ${JSON.stringify(value.range || null)}, + spec: { + spec: ${specName}, + 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)}, + })`; } case "union": { const variants = newConst( @@ -248,34 +248,37 @@ export default async function makeFileContent( value.spec["variant-names"] || {} ) ); - return `List.union( - { - name:${JSON.stringify(value.name || null)}, - range:${JSON.stringify(value.range || null)}, + + return `List.obj({ + name:${JSON.stringify(value.name || null)}, + range:${JSON.stringify(value.range || null)}, + spec: { spec: { - select: { - "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 + ${value?.spec?.tag?.id || 'type'}: { + type: "union", + name: ${JSON.stringify( + value?.spec?.tag?.name || null )}, - uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)}, - default: ${JSON.stringify(value?.spec?.default || null)}, - }, - default: ${JSON.stringify(value.default || null)}, - description: ${JSON.stringify(value.description || null)}, - warning: ${JSON.stringify(value.warning || null)}, - } - )`; + description: ${JSON.stringify( + value?.spec?.tag?.description || null + )}, + warning: ${JSON.stringify( + value?.spec?.tag?.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}"`);