wip: Working with a nested nullable + default

This commit is contained in:
BluJ
2023-04-27 08:19:14 -06:00
parent 76f0a8b0bb
commit 333bddf345
5 changed files with 65 additions and 35 deletions

View File

@@ -18,8 +18,28 @@ import { guardAll } from "../../util";
import { DefaultString } from "../configTypes";
import { _ } from "../../util";
function flatten<A>(a: A): _<A> {
return a as _<A>;
type RequiredLike<A> =
| false
| true
| { default: A }
| { defaultWithRequired: A };
function requiredLikeToAbove<Input extends RequiredLike<any>>(
requiredLike: Input,
) {
// prettier-ignore
return {
default:
(typeof requiredLike === "object" ? (
'default' in requiredLike ? requiredLike.default : requiredLike.defaultWithRequired
) :
null) as Input extends { default: infer T } | {defaultWithRequired: infer T} ? T : null,
required: (requiredLike === true ? true : false) as (
Input extends true ? true :
Input extends { defaultWithRequired: unknown } ? true :
false
),
};
}
/**
* A value is going to be part of the form in the FE of the OS.
@@ -55,26 +75,33 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
...a,
});
}
static text<
A extends {
static text<Required extends RequiredLike<DefaultString>>(a: {
name: string;
description: string | null;
warning: string | null;
required: boolean;
default: DefaultString | null;
description?: string | null;
warning?: string | null;
required: Required;
/** Default = false */
masked: boolean;
placeholder: string | null;
minLength: number | null;
maxLength: number | null;
patterns: Pattern[];
masked?: boolean;
placeholder?: string | null;
minLength?: number | null;
maxLength?: number | null;
patterns?: Pattern[];
/** Default = 'text' */
inputmode: ValueSpecText["inputmode"];
},
>(a: A) {
inputmode?: ValueSpecText["inputmode"];
}) {
return new Value({
type: "text" as const,
description: null,
warning: null,
masked: false,
placeholder: null,
minLength: null,
maxLength: null,
patterns: [],
inputmode: "text",
...a,
...requiredLikeToAbove(a.required),
});
}
static textarea(a: {

View File

@@ -15,7 +15,6 @@ describe("builder tests", () => {
} = Config.of({
"peer-tor-address": Value.text({
name: "Peer tor address",
default: null,
description: "The Tor address of the peer interface",
warning: null,
required: true,
@@ -31,17 +30,17 @@ describe("builder tests", () => {
/*json*/ `{
"peer-tor-address": {
"type": "text",
"name": "Peer tor address",
"default": null,
"description": "The Tor address of the peer interface",
"warning": null,
"required": true,
"masked": true,
"placeholder": null,
"minLength": null,
"maxLength": null,
"patterns": [],
"inputmode":"text"
"inputmode":"text",
"name": "Peer tor address",
"required": true,
"default": null
}}`
.replaceAll("\n", " ")
.replaceAll(/\s{2,}/g, "")
@@ -68,7 +67,6 @@ describe("values", () => {
required: false,
description: null,
warning: null,
default: null,
masked: false,
placeholder: null,
minLength: null,
@@ -86,7 +84,6 @@ describe("values", () => {
required: true,
description: null,
warning: null,
default: null,
masked: false,
placeholder: null,
minLength: null,
@@ -340,7 +337,6 @@ describe("Nested nullable values", () => {
description:
"If no name is provided, the name from config will be used",
required: false,
default: null,
warning: null,
masked: false,
placeholder: null,

View File

@@ -281,7 +281,7 @@ writeConvertedFileFromOld(
spec: {
hostname: {
type: "string",
nullable: false,
nullable: true,
name: "Hostname",
description: "Domain or IP address of bitcoin peer",
pattern:

View File

@@ -18,8 +18,7 @@ export function testOutput<A, B>(): (c: IfEquals<A, B>) => null {
/// Testing the types of the input spec
testOutput<InputSpec["rpc"]["enable"], boolean>()(null);
// @ts-expect-error Because enable should be a boolean
testOutput<InputSpec["rpc"]["enable"], string>()(null);
testOutput<InputSpec["rpc"]["username"], string>()(null);
testOutput<InputSpec["rpc"]["username"], string>()(null);
testOutput<InputSpec["rpc"]["advanced"]["auth"], string[]>()(null);
@@ -28,9 +27,10 @@ testOutput<
"segwit" | "non-segwit"
>()(null);
testOutput<InputSpec["rpc"]["advanced"]["servertimeout"], number>()(null);
testOutput<InputSpec["advanced"]["peers"]["addnode"][0]["hostname"], string>()(
null,
);
testOutput<
InputSpec["advanced"]["peers"]["addnode"][0]["hostname"],
string | null | undefined
>()(null);
testOutput<
InputSpec["testListUnion"][0]["union"][UnionValueKey]["name"],
string
@@ -38,6 +38,9 @@ testOutput<
testOutput<InputSpec["testListUnion"][0]["union"][UnionSelectKey], "lnd">()(
null,
);
// @ts-expect-error Because enable should be a boolean
testOutput<InputSpec["rpc"]["enable"], string>()(null);
// prettier-ignore
// @ts-expect-error Expect that the string is the one above
testOutput<InputSpec["testListUnion"][0][UnionSelectKey][UnionSelectKey], "unionSelectKey">()(null);

View File

@@ -77,10 +77,14 @@ export default async function makeFileContentFromOld(
return `${rangeToTodoComment(value?.range)}Value.text(${JSON.stringify(
{
name: value.name || null,
default: value.default || null,
// prettier-ignore
required: (
value.default != null && !value.nullable ? {default: value.default} :
value.default != null && value.nullable ? {defaultWithRequired: value.default} :
!value.nullable
),
description: value.description || null,
warning: value.warning || null,
required: !(value.nullable || false),
masked: value.masked || false,
placeholder: value.placeholder || null,
inputmode: "text",