More loop step typing improvements.

This commit is contained in:
Sam Rose 2024-01-30 10:37:23 +00:00
parent 67a848bb86
commit 456817ee7b
No known key found for this signature in database
4 changed files with 17 additions and 26 deletions

View File

@ -5,7 +5,7 @@ import {
} from "@budibase/string-templates" } from "@budibase/string-templates"
import sdk from "../sdk" import sdk from "../sdk"
import { Row } from "@budibase/types" import { Row } from "@budibase/types"
import { LoopStep, LoopStepType } from "../definitions/automations" import { LoopInput, LoopStep, LoopStepType } from "../definitions/automations"
/** /**
* When values are input to the system generally they will be of type string as this is required for template strings. * When values are input to the system generally they will be of type string as this is required for template strings.
@ -139,13 +139,12 @@ export function stringSplit(value: string | string[]) {
return value return value
} }
export function typecastForLooping(loopStep: LoopStep) { export function typecastForLooping(input: LoopInput) {
const input = loopStep.inputs
if (!input || !input.binding) { if (!input || !input.binding) {
return null return null
} }
try { try {
switch (loopStep.inputs.option) { switch (input.option) {
case LoopStepType.ARRAY: case LoopStepType.ARRAY:
if (typeof input.binding === "string") { if (typeof input.binding === "string") {
return JSON.parse(input.binding) return JSON.parse(input.binding)

View File

@ -33,32 +33,20 @@ describe("automationUtils", () => {
describe("typeCastForLooping", () => { describe("typeCastForLooping", () => {
it("should parse to correct type", () => { it("should parse to correct type", () => {
expect( expect(
typecastForLooping({ typecastForLooping({ option: LoopStepType.ARRAY, binding: [1, 2, 3] })
inputs: { option: LoopStepType.ARRAY, binding: [1, 2, 3] },
} as LoopStep)
).toEqual([1, 2, 3]) ).toEqual([1, 2, 3])
expect( expect(
typecastForLooping({ typecastForLooping({ option: LoopStepType.ARRAY, binding: "[1,2,3]" })
inputs: { option: LoopStepType.ARRAY, binding: "[1,2,3]" },
} as LoopStep)
).toEqual([1, 2, 3]) ).toEqual([1, 2, 3])
expect( expect(
typecastForLooping({ typecastForLooping({ option: LoopStepType.STRING, binding: [1, 2, 3] })
inputs: { option: LoopStepType.STRING, binding: [1, 2, 3] },
} as LoopStep)
).toEqual("1,2,3") ).toEqual("1,2,3")
}) })
it("should handle null values", () => { it("should handle null values", () => {
// expect it to handle where the binding is null // expect it to handle where the binding is null
expect( expect(typecastForLooping({ option: LoopStepType.ARRAY })).toEqual(null)
typecastForLooping({
inputs: { option: LoopStepType.ARRAY },
} as LoopStep)
).toEqual(null)
expect(() => expect(() =>
typecastForLooping({ typecastForLooping({ option: LoopStepType.ARRAY, binding: "test" })
inputs: { option: LoopStepType.ARRAY, binding: "test" },
} as LoopStep)
).toThrow() ).toThrow()
}) })
}) })

View File

@ -12,6 +12,8 @@ export interface LoopStep extends AutomationStep {
export interface LoopInput { export interface LoopInput {
option: LoopStepType option: LoopStepType
binding?: string[] | string | number[] binding?: string[] | string | number[]
iterations?: string
failure?: any
} }
export interface TriggerOutput { export interface TriggerOutput {

View File

@ -23,6 +23,7 @@ import {
} from "@budibase/types" } from "@budibase/types"
import { import {
AutomationContext, AutomationContext,
LoopInput,
LoopStep, LoopStep,
TriggerOutput, TriggerOutput,
} from "../definitions/automations" } from "../definitions/automations"
@ -254,7 +255,7 @@ class Orchestrator {
this._context.env = await sdkUtils.getEnvironmentVariables() this._context.env = await sdkUtils.getEnvironmentVariables()
let automation = this._automation let automation = this._automation
let stopped = false let stopped = false
let loopStep: AutomationStep | undefined = undefined let loopStep: LoopStep | undefined = undefined
let stepCount = 0 let stepCount = 0
let loopStepNumber: any = undefined let loopStepNumber: any = undefined
@ -309,7 +310,7 @@ class Orchestrator {
stepCount++ stepCount++
if (step.stepId === LOOP_STEP_ID) { if (step.stepId === LOOP_STEP_ID) {
loopStep = step loopStep = step as LoopStep
loopStepNumber = stepCount loopStepNumber = stepCount
continue continue
} }
@ -329,7 +330,7 @@ class Orchestrator {
} }
try { try {
loopStep.inputs.binding = automationUtils.typecastForLooping( loopStep.inputs.binding = automationUtils.typecastForLooping(
loopStep as LoopStep loopStep.inputs as LoopInput
) )
} catch (err) { } catch (err) {
this.updateContextAndOutput( this.updateContextAndOutput(
@ -345,7 +346,7 @@ class Orchestrator {
loopStep = undefined loopStep = undefined
break break
} }
let item = [] let item: any[] = []
if ( if (
typeof loopStep.inputs.binding === "string" && typeof loopStep.inputs.binding === "string" &&
loopStep.inputs.option === "String" loopStep.inputs.option === "String"
@ -396,7 +397,8 @@ class Orchestrator {
if ( if (
index === env.AUTOMATION_MAX_ITERATIONS || index === env.AUTOMATION_MAX_ITERATIONS ||
index === parseInt(loopStep.inputs.iterations) (loopStep.inputs.iterations &&
index === parseInt(loopStep.inputs.iterations))
) { ) {
this.updateContextAndOutput( this.updateContextAndOutput(
loopStepNumber, loopStepNumber,