Skip to content

Commit

Permalink
feat: Add serializers for Any, GenericRecord, GenericArray, GenericEn…
Browse files Browse the repository at this point in the history
…ymSymbol, and GenericFixed
  • Loading branch information
Chuckame committed Sep 20, 2024
1 parent 712292d commit 517cc7a
Show file tree
Hide file tree
Showing 5 changed files with 645 additions and 7 deletions.
49 changes: 48 additions & 1 deletion api/avro4k-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class com/github/avrokotlin/avro4k/Avro : kotlinx/serialization/
public fun encodeToByteArray (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)[B
public final fun encodeToByteArray (Lorg/apache/avro/Schema;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)[B
public final fun getConfiguration ()Lcom/github/avrokotlin/avro4k/AvroConfiguration;
public fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public final fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public final fun schema (Lkotlinx/serialization/descriptors/SerialDescriptor;)Lorg/apache/avro/Schema;
}

Expand Down Expand Up @@ -427,6 +427,53 @@ public final class com/github/avrokotlin/avro4k/serializer/ElementLocation {
public fun toString ()Ljava/lang/String;
}

public final class com/github/avrokotlin/avro4k/serializer/GenericArraySerializer : com/github/avrokotlin/avro4k/serializer/AvroSerializer {
public static final field INSTANCE Lcom/github/avrokotlin/avro4k/serializer/GenericArraySerializer;
public synthetic fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Ljava/lang/Object;
public fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Lorg/apache/avro/generic/GenericArray;
public fun getSchema (Lcom/github/avrokotlin/avro4k/serializer/SchemaSupplierContext;)Lorg/apache/avro/Schema;
public synthetic fun serializeAvro (Lcom/github/avrokotlin/avro4k/AvroEncoder;Ljava/lang/Object;)V
public fun serializeAvro (Lcom/github/avrokotlin/avro4k/AvroEncoder;Lorg/apache/avro/generic/GenericArray;)V
}

public final class com/github/avrokotlin/avro4k/serializer/GenericDataSerializer : com/github/avrokotlin/avro4k/serializer/AvroSerializer {
public static final field INSTANCE Lcom/github/avrokotlin/avro4k/serializer/GenericDataSerializer;
public fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Ljava/lang/Object;
public fun getSchema (Lcom/github/avrokotlin/avro4k/serializer/SchemaSupplierContext;)Lorg/apache/avro/Schema;
public fun serializeAvro (Lcom/github/avrokotlin/avro4k/AvroEncoder;Ljava/lang/Object;)V
}

public final class com/github/avrokotlin/avro4k/serializer/GenericDataSerializersKt {
public static final fun getGenericDataSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
}

public final class com/github/avrokotlin/avro4k/serializer/GenericEnumSerializer : com/github/avrokotlin/avro4k/serializer/AvroSerializer {
public static final field INSTANCE Lcom/github/avrokotlin/avro4k/serializer/GenericEnumSerializer;
public synthetic fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Ljava/lang/Object;
public fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Lorg/apache/avro/generic/GenericEnumSymbol;
public fun getSchema (Lcom/github/avrokotlin/avro4k/serializer/SchemaSupplierContext;)Lorg/apache/avro/Schema;
public synthetic fun serializeAvro (Lcom/github/avrokotlin/avro4k/AvroEncoder;Ljava/lang/Object;)V
public fun serializeAvro (Lcom/github/avrokotlin/avro4k/AvroEncoder;Lorg/apache/avro/generic/GenericEnumSymbol;)V
}

public final class com/github/avrokotlin/avro4k/serializer/GenericFixedSerializer : com/github/avrokotlin/avro4k/serializer/AvroSerializer {
public static final field INSTANCE Lcom/github/avrokotlin/avro4k/serializer/GenericFixedSerializer;
public synthetic fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Ljava/lang/Object;
public fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Lorg/apache/avro/generic/GenericFixed;
public fun getSchema (Lcom/github/avrokotlin/avro4k/serializer/SchemaSupplierContext;)Lorg/apache/avro/Schema;
public synthetic fun serializeAvro (Lcom/github/avrokotlin/avro4k/AvroEncoder;Ljava/lang/Object;)V
public fun serializeAvro (Lcom/github/avrokotlin/avro4k/AvroEncoder;Lorg/apache/avro/generic/GenericFixed;)V
}

public final class com/github/avrokotlin/avro4k/serializer/GenericRecordSerializer : com/github/avrokotlin/avro4k/serializer/AvroSerializer {
public static final field INSTANCE Lcom/github/avrokotlin/avro4k/serializer/GenericRecordSerializer;
public synthetic fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Ljava/lang/Object;
public fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Lorg/apache/avro/generic/GenericRecord;
public fun getSchema (Lcom/github/avrokotlin/avro4k/serializer/SchemaSupplierContext;)Lorg/apache/avro/Schema;
public synthetic fun serializeAvro (Lcom/github/avrokotlin/avro4k/AvroEncoder;Ljava/lang/Object;)V
public fun serializeAvro (Lcom/github/avrokotlin/avro4k/AvroEncoder;Lorg/apache/avro/generic/GenericRecord;)V
}

public final class com/github/avrokotlin/avro4k/serializer/InstantSerializer : com/github/avrokotlin/avro4k/serializer/AvroSerializer {
public static final field INSTANCE Lcom/github/avrokotlin/avro4k/serializer/InstantSerializer;
public synthetic fun deserializeAvro (Lcom/github/avrokotlin/avro4k/AvroDecoder;)Ljava/lang/Object;
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.avrokotlin.avro4k.internal.EnumResolver
import com.github.avrokotlin.avro4k.internal.PolymorphicResolver
import com.github.avrokotlin.avro4k.internal.RecordResolver
import com.github.avrokotlin.avro4k.internal.schema.ValueVisitor
import com.github.avrokotlin.avro4k.serializer.GenericDataSerializersModule
import com.github.avrokotlin.avro4k.serializer.JavaStdLibSerializersModule
import com.github.avrokotlin.avro4k.serializer.JavaTimeSerializersModule
import kotlinx.serialization.BinaryFormat
Expand All @@ -28,7 +29,7 @@ import java.io.ByteArrayInputStream
*/
public sealed class Avro(
public val configuration: AvroConfiguration,
public override val serializersModule: SerializersModule,
public final override val serializersModule: SerializersModule,
) : BinaryFormat {
// We use the identity hash map because we could have multiple descriptors with the same name, especially
// when having 2 different version of the schema for the same name. kotlinx-serialization is instantiating the descriptors
Expand All @@ -42,7 +43,8 @@ public sealed class Avro(
public companion object Default : Avro(
AvroConfiguration(),
JavaStdLibSerializersModule +
JavaTimeSerializersModule
JavaTimeSerializersModule +
GenericDataSerializersModule
)

public fun schema(descriptor: SerialDescriptor): Schema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,20 +333,27 @@ internal abstract class AbstractAvroDirectDecoder(
"string",
Schema.Type.STRING,
Schema.Type.BYTES,
Schema.Type.FIXED
Schema.Type.FIXED,
Schema.Type.ENUM
)
}) {
when (it.type) {
Schema.Type.STRING,
Schema.Type.BYTES,
-> {
Schema.Type.STRING -> {
AnyValueDecoder { binaryDecoder.readString() }
}

Schema.Type.BYTES -> {
AnyValueDecoder { binaryDecoder.readBytes(null).array().decodeToString() }
}

Schema.Type.FIXED -> {
AnyValueDecoder { ByteArray(it.fixedSize).also { buf -> binaryDecoder.readFixed(buf) }.decodeToString() }
}

Schema.Type.ENUM -> {
AnyValueDecoder { it.enumSymbols[binaryDecoder.readEnum()] }
}

else -> null
}
}
Expand Down
Loading

0 comments on commit 517cc7a

Please sign in to comment.