From c53854eecaeb4f4c3a0371bf6ffeb81bbcd2d500 Mon Sep 17 00:00:00 2001 From: codetypes Date: Mon, 9 Sep 2024 22:29:36 +0800 Subject: [PATCH] add optional array type --- src/components/inspector.tsx | 1 + src/misc/b3type.ts | 9 ++++++++- src/misc/b3util.ts | 18 +++++++----------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/components/inspector.tsx b/src/components/inspector.tsx index 03a6f36..63adc65 100644 --- a/src/components/inspector.tsx +++ b/src/components/inspector.tsx @@ -637,6 +637,7 @@ const NodeInspector: FC = () => { }} style={{ width: "100%" }} icon={} + danger={items.length === 0 && !isNodeArgOptional(arg)} > {t("node.args.add")} diff --git a/src/misc/b3type.ts b/src/misc/b3type.ts index f714405..14fff18 100644 --- a/src/misc/b3type.ts +++ b/src/misc/b3type.ts @@ -8,24 +8,31 @@ export interface NodeArg { | "boolean" | "boolean?" | "boolean[]" + | "boolean[]?" | "int" | "int?" | "int[]" + | "int[]?" | "float" | "float?" | "float[]" + | "float[]?" | "string" | "string?" | "string[]" + | "string[]?" | "json" | "json?" | "json[]" + | "json[]?" | "enum" | "enum?" | "enum[]" + | "enum[]?" | "code" | "code?" - | "code[]"; + | "code[]" + | "code[]?"; desc: string; oneof?: string; default?: unknown; diff --git a/src/misc/b3util.ts b/src/misc/b3util.ts index cf3ad67..6d7892c 100644 --- a/src/misc/b3util.ts +++ b/src/misc/b3util.ts @@ -199,13 +199,11 @@ export const checkNodeArg = ( const arg = conf.args![i] as NodeArg; const value = data.args?.[arg.name]; if (isNodeArgArray(arg)) { - if (!Array.isArray(value)) { - if (value !== undefined) { - if (verbose) { - error(data, `'${arg.name}=${value}' is not an array`); - } - hasError = true; + if (!Array.isArray(value) || (!isNodeArgOptional(arg) && value.length === 0)) { + if (verbose) { + error(data, `'${arg.name}=${JSON.stringify(value)}' is not an array or empty array`); } + hasError = true; } else { for (let j = 0; j < value.length; j++) { if (!checkNodeArgValue(data, arg, value[j], verbose)) { @@ -300,13 +298,11 @@ export const checkNodeData = (data: NodeModel | null | undefined) => { for (let i = 0; i < conf.args.length; i++) { const key = conf.args[i].name; const value = data.args?.[key]; - if (!checkNodeArg(data, conf, i, true)) { - hasError = true; - } if (value !== undefined) { args[key] = value; - } else if (isNodeArgArray(conf.args[i])) { - args[key] = []; + } + if (!checkNodeArg(data, conf, i, true)) { + hasError = true; } } data.args = args;