Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: No more kotlin-reflect for logical types #214

Merged
merged 10 commits into from
May 23, 2024
Prev Previous commit
Next Next commit
perf: Stop using tagger encoder
Chuckame committed May 21, 2024
commit 7afa6adecd9783e10bf538d256f68a343be19019
21 changes: 21 additions & 0 deletions api/avro4k-core.api
Original file line number Diff line number Diff line change
@@ -158,6 +158,11 @@ public final class com/github/avrokotlin/avro4k/decoder/AvroDecoder$DefaultImpls
public static fun decodeSerializableValue (Lcom/github/avrokotlin/avro4k/decoder/AvroDecoder;Lkotlinx/serialization/DeserializationStrategy;)Ljava/lang/Object;
}

public final class com/github/avrokotlin/avro4k/decoder/AvroDecoderKt {
public static final fun decodeResolvingUnion (Lcom/github/avrokotlin/avro4k/decoder/AvroDecoder;Lorg/apache/avro/Schema;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun resolveUnion (Lcom/github/avrokotlin/avro4k/decoder/AvroDecoder;Lorg/apache/avro/Schema;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
}

public abstract interface class com/github/avrokotlin/avro4k/encoder/AvroEncoder : kotlinx/serialization/encoding/Encoder {
public abstract fun getCurrentWriterSchema ()Lorg/apache/avro/Schema;
}
@@ -167,6 +172,22 @@ public final class com/github/avrokotlin/avro4k/encoder/AvroEncoder$DefaultImpls
public static fun encodeSerializableValue (Lcom/github/avrokotlin/avro4k/encoder/AvroEncoder;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
}

public final class com/github/avrokotlin/avro4k/encoder/AvroEncoderKt {
public static final fun encodeResolvingUnion (Lcom/github/avrokotlin/avro4k/encoder/AvroEncoder;Lorg/apache/avro/Schema;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun resolveUnion (Lcom/github/avrokotlin/avro4k/encoder/AvroEncoder;Lorg/apache/avro/Schema;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
}

public abstract interface class com/github/avrokotlin/avro4k/encoder/UnionEncoder : com/github/avrokotlin/avro4k/encoder/AvroEncoder {
public abstract fun getCurrentWriterSchema ()Lorg/apache/avro/Schema;
public abstract fun selectUnionIndex (I)V
public abstract fun setCurrentWriterSchema (Lorg/apache/avro/Schema;)V
}

public final class com/github/avrokotlin/avro4k/encoder/UnionEncoder$DefaultImpls {
public static fun beginCollection (Lcom/github/avrokotlin/avro4k/encoder/UnionEncoder;Lkotlinx/serialization/descriptors/SerialDescriptor;I)Lkotlinx/serialization/encoding/CompositeEncoder;
public static fun encodeSerializableValue (Lcom/github/avrokotlin/avro4k/encoder/UnionEncoder;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
}

public abstract interface class com/github/avrokotlin/avro4k/schema/FieldNamingStrategy {
public static final field Builtins Lcom/github/avrokotlin/avro4k/schema/FieldNamingStrategy$Builtins;
public abstract fun resolve (Lkotlinx/serialization/descriptors/SerialDescriptor;I)Ljava/lang/String;
2 changes: 0 additions & 2 deletions src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import com.github.avrokotlin.avro4k.decoder.AvroValueDecoder
import com.github.avrokotlin.avro4k.encoder.AvroValueEncoder
import com.github.avrokotlin.avro4k.internal.EnumResolver
import com.github.avrokotlin.avro4k.internal.RecordResolver
import com.github.avrokotlin.avro4k.internal.UnionResolver
import com.github.avrokotlin.avro4k.schema.FieldNamingStrategy
import com.github.avrokotlin.avro4k.schema.ValueVisitor
import com.github.avrokotlin.avro4k.serializer.BigDecimalSerializer
@@ -49,7 +48,6 @@ public sealed class Avro(
private val schemaCache: MutableMap<SerialDescriptor, Schema> = WeakIdentityHashMap()

internal val recordResolver = RecordResolver(this)
internal val unionResolver = UnionResolver()
internal val enumResolver = EnumResolver()

public companion object Default : Avro(
Original file line number Diff line number Diff line change
@@ -26,4 +26,40 @@ public interface AvroDecoder : Decoder {
*/
@ExperimentalSerializationApi
public fun decodeValue(): Any
}

@ExperimentalSerializationApi
public inline fun <T : Any> AvroDecoder.decodeResolvingUnion(
error: () -> Throwable,
resolver: (Schema) -> (() -> T)?,
): T {
val schema = currentWriterSchema
return decodeResolvingUnion(schema, error, resolver)
}

@PublishedApi
internal inline fun <T : Any> AvroDecoder.decodeResolvingUnion(
schema: Schema,
error: () -> Throwable,
resolver: (Schema) -> (() -> T)?,
): T {
return if (schema.type == Schema.Type.UNION) {
resolveUnion(schema, resolver)
} else {
resolver(schema)?.invoke()
} ?: throw error()
}

@PublishedApi
internal inline fun <T> AvroDecoder.resolveUnion(
schema: Schema,
resolver: (Schema) -> (() -> T)?,
): T? {
for (index in schema.types.indices) {
val subSchema = schema.types[index]
resolver(subSchema)?.let {
return it.invoke()
}
}
return null
}
Loading