-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26d55b9
commit 1c71162
Showing
36 changed files
with
421 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,5 @@ build/ | |
### DEV | ||
_tmp/ | ||
.repository/ | ||
|
||
.console.sh |
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
29 changes: 0 additions & 29 deletions
29
avro-kotlin-serialization/src/main/kotlin/_kserializer-reflection.kt
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
package io.toolisticon.kotlin.avro.serialization | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import org.apache.avro.specific.SpecificRecordBase | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.companionObject | ||
import kotlin.reflect.full.companionObjectInstance | ||
import kotlin.reflect.full.functions | ||
|
||
/** | ||
* Reflective access to [KSerializer<*>] for a given type. | ||
* | ||
* The type has to be a data class or enum and annotated with [Serializable]. | ||
* | ||
* @return kserializer of given type | ||
* @throws IllegalArgumentException when type is not a valid [Serializable] type | ||
*/ | ||
@Throws(IllegalArgumentException::class) | ||
@Deprecated("provided directly by kotlinx.serialization") | ||
fun KClass<*>.kserializer(): KSerializer<*> { | ||
require(this.isData) { "Type ${this.qualifiedName} is not a data class." } | ||
require(this.isSerializable()) { "Type ${this.qualifiedName} is not serializable." } | ||
|
||
val serializerFn = | ||
requireNotNull(this.companionObject?.functions?.find { it.name == "serializer" }) { "Type ${this.qualifiedName} must have a Companion.serializer, as created by the serialization compiler plugin." } | ||
|
||
return serializerFn.call(this.companionObjectInstance) as KSerializer<*> | ||
} | ||
|
||
fun KClass<*>.isSerializable(): Boolean = this.annotations.any { it is Serializable } | ||
|
||
fun KClass<*>.isKotlinxDataClass(): Boolean { | ||
// TODO: can this check be replaced by some convenience magic from kotlinx.serialization | ||
return this.isData && this.isSerializable() | ||
} | ||
|
||
fun KClass<*>.isKotlinxEnumClass(): Boolean { | ||
// TODO: can this check be replaced by some convenience magic from kotlinx.serialization | ||
return this.java.isEnum && this.isSerializable() | ||
} | ||
|
||
fun KClass<*>.isGeneratedSpecificRecordBase(): Boolean = SpecificRecordBase::class.java.isAssignableFrom(this.java) |
12 changes: 12 additions & 0 deletions
12
avro-kotlin-serialization/src/main/kotlin/strategy/GenericRecordSerializationStrategy.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,12 @@ | ||
package io.toolisticon.kotlin.avro.serialization.strategy | ||
|
||
import org.apache.avro.generic.GenericRecord | ||
import java.util.function.Predicate | ||
import kotlin.reflect.KClass | ||
|
||
interface GenericRecordSerializationStrategy : Predicate<KClass<*>> { | ||
|
||
fun <T : Any> deserialize(serializedType: KClass<*>, data: GenericRecord): T | ||
|
||
fun <T : Any> serialize(data: T): GenericRecord | ||
} |
22 changes: 22 additions & 0 deletions
22
avro-kotlin-serialization/src/main/kotlin/strategy/KotlinxDataClassStrategy.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,22 @@ | ||
package io.toolisticon.kotlin.avro.serialization.strategy | ||
|
||
import io.toolisticon.kotlin.avro.serialization.AvroKotlinSerialization | ||
import io.toolisticon.kotlin.avro.serialization.isKotlinxDataClass | ||
import org.apache.avro.generic.GenericRecord | ||
import kotlin.reflect.KClass | ||
|
||
class KotlinxDataClassStrategy( | ||
private val avroKotlinSerialization: AvroKotlinSerialization | ||
) : GenericRecordSerializationStrategy { | ||
|
||
override fun test(serializedType: KClass<*>): Boolean = serializedType.isKotlinxDataClass() | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : Any> deserialize(serializedType: KClass<*>, data: GenericRecord): T { | ||
return avroKotlinSerialization.genericRecordDecoder(serializedType).decode(data) as T | ||
} | ||
|
||
override fun <T : Any> serialize(data: T): GenericRecord { | ||
return avroKotlinSerialization.genericRecordEncoder<T>().encode(data) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
avro-kotlin-serialization/src/main/kotlin/strategy/KotlinxEnumClassStrategy.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,22 @@ | ||
package io.toolisticon.kotlin.avro.serialization.strategy | ||
|
||
import io.toolisticon.kotlin.avro.serialization.AvroKotlinSerialization | ||
import io.toolisticon.kotlin.avro.serialization.isKotlinxEnumClass | ||
import org.apache.avro.generic.GenericRecord | ||
import kotlin.reflect.KClass | ||
|
||
class KotlinxEnumClassStrategy( | ||
private val avroKotlinSerialization: AvroKotlinSerialization | ||
) : GenericRecordSerializationStrategy { | ||
|
||
override fun test(serializedType: KClass<*>): Boolean = serializedType.isKotlinxEnumClass() | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : Any> deserialize(serializedType: KClass<*>, data: GenericRecord): T { | ||
return avroKotlinSerialization.genericRecordDecoder(serializedType).decode(data) as T | ||
} | ||
|
||
override fun <T : Any> serialize(data: T): GenericRecord { | ||
return avroKotlinSerialization.genericRecordEncoder<T>().encode(data) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
avro-kotlin-serialization/src/main/kotlin/strategy/SpecificRecordBaseStrategy.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,20 @@ | ||
package io.toolisticon.kotlin.avro.serialization.strategy | ||
|
||
import io.toolisticon.kotlin.avro.codec.SpecificRecordCodec | ||
import io.toolisticon.kotlin.avro.serialization.isGeneratedSpecificRecordBase | ||
import org.apache.avro.generic.GenericRecord | ||
import org.apache.avro.specific.SpecificRecordBase | ||
import kotlin.reflect.KClass | ||
|
||
class SpecificRecordBaseStrategy : GenericRecordSerializationStrategy { | ||
private val converter = SpecificRecordCodec.specificRecordToGenericRecordConverter() | ||
Check warning on line 10 in avro-kotlin-serialization/src/main/kotlin/strategy/SpecificRecordBaseStrategy.kt Codecov / codecov/patchavro-kotlin-serialization/src/main/kotlin/strategy/SpecificRecordBaseStrategy.kt#L9-L10
|
||
|
||
override fun test(serializedType: KClass<*>): Boolean = serializedType.isGeneratedSpecificRecordBase() | ||
|
||
override fun <T : Any> deserialize(serializedType: KClass<*>, data: GenericRecord): T { | ||
@Suppress("UNCHECKED_CAST") | ||
return SpecificRecordCodec.genericRecordToSpecificRecordConverter(serializedType.java).convert(data) as T | ||
} | ||
|
||
override fun <T : Any> serialize(data: T): GenericRecord = converter.convert(data as SpecificRecordBase) | ||
} |
Oops, something went wrong.