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"
import sdk from "../sdk"
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.
@ -139,13 +139,12 @@ export function stringSplit(value: string | string[]) {
return value
}
export function typecastForLooping(loopStep: LoopStep) {
const input = loopStep.inputs
export function typecastForLooping(input: LoopInput) {
if (!input || !input.binding) {
return null
}
try {
switch (loopStep.inputs.option) {
switch (input.option) {
case LoopStepType.ARRAY:
if (typeof input.binding === "string") {
return JSON.parse(input.binding)

View File

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

View File

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

View File

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