From a1c48bc1b64da8b2c459fcfaa5882db4d1cbe5ff Mon Sep 17 00:00:00 2001 From: dennisrijsdijk Date: Sun, 18 Aug 2024 15:50:19 +0200 Subject: [PATCH 1/2] feat: enable propertyPath and defaultData for effectOutput (#2091) --- .../builtin/metadata/effect-output.ts | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/backend/variables/builtin/metadata/effect-output.ts b/src/backend/variables/builtin/metadata/effect-output.ts index 38f7c4798..c36ccb813 100644 --- a/src/backend/variables/builtin/metadata/effect-output.ts +++ b/src/backend/variables/builtin/metadata/effect-output.ts @@ -5,15 +5,59 @@ const model : ReplaceVariable = { definition: { handle: "effectOutput", usage: "effectOutput[name]", + examples: [ + { + usage: "effectOutput[name, 1]", + description: "Get an array item by providing an array index as a second argument." + }, + { + usage: "effectOutput[name, property]", + description: "Get a property by providing a property path (using dot notation) as a second argument." + }, + { + usage: "effectOutput[name, null, exampleString]", + description: "Set a default value in case the effect output doesn't exist yet." + }, + { + usage: "effectOutput[name, property, exampleString]", + description: "Set a default value in case the effect output doesn't have data at the specified property path." + } + ], description: "Get data that was outputted by a prior effect.", categories: [VariableCategory.ADVANCED], possibleDataOutput: [OutputDataType.NUMBER, OutputDataType.TEXT] }, // eslint-disable-next-line @typescript-eslint/no-inferrable-types - evaluator: ({ effectOutputs }, name: string = "") => { - const output = (effectOutputs ?? {})[name]; - return output || null; + evaluator: ( + { effectOutputs }, + name: string, + propertyPath: string, + defaultData: unknown = null + ) => { + let data = (effectOutputs ?? {})[name]; + + if (!data) { + return defaultData; + } + + if (propertyPath == null || propertyPath === "null" || propertyPath === '') { + return data; + } + + const nodes = propertyPath.split("."); + + try { + for (const node of nodes) { + if (data == null) { + return null; + } + data = data[node]; + } + return data ?? defaultData; + } catch (error) { + return defaultData; + } } }; From 267f7383d93d37ced6a3ed385fadf7d114df0615 Mon Sep 17 00:00:00 2001 From: dennisrijsdijk Date: Sun, 18 Aug 2024 15:59:48 +0200 Subject: [PATCH 2/2] fix: try to parse json string for obs raw --- src/backend/variables/builtin/metadata/effect-output.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/backend/variables/builtin/metadata/effect-output.ts b/src/backend/variables/builtin/metadata/effect-output.ts index c36ccb813..43737064f 100644 --- a/src/backend/variables/builtin/metadata/effect-output.ts +++ b/src/backend/variables/builtin/metadata/effect-output.ts @@ -47,6 +47,12 @@ const model : ReplaceVariable = { const nodes = propertyPath.split("."); + if (typeof data === "string") { + try { + data = JSON.parse(data as string); + } catch (_) { } + } + try { for (const node of nodes) { if (data == null) {