Skip to content

Commit

Permalink
feat(aliases)!: Merge AvroAliases annotation to AvroAlias
Browse files Browse the repository at this point in the history
- Now, to have multiple aliases, just annotate like `@AvroAlias("alias1", "alias2", "alias3")` as it is now a vararg parameter
- updated to kotlin 1.9 to have @repeatable working
  • Loading branch information
Chuckame committed Aug 25, 2023
1 parent 3d64d54 commit db6b108
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 21 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ dependencies {

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.apiVersion = "1.5"
kotlinOptions.languageVersion = "1.5"
kotlinOptions.apiVersion = "1.9"
kotlinOptions.languageVersion = "1.9"
kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
}
java {
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Libs.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object Libs {

const val kotlinVersion = "1.8.20"
const val kotlinVersion = "1.9.10"
const val dokkaVersion = "1.8.10"
const val kotestGradlePlugin = "0.4.10"
const val versionsPlugin = "0.46.0"
Expand All @@ -15,7 +15,7 @@ object Libs {
}

object Kotlinx {
private const val version = "1.5.0"
private const val version = "1.6.0"
const val serializationCore = "org.jetbrains.kotlinx:kotlinx-serialization-core:$version"
const val serializationJson = "org.jetbrains.kotlinx:kotlinx-serialization-json:$version"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.avrokotlin.avro4k

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor

@ExperimentalSerializationApi
class AnnotationExtractor(private val annotations: List<Annotation>) {

Expand All @@ -19,12 +20,7 @@ class AnnotationExtractor(private val annotations: List<Annotation>) {
fun name(): String? = annotations.filterIsInstance<AvroName>().firstOrNull()?.value
fun valueType(): Boolean = annotations.filterIsInstance<AvroInline>().isNotEmpty()
fun doc(): String? = annotations.filterIsInstance<AvroDoc>().firstOrNull()?.value
fun aliases(): List<String> =
if(annotations.any { it is AvroAlias }){
annotations.filterIsInstance<AvroAlias>().map { it.value }
}else{
annotations.filterIsInstance<AvroAliases>().flatMap { it.value.toList() }
}
fun aliases(): List<String> = annotations.filterIsInstance<AvroAlias>().flatMap { it.value.asList() }
fun props(): List<Pair<String, String>> = annotations.filterIsInstance<AvroProp>().map { it.key to it.value }
fun default(): String? = annotations.filterIsInstance<AvroDefault>().firstOrNull()?.value
fun enumDefault(): String? = annotations.filterIsInstance<AvroEnumDefault>().firstOrNull()?.value
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/com/github/avrokotlin/avro4k/annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ annotation class AvroInline
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
annotation class AvroDoc(val value: String)

@Repeatable
@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
annotation class AvroAlias(val value: String)

@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
annotation class AvroAliases(val value: Array<String>)
annotation class AvroAlias(vararg val value: String)

/**
* [AvroFixed] overrides the schema type for a field or a value class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.avrokotlin.avro4k.schema

import com.github.avrokotlin.avro4k.Avro
import com.github.avrokotlin.avro4k.AvroAlias
import com.github.avrokotlin.avro4k.AvroAliases
import io.kotest.matchers.shouldBe
import io.kotest.core.spec.style.WordSpec
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -39,15 +38,16 @@ class AvroAliasSchemaTest : WordSpec({
}) {
@Serializable
@AvroAlias("queen")
@AvroAlias("ledzep")
data class TypeAnnotated(val str: String)

@AvroAliases(["queen","ledzep"])
@AvroAlias("queen", "ledzep")
@Serializable
data class TypeAliasAnnotated(val str: String)

@Serializable
data class FieldAnnotated(@AvroAlias("cold") val str: String, @AvroAlias("kate") val long: Long, val int: Int)

@Serializable
data class FieldAliasAnnotated(@AvroAliases(["queen","ledzep"]) val str: String)
data class FieldAliasAnnotated(@AvroAlias("queen", "ledzep") val str: String)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.avrokotlin.avro4k.schema

import com.github.avrokotlin.avro4k.Avro
import com.github.avrokotlin.avro4k.AvroAliases
import com.github.avrokotlin.avro4k.AvroAlias
import com.github.avrokotlin.avro4k.AvroDefault
import com.github.avrokotlin.avro4k.AvroDoc
import com.github.avrokotlin.avro4k.AvroEnumDefault
Expand Down Expand Up @@ -89,7 +89,7 @@ enum class Wine {
}

@Serializable
@AvroAliases(["MySuit"])
@AvroAlias("MySuit")
@AvroDoc("documentation")
enum class Suit {
SPADES, HEARTS, DIAMONDS, CLUBS;
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/aliases_on_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"name": "TypeAnnotated",
"namespace": "com.github.avrokotlin.avro4k.schema.AvroAliasSchemaTest",
"aliases": [
"queen"
"queen",
"ledzep"
],
"fields": [
{
Expand Down

0 comments on commit db6b108

Please sign in to comment.