From 513ef5563382ff37b5f334acd82dbf89afb4d2b6 Mon Sep 17 00:00:00 2001 From: BluJ Date: Tue, 2 May 2023 17:36:23 -0600 Subject: [PATCH] feat: Make config of the config top --- lib/actions/createAction.ts | 3 +-- lib/autoconfig/AutoConfig.ts | 4 +++- lib/config/builder/config.ts | 30 +++++++++++++----------------- lib/config/builder/list.ts | 2 +- lib/config/builder/value.ts | 2 +- lib/config/builder/variants.ts | 16 +++++++--------- lib/test/configBuilder.test.ts | 22 +++++++++++----------- lib/test/configTypes.test.ts | 2 +- scripts/oldSpecToBuilder.ts | 11 ++++------- 9 files changed, 42 insertions(+), 50 deletions(-) diff --git a/lib/actions/createAction.ts b/lib/actions/createAction.ts index 9b39565..9aea2f3 100644 --- a/lib/actions/createAction.ts +++ b/lib/actions/createAction.ts @@ -1,5 +1,4 @@ -import { Config } from "../config/builder" -import { ExtractConfigType } from "../config/builder/config" +import { Config, ExtractConfigType } from "../config/builder/config" import { ActionMetaData, ActionResult, Effects, ExportedAction } from "../types" import { Utils, utils } from "../util" diff --git a/lib/autoconfig/AutoConfig.ts b/lib/autoconfig/AutoConfig.ts index 477b360..2b91d38 100644 --- a/lib/autoconfig/AutoConfig.ts +++ b/lib/autoconfig/AutoConfig.ts @@ -1,5 +1,7 @@ import { AutoConfigure, DeepPartial, Effects, ExpectedExports } from "../types" -import { Utils, deepEqual, deepMerge, utils } from "../util" +import { Utils, utils } from "../util" +import { deepEqual } from "../util/deepEqual" +import { deepMerge } from "../util/deepMerge" export type AutoConfigFrom = { [key in keyof NestedConfigs & string]: (options: { diff --git a/lib/config/builder/config.ts b/lib/config/builder/config.ts index 39cedcb..2b55b0a 100644 --- a/lib/config/builder/config.ts +++ b/lib/config/builder/config.ts @@ -92,17 +92,19 @@ export class Config, WD> { return answer } - static of, WrapperData>(spec: { - [K in keyof Type]: Value - }) { - const validatorObj = {} as { - [K in keyof Type]: Parser + static of() { + return >(spec: { + [K in keyof Type]: Value + }) => { + const validatorObj = {} as { + [K in keyof Type]: Parser + } + for (const key in spec) { + validatorObj[key] = spec[key].validator + } + const validator = object(validatorObj) + return new Config(spec, validator) } - for (const key in spec) { - validatorObj[key] = spec[key].validator - } - const validator = object(validatorObj) - return new Config(spec, validator) } /** @@ -114,7 +116,7 @@ export class Config, WD> { required: false, }) - return topConfig()({ + return Config.of()({ myValue: a.withWrapperData(), }) ``` @@ -123,9 +125,3 @@ export class Config, WD> { return this as any as Config } } - -export function topConfig() { - return >(spec: { - [K in keyof Type]: Value - }) => Config.of(spec) -} diff --git a/lib/config/builder/list.ts b/lib/config/builder/list.ts index 3b20e61..3f07e97 100644 --- a/lib/config/builder/list.ts +++ b/lib/config/builder/list.ts @@ -260,7 +260,7 @@ export class List { required: false, }) - return topConfig()({ + return Config.of()({ myValue: a.withWrapperData(), }) ``` diff --git a/lib/config/builder/value.ts b/lib/config/builder/value.ts index d3e1d3c..c28caad 100644 --- a/lib/config/builder/value.ts +++ b/lib/config/builder/value.ts @@ -675,7 +675,7 @@ export class Value { required: false, }) - return topConfig()({ + return Config.of()({ myValue: a.withWrapperData(), }) ``` diff --git a/lib/config/builder/variants.ts b/lib/config/builder/variants.ts index 12e8481..0b6e8d0 100644 --- a/lib/config/builder/variants.ts +++ b/lib/config/builder/variants.ts @@ -65,20 +65,18 @@ export class Variants { static of< TypeMap extends Record>, WrapperData, - ConfigType, + TypeOut = { + [K in keyof TypeMap & string]: { + unionSelectKey: K + unionValueKey: TypeMap[K] + } + }[keyof TypeMap & string], >(a: { [K in keyof TypeMap]: { name: string spec: Config } }) { - type TypeOut = { - [K in keyof TypeMap & string]: { - unionSelectKey: K - unionValueKey: TypeMap[K] - } - }[keyof TypeMap & string] - const validator = anyOf( ...Object.entries(a).map(([name, { spec }]) => object({ @@ -111,7 +109,7 @@ export class Variants { required: false, }) - return topConfig()({ + return Config.of()({ myValue: a.withWrapperData(), }) ``` diff --git a/lib/test/configBuilder.test.ts b/lib/test/configBuilder.test.ts index cbd56d2..2e57b44 100644 --- a/lib/test/configBuilder.test.ts +++ b/lib/test/configBuilder.test.ts @@ -11,7 +11,7 @@ describe("builder tests", () => { test("text", async () => { const bitcoinPropertiesBuilt: { "peer-tor-address": ValueSpec - } = await Config.of({ + } = await Config.of()({ "peer-tor-address": Value.text({ name: "Peer tor address", description: "The Tor address of the peer interface", @@ -234,7 +234,7 @@ describe("values", () => { description: null, warning: null, }, - Config.of({ + Config.of()({ a: Value.toggle({ name: "test", description: null, @@ -259,7 +259,7 @@ describe("values", () => { Variants.of({ a: { name: "a", - spec: Config.of({ + spec: Config.of()({ b: Value.toggle({ name: "b", description: null, @@ -512,7 +512,7 @@ describe("values", () => { Variants.of({ a: { name: "a", - spec: Config.of({ + spec: Config.of()({ b: Value.toggle({ name: "b", description: null, @@ -523,7 +523,7 @@ describe("values", () => { }, b: { name: "b", - spec: Config.of({ + spec: Config.of()({ b: Value.toggle({ name: "b", description: null, @@ -584,7 +584,7 @@ describe("Builder List", () => { name: "test", }, { - spec: Config.of({ + spec: Config.of()({ test: Value.toggle({ name: "test", description: null, @@ -655,7 +655,7 @@ describe("Builder List", () => { describe("Nested nullable values", () => { test("Testing text", async () => { - const value = Config.of({ + const value = Config.of()({ a: Value.text({ name: "Temp Name", description: @@ -670,7 +670,7 @@ describe("Nested nullable values", () => { testOutput()(null) }) test("Testing number", async () => { - const value = Config.of({ + const value = Config.of()({ a: Value.number({ name: "Temp Name", description: @@ -692,7 +692,7 @@ describe("Nested nullable values", () => { testOutput()(null) }) test("Testing color", async () => { - const value = Config.of({ + const value = Config.of()({ a: Value.color({ name: "Temp Name", description: @@ -708,7 +708,7 @@ describe("Nested nullable values", () => { testOutput()(null) }) test("Testing select", async () => { - const value = Config.of({ + const value = Config.of()({ a: Value.select({ name: "Temp Name", description: @@ -737,7 +737,7 @@ describe("Nested nullable values", () => { testOutput()(null) }) test("Testing multiselect", async () => { - const value = Config.of({ + const value = Config.of()({ a: Value.multiselect({ name: "Temp Name", description: diff --git a/lib/test/configTypes.test.ts b/lib/test/configTypes.test.ts index a50ec6c..aab6a90 100644 --- a/lib/test/configTypes.test.ts +++ b/lib/test/configTypes.test.ts @@ -9,7 +9,7 @@ describe("Config Types", () => { for (const option of options) { const test = (option as any)( {} as any, - { spec: Config.of({}) } as any, + { spec: Config.of()({}) } as any, ) as any const someList = await Value.list(test).build({} as any) if (isValueSpecListOf(someList, "text")) { diff --git a/scripts/oldSpecToBuilder.ts b/scripts/oldSpecToBuilder.ts index 31e90d4..9f8dac3 100644 --- a/scripts/oldSpecToBuilder.ts +++ b/scripts/oldSpecToBuilder.ts @@ -36,7 +36,7 @@ export default async function makeFileContentFromOld( ) { const outputLines: string[] = [] outputLines.push(` - import { Config, topConfig } from "${startSdk}/lib/config/builder/config" + import { Config } from "${startSdk}/lib/config/builder/config" import { List } from "${startSdk}/lib/config/builder/list" import { Value } from "${startSdk}/lib/config/builder/value" import { Variants } from "${startSdk}/lib/config/builder/variants" @@ -46,10 +46,7 @@ import { Variants } from "${startSdk}/lib/config/builder/variants" const hammerWrapperData = !nested ? ".withWrapperData()" : "" const namedConsts = new Set(["Config", "Value", "List"]) - const configName = newConst( - "configSpec", - `topConfig()(${convertInputSpecInner(data)})`, - ) + const configName = newConst("configSpec", convertInputSpec(data)) const configMatcherName = newConst( "matchConfigSpec", `${configName}.validator`, @@ -82,7 +79,7 @@ import { Variants } from "${startSdk}/lib/config/builder/variants" } function convertInputSpec(data: any) { - return `Config.of(${convertInputSpecInner(data)})` + return `Config.of()(${convertInputSpecInner(data)})` } function convertValueSpec(value: any): string { switch (value.type) { @@ -366,7 +363,7 @@ import { Variants } from "${startSdk}/lib/config/builder/variants" const listConfig = maybeNewConst( value.name + "_list_config", ` - Config.of({ + Config.of()({ "union": ${unionValueName}${hammerWrapperData} }) `,