feat: Make config of the config top

This commit is contained in:
BluJ
2023-05-02 17:36:23 -06:00
parent c8cb6b9a38
commit 513ef55633
9 changed files with 42 additions and 50 deletions

View File

@@ -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"

View File

@@ -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<WD, Input, NestedConfigs> = {
[key in keyof NestedConfigs & string]: (options: {

View File

@@ -92,9 +92,10 @@ export class Config<Type extends Record<string, any>, WD> {
return answer
}
static of<Type extends Record<string, any>, WrapperData>(spec: {
static of<WrapperData>() {
return <Type extends Record<string, any>>(spec: {
[K in keyof Type]: Value<Type[K], WrapperData>
}) {
}) => {
const validatorObj = {} as {
[K in keyof Type]: Parser<unknown, Type[K]>
}
@@ -104,6 +105,7 @@ export class Config<Type extends Record<string, any>, WD> {
const validator = object(validatorObj)
return new Config<Type, WrapperData>(spec, validator)
}
}
/**
* Use this during the times that the input needs a more specific type.
@@ -114,7 +116,7 @@ export class Config<Type extends Record<string, any>, WD> {
required: false,
})
return topConfig<WrapperData>()({
return Config.of<WrapperData>()({
myValue: a.withWrapperData(),
})
```
@@ -123,9 +125,3 @@ export class Config<Type extends Record<string, any>, WD> {
return this as any as Config<Type, NewWrapperData>
}
}
export function topConfig<WrapperData>() {
return <Type extends Record<string, any>>(spec: {
[K in keyof Type]: Value<Type[K], WrapperData>
}) => Config.of<Type, WrapperData>(spec)
}

View File

@@ -260,7 +260,7 @@ export class List<Type, WD> {
required: false,
})
return topConfig<WrapperData>()({
return Config.of<WrapperData>()({
myValue: a.withWrapperData(),
})
```

View File

@@ -675,7 +675,7 @@ export class Value<Type, WD> {
required: false,
})
return topConfig<WrapperData>()({
return Config.of<WrapperData>()({
myValue: a.withWrapperData(),
})
```

View File

@@ -65,20 +65,18 @@ export class Variants<Type, WD> {
static of<
TypeMap extends Record<string, Record<string, any>>,
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<TypeMap[K], WrapperData>
}
}) {
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<Type, WD> {
required: false,
})
return topConfig<WrapperData>()({
return Config.of<WrapperData>()({
myValue: a.withWrapperData(),
})
```

View File

@@ -11,7 +11,7 @@ describe("builder tests", () => {
test("text", async () => {
const bitcoinPropertiesBuilt: {
"peer-tor-address": ValueSpec
} = await Config.of({
} = await Config.of<unknown>()({
"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<null>()({
a: Value.toggle({
name: "test",
description: null,
@@ -259,7 +259,7 @@ describe("values", () => {
Variants.of({
a: {
name: "a",
spec: Config.of({
spec: Config.of<unknown>()({
b: Value.toggle({
name: "b",
description: null,
@@ -512,7 +512,7 @@ describe("values", () => {
Variants.of({
a: {
name: "a",
spec: Config.of({
spec: Config.of<unknown>()({
b: Value.toggle({
name: "b",
description: null,
@@ -523,7 +523,7 @@ describe("values", () => {
},
b: {
name: "b",
spec: Config.of({
spec: Config.of<unknown>()({
b: Value.toggle({
name: "b",
description: null,
@@ -584,7 +584,7 @@ describe("Builder List", () => {
name: "test",
},
{
spec: Config.of({
spec: Config.of<unknown>()({
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<unknown>()({
a: Value.text({
name: "Temp Name",
description:
@@ -670,7 +670,7 @@ describe("Nested nullable values", () => {
testOutput<typeof validator._TYPE, { a: string | null | undefined }>()(null)
})
test("Testing number", async () => {
const value = Config.of({
const value = Config.of<unknown>()({
a: Value.number({
name: "Temp Name",
description:
@@ -692,7 +692,7 @@ describe("Nested nullable values", () => {
testOutput<typeof validator._TYPE, { a: number | null | undefined }>()(null)
})
test("Testing color", async () => {
const value = Config.of({
const value = Config.of<unknown>()({
a: Value.color({
name: "Temp Name",
description:
@@ -708,7 +708,7 @@ describe("Nested nullable values", () => {
testOutput<typeof validator._TYPE, { a: string | null | undefined }>()(null)
})
test("Testing select", async () => {
const value = Config.of({
const value = Config.of<unknown>()({
a: Value.select({
name: "Temp Name",
description:
@@ -737,7 +737,7 @@ describe("Nested nullable values", () => {
testOutput<typeof validator._TYPE, { a: "a" | null | undefined }>()(null)
})
test("Testing multiselect", async () => {
const value = Config.of({
const value = Config.of<unknown>()({
a: Value.multiselect({
name: "Temp Name",
description:

View File

@@ -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")) {

View File

@@ -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<WrapperData>()(${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<WrapperData>()(${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<WrapperData>()({
"union": ${unionValueName}${hammerWrapperData}
})
`,