|
1 | 1 | import { isReference, parseReferencePath, SPECIAL_REFERENCE_PREFIXES } from '@/executor/consts' |
2 | | -import type { ResolutionContext, Resolver } from '@/executor/variables/resolvers/reference' |
| 2 | +import { |
| 3 | + navigatePath, |
| 4 | + type ResolutionContext, |
| 5 | + type Resolver, |
| 6 | +} from '@/executor/variables/resolvers/reference' |
3 | 7 | import type { SerializedWorkflow } from '@/serializer/types' |
4 | 8 | import { normalizeBlockName } from '@/stores/workflows/utils' |
5 | 9 |
|
@@ -50,7 +54,7 @@ export class BlockResolver implements Resolver { |
50 | 54 | return output |
51 | 55 | } |
52 | 56 |
|
53 | | - const result = this.navigatePath(output, pathParts) |
| 57 | + const result = navigatePath(output, pathParts) |
54 | 58 |
|
55 | 59 | if (result === undefined) { |
56 | 60 | const availableKeys = output && typeof output === 'object' ? Object.keys(output) : [] |
@@ -83,67 +87,6 @@ export class BlockResolver implements Resolver { |
83 | 87 | return this.blockByNormalizedName.get(normalized) |
84 | 88 | } |
85 | 89 |
|
86 | | - private navigatePath(obj: any, path: string[]): any { |
87 | | - let current = obj |
88 | | - for (const part of path) { |
89 | | - if (current === null || current === undefined) { |
90 | | - return undefined |
91 | | - } |
92 | | - |
93 | | - const arrayMatch = part.match(/^([^[]+)\[(\d+)\](.*)$/) |
94 | | - if (arrayMatch) { |
95 | | - current = this.resolvePartWithIndices(current, part, '', 'block') |
96 | | - } else if (/^\d+$/.test(part)) { |
97 | | - const index = Number.parseInt(part, 10) |
98 | | - current = Array.isArray(current) ? current[index] : undefined |
99 | | - } else { |
100 | | - current = current[part] |
101 | | - } |
102 | | - } |
103 | | - return current |
104 | | - } |
105 | | - |
106 | | - private resolvePartWithIndices( |
107 | | - base: any, |
108 | | - part: string, |
109 | | - fullPath: string, |
110 | | - sourceName: string |
111 | | - ): any { |
112 | | - let value = base |
113 | | - |
114 | | - const propMatch = part.match(/^([^[]+)/) |
115 | | - let rest = part |
116 | | - if (propMatch) { |
117 | | - const prop = propMatch[1] |
118 | | - value = value[prop] |
119 | | - rest = part.slice(prop.length) |
120 | | - if (value === undefined) { |
121 | | - throw new Error(`No value found at path "${fullPath}" in block "${sourceName}".`) |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - const indexRe = /^\[(\d+)\]/ |
126 | | - while (rest.length > 0) { |
127 | | - const m = rest.match(indexRe) |
128 | | - if (!m) { |
129 | | - throw new Error(`Invalid path "${part}" in "${fullPath}" for block "${sourceName}".`) |
130 | | - } |
131 | | - const idx = Number.parseInt(m[1], 10) |
132 | | - if (!Array.isArray(value)) { |
133 | | - throw new Error(`Invalid path "${part}" in "${fullPath}" for block "${sourceName}".`) |
134 | | - } |
135 | | - if (idx < 0 || idx >= value.length) { |
136 | | - throw new Error( |
137 | | - `Array index ${idx} out of bounds (length: ${value.length}) in path "${part}"` |
138 | | - ) |
139 | | - } |
140 | | - value = value[idx] |
141 | | - rest = rest.slice(m[0].length) |
142 | | - } |
143 | | - |
144 | | - return value |
145 | | - } |
146 | | - |
147 | 90 | public formatValueForBlock( |
148 | 91 | value: any, |
149 | 92 | blockType: string | undefined, |
|
0 commit comments