-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: introduce AirbyteRecord and AirbyteJsonRecordAdapter (#13774)
- Loading branch information
Showing
15 changed files
with
288 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
airbyte-config/config-models/src/main/kotlin/io/airbyte/config/adapters/AirbyteRecord.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.airbyte.config.adapters | ||
|
||
import io.airbyte.config.StreamDescriptor | ||
import io.airbyte.protocol.models.AirbyteMessage | ||
|
||
interface AirbyteRecord { | ||
val streamDescriptor: StreamDescriptor | ||
val asProtocol: AirbyteMessage | ||
|
||
fun has(fieldName: String): Boolean | ||
|
||
fun get(fieldName: String): Value | ||
|
||
fun remove(fieldName: String) | ||
|
||
fun <T : Any> set( | ||
fieldName: String, | ||
value: T, | ||
) | ||
} | ||
|
||
interface Value { | ||
fun asBoolean(): Boolean | ||
|
||
fun asNumber(): Number | ||
|
||
fun asString(): String | ||
} |
49 changes: 49 additions & 0 deletions
49
airbyte-config/config-models/src/main/kotlin/io/airbyte/config/adapters/JsonAdapters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.airbyte.config.adapters | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.BooleanNode | ||
import com.fasterxml.jackson.databind.node.DoubleNode | ||
import com.fasterxml.jackson.databind.node.IntNode | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.fasterxml.jackson.databind.node.TextNode | ||
import io.airbyte.config.StreamDescriptor | ||
import io.airbyte.protocol.models.AirbyteMessage | ||
|
||
class JsonValueAdapter(private val node: JsonNode) : Value { | ||
override fun asBoolean(): Boolean = node.asBoolean() | ||
|
||
override fun asNumber(): Number = node.asDouble() | ||
|
||
override fun asString(): String = node.asText() | ||
} | ||
|
||
data class AirbyteJsonRecordAdapter(private val message: AirbyteMessage) : AirbyteRecord { | ||
override val asProtocol: AirbyteMessage | ||
get() = message | ||
override val streamDescriptor: StreamDescriptor = StreamDescriptor().withNamespace(message.record.namespace).withName(message.record.stream) | ||
private val data: ObjectNode = message.record.data as ObjectNode | ||
|
||
override fun has(fieldName: String): Boolean = data.has(fieldName) | ||
|
||
override fun get(fieldName: String): Value = JsonValueAdapter(data.get(fieldName)) | ||
|
||
override fun remove(fieldName: String) { | ||
data.remove(fieldName) | ||
} | ||
|
||
override fun <T : Any> set( | ||
fieldName: String, | ||
value: T, | ||
) { | ||
data.set<JsonNode>(fieldName, createNode(value)) | ||
} | ||
|
||
private fun <T : Any> createNode(value: T): JsonNode = | ||
when (value) { | ||
is Boolean -> BooleanNode.valueOf(value) | ||
is Double -> DoubleNode.valueOf(value) | ||
is Int -> IntNode.valueOf(value) | ||
is String -> TextNode.valueOf(value) | ||
else -> TODO("Unsupported type ${value::class.java.name}") | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
...-config/config-models/src/test/kotlin/io/airbyte/config/adapters/JsonRecordAdapterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package io.airbyte.config.adapters | ||
|
||
import io.airbyte.commons.json.Jsons | ||
import io.airbyte.config.StreamDescriptor | ||
import io.airbyte.protocol.models.AirbyteMessage | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class JsonRecordAdapterTest { | ||
companion object { | ||
const val BOOLEAN_FIELD = "boolean-field" | ||
const val INT_FIELD = "int-field" | ||
const val NUMBER_FIELD = "number-field" | ||
const val STRING_FIELD = "string-field" | ||
} | ||
|
||
private val jsonRecordString = | ||
""" | ||
{ | ||
"type": "RECORD", | ||
"record": { | ||
"stream": "stream-name", | ||
"namespace": "stream-namespace", | ||
"emitted_at": 1337, | ||
"data": { | ||
"$STRING_FIELD": "bar", | ||
"$BOOLEAN_FIELD": true, | ||
"$INT_FIELD": 42, | ||
"$NUMBER_FIELD": 4.2 | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
|
||
@Test | ||
fun `basic read`() { | ||
val adapter = getAdapterFromRecord(jsonRecordString) | ||
|
||
assertEquals(StreamDescriptor().withName("stream-name").withNamespace("stream-namespace"), adapter.streamDescriptor) | ||
|
||
assertEquals("bar", adapter.get(STRING_FIELD).asString()) | ||
assertEquals(false, adapter.get(STRING_FIELD).asBoolean()) | ||
|
||
assertEquals(true, adapter.get(BOOLEAN_FIELD).asBoolean()) | ||
assertEquals("true", adapter.get(BOOLEAN_FIELD).asString()) | ||
|
||
assertEquals("42", adapter.get(INT_FIELD).asString()) | ||
|
||
assertEquals("4.2", adapter.get(NUMBER_FIELD).asString()) | ||
} | ||
|
||
@Test | ||
fun `verify modify then serialize`() { | ||
val adapter = getAdapterFromRecord(jsonRecordString) | ||
adapter.set(STRING_FIELD, "woohoo") | ||
|
||
val serialized = Jsons.serialize(adapter.asProtocol) | ||
val deserialized = Jsons.deserialize(serialized, AirbyteMessage::class.java) | ||
assertEquals(adapter.asProtocol, deserialized) | ||
} | ||
|
||
@Test | ||
fun `writing boolean`() { | ||
val adapter = getAdapterFromRecord(jsonRecordString) | ||
|
||
adapter.set(STRING_FIELD, true) | ||
assertEquals(true, adapter.get(STRING_FIELD).asBoolean()) | ||
|
||
adapter.set(BOOLEAN_FIELD, false) | ||
assertEquals(false, adapter.get(BOOLEAN_FIELD).asBoolean()) | ||
} | ||
|
||
@Test | ||
fun `writing double`() { | ||
val adapter = getAdapterFromRecord(jsonRecordString) | ||
|
||
adapter.set(NUMBER_FIELD, 1.1) | ||
assertEquals(1.1, adapter.get(NUMBER_FIELD).asNumber()) | ||
|
||
adapter.set(STRING_FIELD, 2) | ||
assertEquals(2.0, adapter.get(STRING_FIELD).asNumber()) | ||
} | ||
|
||
@Test | ||
fun `writing numbers`() { | ||
val adapter = getAdapterFromRecord(jsonRecordString) | ||
|
||
adapter.set(NUMBER_FIELD, 1) | ||
assertEquals(1.0, adapter.get(NUMBER_FIELD).asNumber()) | ||
|
||
adapter.set(STRING_FIELD, 2) | ||
assertEquals(2.0, adapter.get(STRING_FIELD).asNumber()) | ||
} | ||
|
||
@Test | ||
fun `writing strings`() { | ||
val adapter = getAdapterFromRecord(jsonRecordString) | ||
|
||
adapter.set(STRING_FIELD, "updated") | ||
assertEquals("updated", adapter.get(STRING_FIELD).asString()) | ||
|
||
adapter.set(BOOLEAN_FIELD, "overridden") | ||
assertEquals("overridden", adapter.get(BOOLEAN_FIELD).asString()) | ||
} | ||
|
||
fun getAdapterFromRecord(jsonString: String) = AirbyteJsonRecordAdapter(getRecord(jsonString)) | ||
|
||
fun getRecord(jsonString: String): AirbyteMessage = Jsons.deserialize(jsonString, AirbyteMessage::class.java) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.