( inputFormat: InputFormatField[], workflowInput: unknown )
| 270 | } |
| 271 | |
| 272 | function deriveInputFromFormat( |
| 273 | inputFormat: InputFormatField[], |
| 274 | workflowInput: unknown |
| 275 | ): DerivedInputResult { |
| 276 | const structuredInput: Record<string, unknown> = {} |
| 277 | |
| 278 | if (inputFormat.length === 0) { |
| 279 | return { |
| 280 | structuredInput, |
| 281 | finalInput: getRawInputCandidate(workflowInput), |
| 282 | hasStructured: false, |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | for (const field of inputFormat) { |
| 287 | const fieldName = readInputFormatFieldName(field) |
| 288 | if (!fieldName) continue |
| 289 | |
| 290 | let fieldValue: unknown |
| 291 | const workflowRecord = isRecordLike(workflowInput) ? workflowInput : undefined |
| 292 | |
| 293 | if (workflowRecord) { |
| 294 | const inputContainer = workflowRecord.input |
| 295 | if (isRecordLike(inputContainer) && Object.hasOwn(inputContainer, fieldName)) { |
| 296 | fieldValue = inputContainer[fieldName] |
| 297 | } else if (Object.hasOwn(workflowRecord, fieldName)) { |
| 298 | fieldValue = workflowRecord[fieldName] |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // Use the default value from inputFormat if the field value wasn't provided at runtime |
| 303 | if (fieldValue === undefined || fieldValue === null) { |
| 304 | fieldValue = field.value |
| 305 | } |
| 306 | |
| 307 | structuredInput[fieldName] = coerceValue(field.type, fieldValue) |
| 308 | } |
| 309 | |
| 310 | const hasStructured = Object.keys(structuredInput).length > 0 |
| 311 | const finalInput = hasStructured ? structuredInput : getRawInputCandidate(workflowInput) |
| 312 | |
| 313 | return { |
| 314 | structuredInput, |
| 315 | finalInput, |
| 316 | hasStructured, |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | function getRawInputCandidate(workflowInput: unknown): unknown { |
| 321 | if (isRecordLike(workflowInput) && Object.hasOwn(workflowInput, 'input')) { |
no test coverage detected