Skip to content

Commit

Permalink
Merge pull request #156 from Chuckame/alias-fix
Browse files Browse the repository at this point in the history
feat(aliases): Merge AvroAliases annotation to AvroAlias
  • Loading branch information
Chuckame authored Aug 30, 2023
2 parents 4b881d2 + ab44be6 commit dab6cf4
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dependencies {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.apiVersion = "1.5"
kotlinOptions.languageVersion = "1.5"
kotlinOptions.languageVersion = "1.5"
kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
}
java {
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.firstNotNullOfOrNull { it as? AvroAlias }?.value ?: emptyArray()).asList() + (annotations.firstNotNullOfOrNull {it as? AvroAliases}?.value ?: emptyArray())
fun props(): List<Pair<String, String>> = annotations.filterIsInstance<AvroProp>().map { it.key to it.value }
fun jsonProps(): List<Pair<String, String>> = annotations.filterIsInstance<AvroJsonProp>().map { it.key to it.jsonValue }
fun default(): String? = annotations.filterIsInstance<AvroDefault>().firstOrNull()?.value
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/github/avrokotlin/avro4k/annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ annotation class AvroDoc(val value: String)

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

@SerialInfo
@Deprecated(message = "Will be removed in the next major release", replaceWith = ReplaceWith("@AvroAlias(alias1, alias2)"))
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
annotation class AvroAliases(val value: Array<String>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ 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 io.kotest.matchers.shouldBe
import kotlinx.serialization.Serializable

class AvroAliasSchemaTest : WordSpec({
Expand Down Expand Up @@ -41,13 +40,13 @@ class AvroAliasSchemaTest : WordSpec({
@AvroAlias("queen")
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

0 comments on commit dab6cf4

Please sign in to comment.