mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 04:11:57 +00:00
feat: Make config of the config top
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import { Config } from "../config/builder"
|
import { Config, ExtractConfigType } from "../config/builder/config"
|
||||||
import { ExtractConfigType } from "../config/builder/config"
|
|
||||||
import { ActionMetaData, ActionResult, Effects, ExportedAction } from "../types"
|
import { ActionMetaData, ActionResult, Effects, ExportedAction } from "../types"
|
||||||
import { Utils, utils } from "../util"
|
import { Utils, utils } from "../util"
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { AutoConfigure, DeepPartial, Effects, ExpectedExports } from "../types"
|
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> = {
|
export type AutoConfigFrom<WD, Input, NestedConfigs> = {
|
||||||
[key in keyof NestedConfigs & string]: (options: {
|
[key in keyof NestedConfigs & string]: (options: {
|
||||||
|
|||||||
@@ -92,17 +92,19 @@ export class Config<Type extends Record<string, any>, WD> {
|
|||||||
return answer
|
return answer
|
||||||
}
|
}
|
||||||
|
|
||||||
static of<Type extends Record<string, any>, WrapperData>(spec: {
|
static of<WrapperData>() {
|
||||||
[K in keyof Type]: Value<Type[K], 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]>
|
const validatorObj = {} as {
|
||||||
|
[K in keyof Type]: Parser<unknown, Type[K]>
|
||||||
|
}
|
||||||
|
for (const key in spec) {
|
||||||
|
validatorObj[key] = spec[key].validator
|
||||||
|
}
|
||||||
|
const validator = object(validatorObj)
|
||||||
|
return new Config<Type, WrapperData>(spec, validator)
|
||||||
}
|
}
|
||||||
for (const key in spec) {
|
|
||||||
validatorObj[key] = spec[key].validator
|
|
||||||
}
|
|
||||||
const validator = object(validatorObj)
|
|
||||||
return new Config<Type, WrapperData>(spec, validator)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,7 +116,7 @@ export class Config<Type extends Record<string, any>, WD> {
|
|||||||
required: false,
|
required: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
return topConfig<WrapperData>()({
|
return Config.of<WrapperData>()({
|
||||||
myValue: a.withWrapperData(),
|
myValue: a.withWrapperData(),
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@@ -123,9 +125,3 @@ export class Config<Type extends Record<string, any>, WD> {
|
|||||||
return this as any as Config<Type, NewWrapperData>
|
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)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ export class List<Type, WD> {
|
|||||||
required: false,
|
required: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
return topConfig<WrapperData>()({
|
return Config.of<WrapperData>()({
|
||||||
myValue: a.withWrapperData(),
|
myValue: a.withWrapperData(),
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -675,7 +675,7 @@ export class Value<Type, WD> {
|
|||||||
required: false,
|
required: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
return topConfig<WrapperData>()({
|
return Config.of<WrapperData>()({
|
||||||
myValue: a.withWrapperData(),
|
myValue: a.withWrapperData(),
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -65,20 +65,18 @@ export class Variants<Type, WD> {
|
|||||||
static of<
|
static of<
|
||||||
TypeMap extends Record<string, Record<string, any>>,
|
TypeMap extends Record<string, Record<string, any>>,
|
||||||
WrapperData,
|
WrapperData,
|
||||||
ConfigType,
|
TypeOut = {
|
||||||
|
[K in keyof TypeMap & string]: {
|
||||||
|
unionSelectKey: K
|
||||||
|
unionValueKey: TypeMap[K]
|
||||||
|
}
|
||||||
|
}[keyof TypeMap & string],
|
||||||
>(a: {
|
>(a: {
|
||||||
[K in keyof TypeMap]: {
|
[K in keyof TypeMap]: {
|
||||||
name: string
|
name: string
|
||||||
spec: Config<TypeMap[K], WrapperData>
|
spec: Config<TypeMap[K], WrapperData>
|
||||||
}
|
}
|
||||||
}) {
|
}) {
|
||||||
type TypeOut = {
|
|
||||||
[K in keyof TypeMap & string]: {
|
|
||||||
unionSelectKey: K
|
|
||||||
unionValueKey: TypeMap[K]
|
|
||||||
}
|
|
||||||
}[keyof TypeMap & string]
|
|
||||||
|
|
||||||
const validator = anyOf(
|
const validator = anyOf(
|
||||||
...Object.entries(a).map(([name, { spec }]) =>
|
...Object.entries(a).map(([name, { spec }]) =>
|
||||||
object({
|
object({
|
||||||
@@ -111,7 +109,7 @@ export class Variants<Type, WD> {
|
|||||||
required: false,
|
required: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
return topConfig<WrapperData>()({
|
return Config.of<WrapperData>()({
|
||||||
myValue: a.withWrapperData(),
|
myValue: a.withWrapperData(),
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ describe("builder tests", () => {
|
|||||||
test("text", async () => {
|
test("text", async () => {
|
||||||
const bitcoinPropertiesBuilt: {
|
const bitcoinPropertiesBuilt: {
|
||||||
"peer-tor-address": ValueSpec
|
"peer-tor-address": ValueSpec
|
||||||
} = await Config.of({
|
} = await Config.of<unknown>()({
|
||||||
"peer-tor-address": Value.text({
|
"peer-tor-address": Value.text({
|
||||||
name: "Peer tor address",
|
name: "Peer tor address",
|
||||||
description: "The Tor address of the peer interface",
|
description: "The Tor address of the peer interface",
|
||||||
@@ -234,7 +234,7 @@ describe("values", () => {
|
|||||||
description: null,
|
description: null,
|
||||||
warning: null,
|
warning: null,
|
||||||
},
|
},
|
||||||
Config.of({
|
Config.of<null>()({
|
||||||
a: Value.toggle({
|
a: Value.toggle({
|
||||||
name: "test",
|
name: "test",
|
||||||
description: null,
|
description: null,
|
||||||
@@ -259,7 +259,7 @@ describe("values", () => {
|
|||||||
Variants.of({
|
Variants.of({
|
||||||
a: {
|
a: {
|
||||||
name: "a",
|
name: "a",
|
||||||
spec: Config.of({
|
spec: Config.of<unknown>()({
|
||||||
b: Value.toggle({
|
b: Value.toggle({
|
||||||
name: "b",
|
name: "b",
|
||||||
description: null,
|
description: null,
|
||||||
@@ -512,7 +512,7 @@ describe("values", () => {
|
|||||||
Variants.of({
|
Variants.of({
|
||||||
a: {
|
a: {
|
||||||
name: "a",
|
name: "a",
|
||||||
spec: Config.of({
|
spec: Config.of<unknown>()({
|
||||||
b: Value.toggle({
|
b: Value.toggle({
|
||||||
name: "b",
|
name: "b",
|
||||||
description: null,
|
description: null,
|
||||||
@@ -523,7 +523,7 @@ describe("values", () => {
|
|||||||
},
|
},
|
||||||
b: {
|
b: {
|
||||||
name: "b",
|
name: "b",
|
||||||
spec: Config.of({
|
spec: Config.of<unknown>()({
|
||||||
b: Value.toggle({
|
b: Value.toggle({
|
||||||
name: "b",
|
name: "b",
|
||||||
description: null,
|
description: null,
|
||||||
@@ -584,7 +584,7 @@ describe("Builder List", () => {
|
|||||||
name: "test",
|
name: "test",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
spec: Config.of({
|
spec: Config.of<unknown>()({
|
||||||
test: Value.toggle({
|
test: Value.toggle({
|
||||||
name: "test",
|
name: "test",
|
||||||
description: null,
|
description: null,
|
||||||
@@ -655,7 +655,7 @@ describe("Builder List", () => {
|
|||||||
|
|
||||||
describe("Nested nullable values", () => {
|
describe("Nested nullable values", () => {
|
||||||
test("Testing text", async () => {
|
test("Testing text", async () => {
|
||||||
const value = Config.of({
|
const value = Config.of<unknown>()({
|
||||||
a: Value.text({
|
a: Value.text({
|
||||||
name: "Temp Name",
|
name: "Temp Name",
|
||||||
description:
|
description:
|
||||||
@@ -670,7 +670,7 @@ describe("Nested nullable values", () => {
|
|||||||
testOutput<typeof validator._TYPE, { a: string | null | undefined }>()(null)
|
testOutput<typeof validator._TYPE, { a: string | null | undefined }>()(null)
|
||||||
})
|
})
|
||||||
test("Testing number", async () => {
|
test("Testing number", async () => {
|
||||||
const value = Config.of({
|
const value = Config.of<unknown>()({
|
||||||
a: Value.number({
|
a: Value.number({
|
||||||
name: "Temp Name",
|
name: "Temp Name",
|
||||||
description:
|
description:
|
||||||
@@ -692,7 +692,7 @@ describe("Nested nullable values", () => {
|
|||||||
testOutput<typeof validator._TYPE, { a: number | null | undefined }>()(null)
|
testOutput<typeof validator._TYPE, { a: number | null | undefined }>()(null)
|
||||||
})
|
})
|
||||||
test("Testing color", async () => {
|
test("Testing color", async () => {
|
||||||
const value = Config.of({
|
const value = Config.of<unknown>()({
|
||||||
a: Value.color({
|
a: Value.color({
|
||||||
name: "Temp Name",
|
name: "Temp Name",
|
||||||
description:
|
description:
|
||||||
@@ -708,7 +708,7 @@ describe("Nested nullable values", () => {
|
|||||||
testOutput<typeof validator._TYPE, { a: string | null | undefined }>()(null)
|
testOutput<typeof validator._TYPE, { a: string | null | undefined }>()(null)
|
||||||
})
|
})
|
||||||
test("Testing select", async () => {
|
test("Testing select", async () => {
|
||||||
const value = Config.of({
|
const value = Config.of<unknown>()({
|
||||||
a: Value.select({
|
a: Value.select({
|
||||||
name: "Temp Name",
|
name: "Temp Name",
|
||||||
description:
|
description:
|
||||||
@@ -737,7 +737,7 @@ describe("Nested nullable values", () => {
|
|||||||
testOutput<typeof validator._TYPE, { a: "a" | null | undefined }>()(null)
|
testOutput<typeof validator._TYPE, { a: "a" | null | undefined }>()(null)
|
||||||
})
|
})
|
||||||
test("Testing multiselect", async () => {
|
test("Testing multiselect", async () => {
|
||||||
const value = Config.of({
|
const value = Config.of<unknown>()({
|
||||||
a: Value.multiselect({
|
a: Value.multiselect({
|
||||||
name: "Temp Name",
|
name: "Temp Name",
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ describe("Config Types", () => {
|
|||||||
for (const option of options) {
|
for (const option of options) {
|
||||||
const test = (option as any)(
|
const test = (option as any)(
|
||||||
{} as any,
|
{} as any,
|
||||||
{ spec: Config.of({}) } as any,
|
{ spec: Config.of()({}) } as any,
|
||||||
) as any
|
) as any
|
||||||
const someList = await Value.list(test).build({} as any)
|
const someList = await Value.list(test).build({} as any)
|
||||||
if (isValueSpecListOf(someList, "text")) {
|
if (isValueSpecListOf(someList, "text")) {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ export default async function makeFileContentFromOld(
|
|||||||
) {
|
) {
|
||||||
const outputLines: string[] = []
|
const outputLines: string[] = []
|
||||||
outputLines.push(`
|
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 { List } from "${startSdk}/lib/config/builder/list"
|
||||||
import { Value } from "${startSdk}/lib/config/builder/value"
|
import { Value } from "${startSdk}/lib/config/builder/value"
|
||||||
import { Variants } from "${startSdk}/lib/config/builder/variants"
|
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 hammerWrapperData = !nested ? ".withWrapperData()" : ""
|
||||||
|
|
||||||
const namedConsts = new Set(["Config", "Value", "List"])
|
const namedConsts = new Set(["Config", "Value", "List"])
|
||||||
const configName = newConst(
|
const configName = newConst("configSpec", convertInputSpec(data))
|
||||||
"configSpec",
|
|
||||||
`topConfig<WrapperData>()(${convertInputSpecInner(data)})`,
|
|
||||||
)
|
|
||||||
const configMatcherName = newConst(
|
const configMatcherName = newConst(
|
||||||
"matchConfigSpec",
|
"matchConfigSpec",
|
||||||
`${configName}.validator`,
|
`${configName}.validator`,
|
||||||
@@ -82,7 +79,7 @@ import { Variants } from "${startSdk}/lib/config/builder/variants"
|
|||||||
}
|
}
|
||||||
|
|
||||||
function convertInputSpec(data: any) {
|
function convertInputSpec(data: any) {
|
||||||
return `Config.of(${convertInputSpecInner(data)})`
|
return `Config.of<WrapperData>()(${convertInputSpecInner(data)})`
|
||||||
}
|
}
|
||||||
function convertValueSpec(value: any): string {
|
function convertValueSpec(value: any): string {
|
||||||
switch (value.type) {
|
switch (value.type) {
|
||||||
@@ -366,7 +363,7 @@ import { Variants } from "${startSdk}/lib/config/builder/variants"
|
|||||||
const listConfig = maybeNewConst(
|
const listConfig = maybeNewConst(
|
||||||
value.name + "_list_config",
|
value.name + "_list_config",
|
||||||
`
|
`
|
||||||
Config.of({
|
Config.of<WrapperData>()({
|
||||||
"union": ${unionValueName}${hammerWrapperData}
|
"union": ${unionValueName}${hammerWrapperData}
|
||||||
})
|
})
|
||||||
`,
|
`,
|
||||||
|
|||||||
Reference in New Issue
Block a user