check build tree

This commit is contained in:
codetypes committed 2024-07-23 11:40:50 +08:00
1 parent 472bbbfbb8
commit 6d6fdf8c07
3 files changed
+145 -96

No files matched your search

+36 -23
View File
@@ -107,7 +107,7 @@ const NodeInspector: FC = () => {
form.setFieldValue("children", def.children); form.setFieldValue("children", def.children);
} }
def.args?.forEach((v) => { def.args?.forEach((v) => {
if (v.type === "object" || v.type === "object?") { if (v.type === "json" || v.type === "json?") {
form.setFieldValue( form.setFieldValue(
`args.${v.name}`, `args.${v.name}`,
JSON.stringify(data.args?.[v.name] ?? v.default, null, 2) JSON.stringify(data.args?.[v.name] ?? v.default, null, 2)
@@ -187,29 +187,42 @@ const NodeInspector: FC = () => {
data.desc = values.desc && values.desc !== def.desc ? values.desc : undefined; data.desc = values.desc && values.desc !== def.desc ? values.desc : undefined;
data.path = values.path || undefined; data.path = values.path || undefined;
def.args?.forEach((arg) => { if (def.args?.length) {
const v = values[`args.${arg.name}`]; def.args?.forEach((arg) => {
if (v !== null && v !== undefined && v !== "") { const v = values[`args.${arg.name}`];
data.args ||= {}; if (v !== null && v !== undefined && v !== "") {
if (arg.type === "object" || arg.type === "object?") { data.args ||= {};
data.args[arg.name] = JSON.parse(v); if (arg.type === "json" || arg.type === "json?") {
} else { data.args[arg.name] = JSON.parse(v);
data.args[arg.name] = v; } else {
data.args[arg.name] = v;
}
} }
} });
}); } else {
data.args = {};
}
def.input?.forEach((_, i) => { if (def.input?.length) {
const v = values[`input.${i}`]; def.input?.forEach((_, i) => {
data.input ||= []; const v = values[`input.${i}`];
data.input.push(v ?? ""); data.input ||= [];
}); data.input.push(v ?? "");
});
} else {
data.input = [];
}
if (def.output?.length) {
def.output?.forEach((_, i) => {
const v = values[`output.${i}`];
data.output ||= [];
data.output.push(v ?? "");
});
} else {
data.output = [];
}
def.output?.forEach((_, i) => {
const v = values[`output.${i}`];
data.output ||= [];
data.output.push(v ?? "");
});
workspace.editing?.dispatch("updateNode", { workspace.editing?.dispatch("updateNode", {
data: data, data: data,
} as EditNode); } as EditNode);
@@ -390,7 +403,7 @@ const NodeInspector: FC = () => {
{ required, message: t("node.fileRequired", { field: v.desc }) }, { required, message: t("node.fileRequired", { field: v.desc }) },
({ getFieldValue, setFieldValue, isFieldValidating, validateFields }) => ({ ({ getFieldValue, setFieldValue, isFieldValidating, validateFields }) => ({
validator(_, value) { validator(_, value) {
if (value && (v.type == "object" || v.type == "object?")) { if (value && (v.type == "json" || v.type == "json?")) {
try { try {
JSON.parse(value); JSON.parse(value);
} catch (e) { } catch (e) {
@@ -439,7 +452,7 @@ const NodeInspector: FC = () => {
{type === "string" && ( {type === "string" && (
<TextArea autoSize disabled={disabled} onBlur={form.submit} /> <TextArea autoSize disabled={disabled} onBlur={form.submit} />
)} )}
{type === "object" && ( {type === "json" && (
<TextArea autoSize disabled={disabled} onBlur={form.submit} /> <TextArea autoSize disabled={disabled} onBlur={form.submit} />
)} )}
{type === "int" && ( {type === "int" && (
+1 -1
View File
@@ -1,6 +1,6 @@
import { TreeGraphData as G6TreeGraphData } from "@antv/g6"; import { TreeGraphData as G6TreeGraphData } from "@antv/g6";
export type NodeArgType = "boolean" | "string" | "int" | "float" | "object" | "enum" | "code"; export type NodeArgType = "boolean" | "string" | "int" | "float" | "json" | "enum" | "code";
export type NodeType = "Action" | "Composite" | "Decorator" | "Condition" | "Other" | "Error"; export type NodeType = "Action" | "Composite" | "Decorator" | "Condition" | "Other" | "Error";
export interface NodeArgOption { export interface NodeArgOption {
+108 -72
View File
@@ -67,6 +67,10 @@ export const isNodeEqual = (node1: NodeModel, node2: NodeModel) => {
return false; return false;
}; };
const error = (data: NodeModel, msg: string) => {
console.error(`check ${data.id}|${data.name}: ${msg}`);
};
export const checkNodeData = (data: NodeModel | null | undefined) => { export const checkNodeData = (data: NodeModel | null | undefined) => {
if (!data) { if (!data) {
return false; return false;
@@ -77,9 +81,7 @@ export const checkNodeData = (data: NodeModel | null | undefined) => {
const count = data.children?.length || 0; const count = data.children?.length || 0;
if (conf.children !== count) { if (conf.children !== count) {
hasError = true; hasError = true;
console.error( error(data, `expect ${conf.children} children, but got ${count}`);
`check ${data.id}|${data.name}: expect ${conf.children} children, but got ${count}`
);
} }
} }
if (conf.input) { if (conf.input) {
@@ -91,11 +93,14 @@ export const checkNodeData = (data: NodeModel | null | undefined) => {
data.input[i] = ""; data.input[i] = "";
} }
if (conf.input[i].indexOf("?") === -1 && !data.input[i]) { if (conf.input[i].indexOf("?") === -1 && !data.input[i]) {
console.error(`check ${data.id}|${data.name}: intput field '${conf.input[i]}' is required`); error(data, `intput field '${conf.input[i]}' is required`);
hasError = true; hasError = true;
} }
} }
} }
if (data.input) {
data.input.length = conf.input?.length || 0;
}
if (conf.output) { if (conf.output) {
for (let i = 0; i < conf.output.length; i++) { for (let i = 0; i < conf.output.length; i++) {
if (!data.output) { if (!data.output) {
@@ -105,69 +110,74 @@ export const checkNodeData = (data: NodeModel | null | undefined) => {
data.output[i] = ""; data.output[i] = "";
} }
if (conf.output[i].indexOf("?") === -1 && !data.output[i]) { if (conf.output[i].indexOf("?") === -1 && !data.output[i]) {
console.error( error(data, `output field '${conf.output[i]}' is required`);
`check ${data.id}|${data.name}: output field '${conf.output[i]}' is required`
);
hasError = true; hasError = true;
} }
} }
} }
if (data.output) {
data.output.length = conf.output?.length || 0;
}
if (conf.args) { if (conf.args) {
for (let i = 0; i < conf.args.length; i++) { for (let i = 0; i < conf.args.length; i++) {
const arg = conf.args[i]; const arg = conf.args[i] as NodeArg;
const type = arg.type.replace("?", "") as NodeArgType; const type = arg.type.replace("?", "") as NodeArgType;
const value = data.args?.[arg.name]; const value = data.args?.[arg.name];
if (type === "float") { if (type === "float") {
if (!(typeof value === "number" || (value === undefined && arg.type === "float?"))) { const isNumber = typeof value === "number";
console.error(`check ${data.id}|${data.name}: '${arg.name}=${value}' is not a number`); const isOptional = value === undefined && arg.type === "float?";
if (!(isNumber || isOptional)) {
error(data, `'${arg.name}=${value}' is not a number`);
hasError = true; hasError = true;
} }
} else if (type === "int") { } else if (type === "int") {
if ( const isInt = typeof value === "number" && value === Math.floor(value);
!( const isOptional = value === undefined && arg.type === "int?";
(typeof value === "number" && value === Math.floor(value)) || if (!(isInt || isOptional)) {
(value === undefined && arg.type === "int?") error(data, `'${arg.name}=${value}' is not a int`);
)
) {
console.error(`check ${data.id}|${data.name}: '${arg.name}=${value}' is not a int`);
hasError = true; hasError = true;
} }
} else if (type === "string") { } else if (type === "string") {
if ( const isString = typeof value === "string" && value;
!( const isOptional = (value === undefined || value === "") && arg.type === "string?";
(typeof value === "string" && value) || if (!(isString || isOptional)) {
((value === undefined || value === "") && arg.type === "string?") error(data, `'${arg.name}=${value}' is not a string`);
)
) {
console.error(`check ${data.id}|${data.name}: '${arg.name}=${value}' is not a string`);
hasError = true; hasError = true;
} }
} else if (type === "enum") { } else if (type === "enum") {
const found = !!arg.options?.find((option) => option.value === value); const isEnum = !!arg.options?.find((option) => option.value === value);
if (!(found || (value === undefined && arg.type === "enum?"))) { const isOptional = value === undefined && arg.type === "enum?";
console.error( if (!(isEnum || isOptional)) {
`check ${data.id}|${data.name}: '${arg.name}=${value}' is not a one of the option values` error(data, `'${arg.name}=${value}' is not a one of the option values`);
);
hasError = true; hasError = true;
} }
} else if (type == "code") { } else if (type == "code") {
if ( const isCode = typeof value === "string" && value;
!( const isOptional = (value === undefined || value === "") && arg.type === "code?";
(typeof value === "string" && value) || if (!(isCode || isOptional)) {
((value === undefined || value === "") && arg.type === "code?") error(data, `'${arg.name}=${value}' is not a string`);
)
) {
console.error(`check ${data.id}|${data.name}: '${arg.name}=${value}' is not a string`);
hasError = true; hasError = true;
} }
} else if (type == "object") { } else if (type == "json") {
if (!((value !== undefined && value !== null && value !== "") || arg.type === "object?")) { const isJson = value !== undefined && value !== null && value !== "";
console.error( const isOptional = arg.type === "json?";
`check ${data.id}|${data.name}: '${arg.name}=${value}' is not an invalid object` if (!(isJson || isOptional)) {
); error(data, `'${arg.name}=${value}' is not an invalid object`);
hasError = true; hasError = true;
} }
} }
if (arg.oneof !== undefined) {
const idx = conf.input?.findIndex((v) => v.startsWith(arg.oneof!)) ?? -1;
const inputValue = data.input?.[idx] ?? "";
const argValue = data.args?.[arg.name] ?? "";
if ((inputValue !== "" && argValue !== "") || (inputValue === "" && argValue === "")) {
error(
data,
`only one is allowed for between argument '${arg.name}' and input '${data.input?.[idx]}'`
);
return false;
}
}
} }
} }
@@ -390,35 +400,50 @@ export const checkTreeData = (data: TreeGraphData) => {
if (conf.args) { if (conf.args) {
for (let i = 0; i < conf.args.length; i++) { for (let i = 0; i < conf.args.length; i++) {
const arg = conf.args[i] as NodeArg; const arg = conf.args[i] as NodeArg;
if (arg.type.indexOf("?") === -1) { const type = arg.type.replace("?", "") as NodeArgType;
const type = arg.type as NodeArgType; const value = data.args?.[arg.name];
const value = data.args?.[arg.name]; if (type === "float") {
if (type === "float") { const isNumber = typeof value === "number";
if (typeof value !== "number") { const isOptional = value === undefined && arg.type === "float?";
return false; if (!(isNumber || isOptional)) {
} return false;
} else if (type === "int") {
if (typeof value !== "number" || value !== Math.floor(value)) {
return false;
}
} else if (type === "string") {
if (!value || typeof value !== "string") {
return false;
}
} else if (type === "enum") {
if (!arg.options?.find((option) => option.value === value)) {
return false;
}
} else if (type === "code") {
if (!value || typeof value !== "string") {
return false;
}
} }
} else if (arg.oneof !== undefined) { } else if (type === "int") {
const isInt = typeof value === "number" && value === Math.floor(value);
const isOptional = value === undefined && arg.type === "int?";
if (!(isInt || isOptional)) {
return false;
}
} else if (type === "string") {
const isString = typeof value === "string" && value;
const isOptional = (value === undefined || value === "") && arg.type === "string?";
if (!(isString || isOptional)) {
return false;
}
} else if (type === "enum") {
const isEnum = !!arg.options?.find((option) => option.value === value);
const isOptional = value === undefined && arg.type === "enum?";
if (!(isEnum || isOptional)) {
return false;
}
} else if (type === "code") {
const isCode = typeof value === "string" && value;
const isOptional = (value === undefined || value === "") && arg.type === "code?";
if (!(isCode || isOptional)) {
return false;
}
} else if (type == "json") {
const isJson = value !== undefined && value !== null && value !== "";
const isOptional = arg.type === "json?";
if (!(isJson || isOptional)) {
return false;
}
}
if (arg.oneof !== undefined) {
const idx = conf.input?.findIndex((v) => v.startsWith(arg.oneof!)) ?? -1; const idx = conf.input?.findIndex((v) => v.startsWith(arg.oneof!)) ?? -1;
const input = data.input?.[idx] ?? ""; const inputValue = data.input?.[idx] ?? "";
const value = data.args?.[arg.name] ?? ""; const argValue = data.args?.[arg.name] ?? "";
if ((input !== "" && value !== "") || (input === "" && value === "")) { if ((inputValue !== "" && argValue !== "") || (inputValue === "" && argValue === "")) {
return false; return false;
} }
} }
@@ -473,9 +498,9 @@ export const createTreeData = (node: NodeModel, parent?: string) => {
treeData.parent = parent; treeData.parent = parent;
treeData.id = node.id.toFixed(); treeData.id = node.id.toFixed();
treeData.size = calcTreeDataSize(treeData); treeData.size = calcTreeDataSize(treeData);
} catch (error) { } catch (e) {
message.error(`解析子树失败:${node.path}`); message.error(`解析子树失败:${node.path}`);
console.log("parse subtree:", error); console.log("parse subtree:", e);
} }
parsingStack.pop(); parsingStack.pop();
} }
@@ -491,8 +516,8 @@ export const createBuildData = (path: string) => {
refreshTreeDataId(data); refreshTreeDataId(data);
treeModel.root = createFileData(data, true); treeModel.root = createFileData(data, true);
return treeModel as TreeModel; return treeModel as TreeModel;
} catch (error) { } catch (e) {
console.log("build error:", path, error); console.log("build error:", path, e);
} }
return null; return null;
}; };
@@ -509,6 +534,17 @@ export const createFileData = (data: TreeGraphData, includeSubtree?: boolean) =>
disabled: data.disabled || undefined, disabled: data.disabled || undefined,
path: data.path || undefined, path: data.path || undefined,
}; };
const conf = useWorkspace.getState().getNodeDef(data.name);
if (!conf.input?.length) {
nodeData.input = undefined;
}
if (!conf.output?.length) {
nodeData.output = undefined;
}
if (!conf.args?.length) {
nodeData.args = undefined;
}
if (data.children?.length && (includeSubtree || !isSubtreeRoot(data))) { if (data.children?.length && (includeSubtree || !isSubtreeRoot(data))) {
nodeData.children = []; nodeData.children = [];
data.children.forEach((child) => { data.children.forEach((child) => {