( connectionType: ConnectionType, value: unknown, )
| 91 | } |
| 92 | |
| 93 | function coerceValueForConnectionType( |
| 94 | connectionType: ConnectionType, |
| 95 | value: unknown, |
| 96 | ): CoercionResult { |
| 97 | if (connectionType.kind === 'primitive') { |
| 98 | return coercePrimitiveValue(connectionType.name, value); |
| 99 | } |
| 100 | |
| 101 | if (connectionType.kind === 'contract') { |
| 102 | return { ok: true, value }; |
| 103 | } |
| 104 | |
| 105 | if (connectionType.kind === 'list') { |
| 106 | if (!Array.isArray(value)) { |
| 107 | return { ok: false, error: 'Expected array for list port' }; |
| 108 | } |
| 109 | const coerced: unknown[] = []; |
| 110 | for (const item of value) { |
| 111 | if (!connectionType.element) { |
| 112 | return { ok: false, error: 'Connection type element is null for list item' }; |
| 113 | } |
| 114 | const result = coerceValueForConnectionType(connectionType.element, item); |
| 115 | if (!result.ok) { |
| 116 | return { ok: false, error: result.error ?? 'Failed to coerce list item' }; |
| 117 | } |
| 118 | coerced.push(result.value); |
| 119 | } |
| 120 | return { ok: true, value: coerced }; |
| 121 | } |
| 122 | |
| 123 | if (connectionType.kind === 'map') { |
| 124 | if (!value || typeof value !== 'object' || Array.isArray(value)) { |
| 125 | return { ok: false, error: 'Expected object for map port' }; |
| 126 | } |
| 127 | const inputRecord = value as Record<string, unknown>; |
| 128 | const coerced: Record<string, unknown> = {}; |
| 129 | for (const [key, entry] of Object.entries(inputRecord)) { |
| 130 | if (!connectionType.element) { |
| 131 | return { ok: false, error: `Connection type element is null for key ${key}` }; |
| 132 | } |
| 133 | const result = coerceValueForConnectionType(connectionType.element, entry); |
| 134 | if (!result.ok) { |
| 135 | return { ok: false, error: result.error ?? `Failed to coerce value for key ${key}` }; |
| 136 | } |
| 137 | coerced[key] = result.value; |
| 138 | } |
| 139 | return { ok: true, value: coerced }; |
| 140 | } |
| 141 | |
| 142 | return { ok: true, value }; |
| 143 | } |
| 144 | |
| 145 | export function resolveInputValue(sourceOutput: unknown, sourceHandle: string): unknown { |
| 146 | if (sourceOutput === null || sourceOutput === undefined) { |
no test coverage detected