rename enum and add multiselect

This commit is contained in:
Matt Hill
2023-03-25 12:49:58 -06:00
parent 997daca59b
commit 16fed45f4b
9 changed files with 58 additions and 53 deletions

View File

@@ -19,7 +19,7 @@ import { Value } from "./value";
The idea of a config is that now the form is going to ask for
Test: [ ] and the value is going to be checked as a boolean.
There are more complex values like enums, lists, and objects. See {@link Value}
There are more complex values like selects, lists, and objects. See {@link Value}
Also, there is the ability to get a validator/parser from this config spec.
```ts
@@ -81,7 +81,7 @@ import { Value } from "./value";
"warning": null,
});
export const auth = Value.list(authorizationList);
export const serialversion = Value.enum({
export const serialversion = Value.select({
"name": "Serialization Version",
"description":
"Return raw transaction or block hex with Segwit or non-SegWit serialization.",

View File

@@ -59,24 +59,6 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
...a,
});
}
static enum<
A extends Description &
Default<string[]> & {
range: string;
spec: {
values: string[];
"value-names": {
[key: string]: string;
};
};
}
>(a: A) {
return new List({
type: "list" as const,
subtype: "enum" as const,
...a,
});
}
static obj<
A extends Description &
Default<Record<string, unknown>[]> & {

View File

@@ -95,7 +95,7 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
...a,
} as ValueSpecNumber);
}
static enum<
static select<
A extends Description &
Default<string> & {
values: readonly string[] | string[];
@@ -103,7 +103,7 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
}
>(a: A) {
return new Value({
type: "enum" as const,
type: "select" as const,
...a,
});
}

View File

@@ -3,9 +3,9 @@ import { BuilderExtract, IBuilder } from "./builder";
import { Config } from ".";
/**
* Used in the the Value.enum { @link './value.ts' }
* to indicate the type of enums variants that are available. The key for the record passed in will be the
* key to the tag.id in the Value.enum
* Used in the the Value.select { @link './value.ts' }
* to indicate the type of select variants that are available. The key for the record passed in will be the
* key to the tag.id in the Value.select
```ts
export const pruningSettingsVariants = Variants.of({
"disabled": disabled,

View File

@@ -97,7 +97,7 @@ export default async function makeFileContent(
)})`;
}
case "enum": {
return `Value.enum(${JSON.stringify(
return `Value.select(${JSON.stringify(
{
name: value.name || null,
description: value.description || null,
@@ -205,14 +205,12 @@ export default async function makeFileContent(
)})`;
}
case "enum": {
return `List.enum(${JSON.stringify(
return `Value.multiselect(${JSON.stringify(
{
name: value.name || null,
range: value.range || null,
spec: {
values: value?.spec?.["values"] || null,
"value-names": value?.spec?.["value-names"] || {},
},
values: value?.spec?.["values"] || null,
"value-names": value?.spec?.["value-names"] || {},
default: value.default || null,
description: value.description || null,
warning: value.warning || null,

View File

@@ -4,7 +4,8 @@ export type ValueType =
| "string"
| "number"
| "boolean"
| "enum"
| "select"
| "multiselect"
| "list"
| "object"
| "file"
@@ -18,8 +19,10 @@ export type ValueSpecOf<T extends ValueType> = T extends "string"
? ValueSpecNumber
: T extends "boolean"
? ValueSpecBoolean
: T extends "enum"
? ValueSpecEnum
: T extends "select"
? ValueSpecSelect
: T extends "multiselect"
? ValueSpecMultiselect
: T extends "list"
? ValueSpecList
: T extends "object"
@@ -43,11 +46,17 @@ export interface ValueSpecNumber extends ListValueSpecNumber, WithStandalone {
default: null | number;
}
export interface ValueSpecEnum extends ListValueSpecEnum, WithStandalone {
type: "enum";
export interface ValueSpecSelect extends SelectBase, WithStandalone {
type: "select";
default: string;
}
export interface ValueSpecMultiselect extends SelectBase, WithStandalone {
type: "multiselect";
range: string; // '[0,1]' (inclusive) OR '[0,*)' (right unbounded), normal math rules
default: string[];
}
export interface ValueSpecBoolean extends WithStandalone {
type: "boolean";
default: boolean;
@@ -78,11 +87,15 @@ export interface WithStandalone {
warning: null | string;
}
export interface SelectBase {
values: string[] | readonly string[];
"value-names": { [value: string]: string };
}
// no lists of booleans, lists
export type ListValueSpecType =
| "string"
| "number"
| "enum"
| "object"
| "union";
@@ -91,8 +104,6 @@ export type ListValueSpecOf<T extends ListValueSpecType> = T extends "string"
? ListValueSpecString
: T extends "number"
? ListValueSpecNumber
: T extends "enum"
? ListValueSpecEnum
: T extends "object"
? ListValueSpecObject
: T extends "union"
@@ -134,17 +145,12 @@ export interface ListValueSpecString {
}
export interface ListValueSpecNumber {
range: string;
range: string; // '[0,1]' (inclusive) OR '[0,*)' (right unbounded), normal math rules
integral: boolean;
units: null | string;
placeholder: null | string;
}
export interface ListValueSpecEnum {
values: string[] | readonly string[];
"value-names": { [value: string]: string };
}
export interface ListValueSpecObject {
spec: InputSpec; // this is a mapped type of the config object at this level, replacing the object's values with specs on those values
"unique-by": UniqueBy; // indicates whether duplicates can be permitted in the list

View File

@@ -1,5 +1,4 @@
import { writeConvertedFile } from "../../scripts/oldSpecToBuilder";
import { writeFileSync, readFileSync } from "fs";
writeConvertedFile(
"./lib/util/artifacts/output.ts",
@@ -66,7 +65,7 @@ writeConvertedFile(
name: "Serialization Version",
description:
"Return raw transaction or block hex with Segwit or non-SegWit serialization.",
type: "enum",
type: "select",
values: ["non-segwit", "segwit"],
"value-names": {},
default: "segwit",

View File

@@ -51,7 +51,7 @@ export const authorizationList = List.string({
warning: null,
});
export const auth = Value.list(authorizationList);
export const serialversion = Value.enum({
export const serialversion = Value.select({
name: "Serialization Version",
description:
"Return raw transaction or block hex with Segwit or non-SegWit serialization.",

View File

@@ -6,7 +6,8 @@ type TypeString = "string";
type TypeNumber = "number";
type TypeObject = "object";
type TypeList = "list";
type TypeEnum = "enum";
type TypeSelect = "select";
type TypeMultiselect = "multiselect";
type TypePointer = "pointer";
type TypeUnion = "union";
@@ -49,8 +50,12 @@ type GuardPointer<A> =
A extends {readonly type:TypePointer} ? (string | null) :
unknown
// prettier-ignore
type GuardEnum<A> =
A extends {readonly type:TypeEnum, readonly values: ArrayLike<infer B>} ? GuardDefaultNullable<A, B> :
type GuardSelect<A> =
A extends {readonly type:TypeSelect, readonly values: ArrayLike<infer B>} ? GuardDefaultNullable<A, B> :
unknown
// prettier-ignore
type GuardMultiselect<A> =
A extends {readonly type:TypeMultiselect, readonly values: ArrayLike<infer B>} ? GuardDefaultNullable<A, B> :
unknown
// prettier-ignore
type GuardUnion<A> =
@@ -65,7 +70,8 @@ export type GuardAll<A> = GuardNumber<A> &
GuardList<A> &
GuardPointer<A> &
GuardUnion<A> &
GuardEnum<A>;
GuardSelect<A> &
GuardMultiselect<A>;
// prettier-ignore
export type TypeFromProps<A> =
A extends Record<string, unknown> ? {readonly [K in keyof A & string]: _<GuardAll<A[K]>>} :
@@ -247,7 +253,7 @@ export function guardAll<A extends ValueSpecAny>(
value
) as any;
}
case "enum":
case "select":
if (matchValues.test(value)) {
return defaultNullable(
matches.literals(value.values[0], ...value.values),
@@ -255,6 +261,20 @@ export function guardAll<A extends ValueSpecAny>(
) as any;
}
return matches.unknown as any;
case "multiselect":
if (matchValues.test(value)) {
const rangeValidate =
(matchRange.test(value) && matchNumberWithRange(value.range).test) ||
(() => true);
return defaultNullable(
matches
.literals(value.values[0], ...value.values)
.validate((x) => rangeValidate(x.length), "valid length"),
value
) as any;
}
return matches.unknown as any;
case "union":
if (matchUnion.test(value)) {
return matches.some(