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(aliases): Merge AvroAliases annotation to AvroAlias #156

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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