|
5 | 5 | package io.airbyte.cdk.load.data
|
6 | 6 |
|
7 | 7 | import com.fasterxml.jackson.databind.JsonNode
|
| 8 | +import io.github.oshai.kotlinlogging.KotlinLogging |
| 9 | + |
| 10 | +private val logger = KotlinLogging.logger {} |
8 | 11 |
|
9 | 12 | sealed interface AirbyteType {
|
10 | 13 | /**
|
@@ -64,6 +67,32 @@ data object ObjectTypeWithoutSchema : AirbyteType {
|
64 | 67 | }
|
65 | 68 |
|
66 | 69 | data class UnionType(val options: Set<AirbyteType>) : AirbyteType {
|
| 70 | + /** |
| 71 | + * This is a hack to handle weird schemas like {type: [object, string]}. If a stream's top-level |
| 72 | + * schema looks like this, we still want to be able to extract the object properties (i.e. treat |
| 73 | + * it as though the string option didn't exist). |
| 74 | + * |
| 75 | + * @throws IllegalArgumentException if we cannot extract columns from this schema |
| 76 | + */ |
| 77 | + override fun asColumns(): LinkedHashMap<String, FieldType> { |
| 78 | + logger.warn { "asColumns options=$options" } |
| 79 | + val numObjectOptions = options.count { it.isObject } |
| 80 | + if (numObjectOptions > 1) { |
| 81 | + logger.error { "Can't extract columns from a schema with multiple object options" } |
| 82 | + return LinkedHashMap() |
| 83 | + } |
| 84 | + |
| 85 | + var retVal: LinkedHashMap<String, FieldType> |
| 86 | + try { |
| 87 | + retVal = options.first { it.isObject }.asColumns() |
| 88 | + } catch (_: NoSuchElementException) { |
| 89 | + logger.error { "Can't extract columns from a schema with no object options" } |
| 90 | + retVal = LinkedHashMap() |
| 91 | + } |
| 92 | + logger.warn { "Union.asColumns retVal=$retVal" } |
| 93 | + return retVal |
| 94 | + } |
| 95 | + |
67 | 96 | companion object {
|
68 | 97 | fun of(options: Set<AirbyteType>): AirbyteType {
|
69 | 98 | if (options.size == 1) {
|
|
0 commit comments