From 2ecc97dadc865b1fd0e2d2d80cb8d5b4b9a279b6 Mon Sep 17 00:00:00 2001 From: Chuckame Date: Sat, 3 Feb 2024 01:16:04 +0100 Subject: [PATCH] feat: Add naming cache --- README.md | 118 ++++++++++++++++-- .../com/github/avrokotlin/avro4k/Avro.kt | 11 +- .../avrokotlin/avro4k/AvroConfiguration.kt | 45 ++++++- .../avrokotlin/avro4k/decoder/ListDecoder.kt | 6 +- .../avrokotlin/avro4k/decoder/MapDecoder.kt | 7 +- .../avro4k/decoder/RecordDecoder.kt | 5 +- .../avro4k/decoder/RootRecordDecoder.kt | 4 +- .../avrokotlin/avro4k/decoder/UnionDecoder.kt | 4 +- .../avrokotlin/avro4k/encoder/ListEncoder.kt | 4 +- .../avrokotlin/avro4k/encoder/MapEncoder.kt | 4 +- .../avro4k/encoder/RecordEncoder.kt | 7 +- .../avro4k/encoder/RootRecordEncoder.kt | 4 +- .../avrokotlin/avro4k/encoder/UnionEncoder.kt | 4 +- .../avro4k/schema/ClassSchemaFor.kt | 4 +- .../avrokotlin/avro4k/schema/SchemaFor.kt | 14 +-- .../avro4k/schema/UnionSchemaFor.kt | 4 +- .../avro4k/AvroSerializationAssertThat.kt | 4 +- .../avro4k/schema/AvroNamespaceTest.kt | 9 +- 18 files changed, 202 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 85fc67b6..f642103c 100644 --- a/README.md +++ b/README.md @@ -120,13 +120,12 @@ Would normally have a schema like this: } ``` -However we can override the name and/or the namespace like this: +#### Overriding the class name and the namespace ```kotlin package com.github.avrokotlin.avro4k.example -@AvroName("Wibble") -@AvroNamespace("com.other") +@SerialName("com.other.Wibble") data class Foo(val a: String) ``` @@ -146,15 +145,67 @@ And then the generated schema looks like this: } ``` -Note: It is possible, but not necessary, to use both AvroName and AvroNamespace. You can just use either of them if you wish. +#### Overriding only the namespace +We can also just override the namespace while keeping the class name as record name: +```kotlin +package com.github.avrokotlin.avro4k.example + +@AvroNamespaceOverride("com.other") +data class Foo(val a: String) +``` + +And then the generated schema looks like this: + +```json +{ + "type":"record", + "name":"Foo", + "namespace":"com.other", + "fields":[ + { + "name":"a", + "type":"string" + } + ] +} +``` + + + +#### Overriding only the name + +We can just override the name while keeping the namespace. Note that you need to replicate the namespace in the `@SerialName` annotation: + +```kotlin +package com.github.avrokotlin.avro4k.example + +@SerialName("com.github.avrokotlin.avro4k.example.Wibble") +data class Foo(val a: String) +``` + +And then the generated schema looks like this: + +```json +{ + "type":"record", + "name":"Wibble", + "namespace":"com.github.avrokotlin.avro4k.example", + "fields":[ + { + "name":"a", + "type":"string" + } + ] +} +``` ### Overriding a field name -The `@AvroName` annotation can also be used to override field names. +The `@SerialName` annotation can also be used to override field names. This is useful when the record instances you are generating or reading need to have field names different from the Kotlin data classes. For example if you are reading data generated by another system or another language. @@ -163,7 +214,7 @@ Given the following class. ```kotlin package com.github.avrokotlin.avro4k.example -data class Foo(val a: String, @AvroName("z") val b : String) +data class Foo(val a: String, @SerialName("z") val b : String) ``` Then the generated schema would look like this: @@ -188,12 +239,64 @@ Then the generated schema would look like this: Notice that the second field is z and not b. -Note: `@AvroName` does not add an alternative name for the field, but an override. +Note: `@SerialName` does not add an alternative name for the field, but an override. If you wish to have alternatives then you should use `@AvroAlias`. +### Overriding the namespaces for all nested records, fixed and enums in a field + +```kotlin +package com.github.avrokotlin.avro4k.example +data class Foo(@AvroNamespaceOverride("overridden") val nested: Bar) +data class Bar(val a: String) +``` +Then the generated schema would look like this: + +```json +{ + "type":"record", + "name":"Foo", + "namespace":"com.github.avrokotlin.avro4k.example", + "fields":[ + { + "name":"nested", + "type": { + "type":"record", + "name":"Bar", + "namespace":"overridden", + "fields":[ + { + "name":"a", + "type":"string" + } + ] + } + } + ] +} +``` + +Notice that the second field is z and not b. + +Note: `@SerialName` does not add an alternative name for the field, but an override. +If you wish to have alternatives then you should use `@AvroAlias`. + + + +### Change the record/enum/fixed naming strategy +To change the naming strategy for records, enums and fixed types, create your own instance of `Avro` with the wanted naming strategies. + +- `fieldNamingStrategy` is used for field names. +- `recordNamingStrategy` is used for record, enum and fixed names. + +```kotlin +Avro(AvroConfiguration( + fieldNamingStrategy = /* ... */, + recordNamingStrategy = /* ... */, +)) +``` ### Adding properties and docs to a Schema @@ -438,7 +541,6 @@ But if you want to remove a nullable field that is not optional, depending on th So to mark a field as optional and facilitate avro contract evolution regarding compatibility checks, then set `default` to `null`. - ## Types Avro4k supports the Avro logical types out of the box as well as other common JDK types. diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt index bba82cd5..0e405a7f 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt @@ -153,10 +153,15 @@ class AvroOutputStreamBuilder( } @OptIn(ExperimentalSerializationApi::class) -class Avro( - override val serializersModule: SerializersModule = defaultModule, - internal val configuration: AvroConfiguration = AvroConfiguration(), +class Avro internal constructor( + internal val configuration: AvroInternalConfiguration, + override val serializersModule: SerializersModule, ) : SerialFormat, BinaryFormat { + constructor( + serializersModule: SerializersModule = defaultModule, + configuration: AvroConfiguration = AvroConfiguration(), + ) : this(AvroInternalConfiguration(configuration), serializersModule) + constructor(configuration: AvroConfiguration) : this(defaultModule, configuration) companion object { diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/AvroConfiguration.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/AvroConfiguration.kt index 18cd7c85..8f1e6401 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/AvroConfiguration.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/AvroConfiguration.kt @@ -1,7 +1,10 @@ package com.github.avrokotlin.avro4k import com.github.avrokotlin.avro4k.schema.FieldNamingStrategy +import com.github.avrokotlin.avro4k.schema.RecordName import com.github.avrokotlin.avro4k.schema.RecordNamingStrategy +import kotlinx.serialization.descriptors.SerialDescriptor +import java.util.concurrent.ConcurrentHashMap data class AvroConfiguration( val recordNamingStrategy: RecordNamingStrategy = RecordNamingStrategy.Default, @@ -11,4 +14,44 @@ data class AvroConfiguration( * When set to [true], the nullable fields that haven't any default value are set as null if the value is missing. It also adds `"default": null` to those fields when generating schema using avro4k. */ val implicitNulls: Boolean = false, -) \ No newline at end of file + val namingCacheEnabled: Boolean = true, +) + +class AvroInternalConfiguration private constructor( + val recordNamingStrategy: RecordNamingStrategy, + val fieldNamingStrategy: FieldNamingStrategy, + val implicitNulls: Boolean, +) { + constructor(configuration: AvroConfiguration) : this( + recordNamingStrategy = configuration.recordNamingStrategy.cachedIfNecessary(configuration.namingCacheEnabled), + fieldNamingStrategy = configuration.fieldNamingStrategy.cachedIfNecessary(configuration.namingCacheEnabled), + implicitNulls = configuration.implicitNulls + ) +} + +internal fun RecordNamingStrategy.cachedIfNecessary(cacheEnabled: Boolean): RecordNamingStrategy = + object : RecordNamingStrategy { + private val cache = ConcurrentHashMap() + + override fun resolve( + descriptor: SerialDescriptor, + serialName: String, + ): RecordName = + cache.getOrPut(descriptor) { + this@cachedIfNecessary.resolve(descriptor, serialName) + } + } + +internal fun FieldNamingStrategy.cachedIfNecessary(cacheEnabled: Boolean): FieldNamingStrategy = + object : FieldNamingStrategy { + private val cache = ConcurrentHashMap, String>() + + override fun resolve( + descriptor: SerialDescriptor, + elementIndex: Int, + serialName: String, + ): String = + cache.getOrPut(descriptor to elementIndex) { + this@cachedIfNecessary.resolve(descriptor, elementIndex, serialName) + } + } \ No newline at end of file diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/ListDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/ListDecoder.kt index 7b8eef29..42bed488 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/ListDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/ListDecoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.decoder -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.descriptors.PolymorphicKind @@ -19,7 +19,7 @@ class ListDecoder( private val schema: Schema, private val array: List, override val serializersModule: SerializersModule, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, ) : AbstractDecoder(), FieldDecoder { init { require(schema.type == Schema.Type.ARRAY) @@ -85,7 +85,7 @@ class ListDecoder( return when (descriptor.kind) { StructureKind.CLASS -> RecordDecoder(descriptor, array[index] as GenericRecord, serializersModule, configuration) StructureKind.LIST -> ListDecoder(schema.elementType, array[index] as GenericArray<*>, serializersModule, configuration) - StructureKind.MAP -> MapDecoder(descriptor, schema.elementType, array[index] as Map, serializersModule, configuration) + StructureKind.MAP -> MapDecoder(schema.elementType, array[index] as Map, serializersModule, configuration) PolymorphicKind.SEALED, PolymorphicKind.OPEN -> UnionDecoder(descriptor, array[index] as GenericRecord, serializersModule, configuration) else -> throw UnsupportedOperationException("Kind ${descriptor.kind} is currently not supported.") } diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/MapDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/MapDecoder.kt index f930bbe0..4f6e7202 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/MapDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/MapDecoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.decoder -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException import kotlinx.serialization.descriptors.PolymorphicKind @@ -17,11 +17,10 @@ import java.nio.ByteBuffer @ExperimentalSerializationApi class MapDecoder( - private val desc: SerialDescriptor, private val schema: Schema, map: Map<*, *>, override val serializersModule: SerializersModule, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, ) : AbstractDecoder(), CompositeDecoder { init { require(schema.type == Schema.Type.MAP) @@ -146,7 +145,7 @@ class MapDecoder( PrimitiveKind.BYTE -> ByteArrayDecoder((value() as ByteBuffer).array(), serializersModule) else -> ListDecoder(schema.valueType, value() as GenericArray<*>, serializersModule, configuration) } - StructureKind.MAP -> MapDecoder(descriptor, schema.valueType, value() as Map, serializersModule, configuration) + StructureKind.MAP -> MapDecoder(schema.valueType, value() as Map, serializersModule, configuration) PolymorphicKind.SEALED, PolymorphicKind.OPEN -> UnionDecoder(descriptor, value() as GenericRecord, serializersModule, configuration) else -> throw UnsupportedOperationException("Kind ${descriptor.kind} is currently not supported.") } diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RecordDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RecordDecoder.kt index f3c92c30..c85ef2f4 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RecordDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RecordDecoder.kt @@ -1,7 +1,7 @@ package com.github.avrokotlin.avro4k.decoder import com.github.avrokotlin.avro4k.AnnotationExtractor -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import com.github.avrokotlin.avro4k.schema.extractNonNull import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException @@ -31,7 +31,7 @@ class RecordDecoder( private val desc: SerialDescriptor, private val record: GenericRecord, override val serializersModule: SerializersModule, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, ) : AbstractDecoder(), FieldDecoder { private var currentIndex = -1 @@ -42,7 +42,6 @@ class RecordDecoder( StructureKind.CLASS -> RecordDecoder(descriptor, value as GenericRecord, serializersModule, configuration) StructureKind.MAP -> MapDecoder( - descriptor, fieldSchema(), value as Map, serializersModule, diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RootRecordDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RootRecordDecoder.kt index 9c53b9c4..a14cce62 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RootRecordDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RootRecordDecoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.decoder -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException import kotlinx.serialization.descriptors.PolymorphicKind @@ -16,7 +16,7 @@ import org.apache.avro.generic.GenericRecord class RootRecordDecoder( private val record: GenericRecord, override val serializersModule: SerializersModule, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, ) : AbstractDecoder() { var decoded = false diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/UnionDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/UnionDecoder.kt index a8bdcb7d..2253ef96 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/UnionDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/UnionDecoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.decoder -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import com.github.avrokotlin.avro4k.possibleSerializationSubclasses import com.github.avrokotlin.avro4k.schema.RecordName import kotlinx.serialization.DeserializationStrategy @@ -18,7 +18,7 @@ class UnionDecoder( descriptor: SerialDescriptor, private val value: GenericRecord, override val serializersModule: SerializersModule, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, ) : AbstractDecoder(), FieldDecoder { private enum class DecoderState(val index: Int) { BEFORE(0), diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/ListEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/ListEncoder.kt index 3cac4e51..ef2a9b8d 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/ListEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/ListEncoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.AbstractEncoder @@ -15,7 +15,7 @@ import java.nio.ByteBuffer class ListEncoder( private val schema: Schema, override val serializersModule: SerializersModule, - override val configuration: AvroConfiguration, + override val configuration: AvroInternalConfiguration, private val callback: (GenericData.Array) -> Unit, ) : AbstractEncoder(), StructureEncoder { private val list = mutableListOf() diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/MapEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/MapEncoder.kt index c6599179..76ec1743 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/MapEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/MapEncoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException import kotlinx.serialization.descriptors.SerialDescriptor @@ -16,7 +16,7 @@ import java.nio.ByteBuffer class MapEncoder( schema: Schema, override val serializersModule: SerializersModule, - override val configuration: AvroConfiguration, + override val configuration: AvroInternalConfiguration, private val callback: (Map) -> Unit, ) : AbstractEncoder(), CompositeEncoder, diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RecordEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RecordEncoder.kt index af1e2944..f49a94d8 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RecordEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RecordEncoder.kt @@ -1,7 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AnnotationExtractor -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import com.github.avrokotlin.avro4k.ListRecord import com.github.avrokotlin.avro4k.Record import com.github.avrokotlin.avro4k.schema.extractNonNull @@ -21,7 +20,7 @@ import java.nio.ByteBuffer @ExperimentalSerializationApi interface StructureEncoder : FieldEncoder { - val configuration: AvroConfiguration + val configuration: AvroInternalConfiguration override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder { return when (descriptor.kind) { @@ -43,7 +42,7 @@ interface StructureEncoder : FieldEncoder { class RecordEncoder( private val schema: Schema, override val serializersModule: SerializersModule, - override val configuration: AvroConfiguration, + override val configuration: AvroInternalConfiguration, val callback: (Record) -> Unit, ) : AbstractEncoder(), StructureEncoder { private val builder = RecordBuilder(schema) diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RootRecordEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RootRecordEncoder.kt index 05600355..5bacb035 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RootRecordEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RootRecordEncoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import com.github.avrokotlin.avro4k.Record import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException @@ -16,7 +16,7 @@ import org.apache.avro.Schema class RootRecordEncoder( private val schema: Schema, override val serializersModule: SerializersModule, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, private val callback: (Record) -> Unit, ) : AbstractEncoder() { override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder { diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/UnionEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/UnionEncoder.kt index 4397451e..2e5908fd 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/UnionEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/UnionEncoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import com.github.avrokotlin.avro4k.Record import com.github.avrokotlin.avro4k.schema.RecordName import kotlinx.serialization.ExperimentalSerializationApi @@ -16,7 +16,7 @@ import org.apache.avro.Schema class UnionEncoder( private val unionSchema: Schema, override val serializersModule: SerializersModule, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, private val callback: (Record) -> Unit, ) : AbstractEncoder() { override fun encodeString(value: String) { diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/schema/ClassSchemaFor.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/schema/ClassSchemaFor.kt index 8770eec4..f1dd4cb0 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/schema/ClassSchemaFor.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/schema/ClassSchemaFor.kt @@ -3,7 +3,7 @@ package com.github.avrokotlin.avro4k.schema import com.github.avrokotlin.avro4k.AnnotationExtractor import com.github.avrokotlin.avro4k.Avro import com.github.avrokotlin.avro4k.AvroAlias -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import com.github.avrokotlin.avro4k.AvroJsonProp import com.github.avrokotlin.avro4k.AvroNamespaceOverride import com.github.avrokotlin.avro4k.AvroProp @@ -25,7 +25,7 @@ import org.apache.avro.SchemaBuilder @ExperimentalSerializationApi class ClassSchemaFor( private val descriptor: SerialDescriptor, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, private val serializersModule: SerializersModule, private val resolvedSchemas: MutableMap, ) : SchemaFor { diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt index 065411e7..fea888c9 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/schema/SchemaFor.kt @@ -2,9 +2,9 @@ package com.github.avrokotlin.avro4k.schema import com.github.avrokotlin.avro4k.AnnotationExtractor import com.github.avrokotlin.avro4k.Avro -import com.github.avrokotlin.avro4k.AvroConfiguration import com.github.avrokotlin.avro4k.AvroDecimalLogicalType import com.github.avrokotlin.avro4k.AvroFixed +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import com.github.avrokotlin.avro4k.AvroTimeLogicalType import com.github.avrokotlin.avro4k.AvroUuidLogicalType import com.github.avrokotlin.avro4k.LogicalDecimalTypeEnum @@ -52,7 +52,7 @@ interface SchemaFor { @ExperimentalSerializationApi class EnumSchemaFor( private val descriptor: SerialDescriptor, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, ) : SchemaFor { override fun schema(): Schema { val naming = configuration.recordNamingStrategy.resolve(descriptor, descriptor.serialName) @@ -82,7 +82,7 @@ class EnumSchemaFor( class ListSchemaFor( private val descriptor: SerialDescriptor, private val serializersModule: SerializersModule, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, private val resolvedSchemas: MutableMap, ) : SchemaFor { override fun schema(): Schema { @@ -108,7 +108,7 @@ class ListSchemaFor( class MapSchemaFor( private val descriptor: SerialDescriptor, private val serializersModule: SerializersModule, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, private val resolvedSchemas: MutableMap, ) : SchemaFor { override fun schema(): Schema { @@ -159,7 +159,7 @@ fun schemaFor( serializersModule: SerializersModule, descriptor: SerialDescriptor, annos: List, - configuration: AvroConfiguration, + configuration: AvroInternalConfiguration, resolvedSchemas: MutableMap, ): SchemaFor { val schemaFor: SchemaFor = @@ -202,7 +202,7 @@ fun schemaFor( private fun schemaForLogicalTypes( descriptor: SerialDescriptor, annos: List, - configuration: AvroConfiguration, + configuration: AvroInternalConfiguration, ): Schema? { val annotations = annos + descriptor.annotations + (if (descriptor.isInline) descriptor.unwrapValueClass.annotations else emptyList()) @@ -241,7 +241,7 @@ private fun schemaForLogicalTypes( private fun createFixedSchema( descriptor: SerialDescriptor, fixedSize: Int, - configuration: AvroConfiguration, + configuration: AvroInternalConfiguration, ): Schema { return configuration.recordNamingStrategy.resolve(descriptor, descriptor.serialName).let { SchemaBuilder.fixed(it.name).namespace(it.namespace).size(fixedSize) diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/schema/UnionSchemaFor.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/schema/UnionSchemaFor.kt index 832e723c..130635c1 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/schema/UnionSchemaFor.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/schema/UnionSchemaFor.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.schema -import com.github.avrokotlin.avro4k.AvroConfiguration +import com.github.avrokotlin.avro4k.AvroInternalConfiguration import com.github.avrokotlin.avro4k.possibleSerializationSubclasses import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.descriptors.SerialDescriptor @@ -10,7 +10,7 @@ import org.apache.avro.Schema @ExperimentalSerializationApi class UnionSchemaFor( private val descriptor: SerialDescriptor, - private val configuration: AvroConfiguration, + private val configuration: AvroInternalConfiguration, private val serializersModule: SerializersModule, private val resolvedSchemas: MutableMap, ) : SchemaFor { diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/AvroSerializationAssertThat.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/AvroSerializationAssertThat.kt index 911b9ab5..6f0f78dc 100644 --- a/src/test/kotlin/com/github/avrokotlin/avro4k/AvroSerializationAssertThat.kt +++ b/src/test/kotlin/com/github/avrokotlin/avro4k/AvroSerializationAssertThat.kt @@ -24,7 +24,7 @@ import java.nio.file.Path class AvroSerializationAssertThat(private val valueToEncode: T, private val serializer: KSerializer) { private var serializersModule: SerializersModule = Avro.default.serializersModule - private var config: AvroConfiguration = Avro.default.configuration + private var config: AvroInternalConfiguration = Avro.default.configuration private var readerSchema: Schema = avro.schema(serializer) private lateinit var writerSchema: Schema private val avro: Avro @@ -36,7 +36,7 @@ class AvroSerializationAssertThat(private val valueToEncode: T, private val s } fun withConfig(config: AvroConfiguration): AvroSerializationAssertThat { - this.config = config + this.config = AvroInternalConfiguration(config) return this } diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/schema/AvroNamespaceTest.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/schema/AvroNamespaceTest.kt index e3d1c817..c15d28d4 100644 --- a/src/test/kotlin/com/github/avrokotlin/avro4k/schema/AvroNamespaceTest.kt +++ b/src/test/kotlin/com/github/avrokotlin/avro4k/schema/AvroNamespaceTest.kt @@ -1,7 +1,6 @@ package com.github.avrokotlin.avro4k.schema import com.github.avrokotlin.avro4k.Avro -import com.github.avrokotlin.avro4k.AvroInline import com.github.avrokotlin.avro4k.AvroNamespaceOverride import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe @@ -92,17 +91,17 @@ class AvroNamespaceSchemaTest : FunSpec({ @Serializable data class NestedValueClasses(val nested: Nested1) - @AvroInline + @JvmInline @SerialName("shouldbeignored.Nested1") @Serializable - data class Nested1( + value class Nested1( @AvroNamespaceOverride("primaryspace") val s: Nested2, ) - @AvroInline + @JvmInline @SerialName("shouldbeignored.Nested2") @Serializable - data class Nested2( + value class Nested2( @AvroNamespaceOverride("shouldalsobeignored") val s: Nested3, )