Skip to content

Commit

Permalink
Merge pull request #204 from Chuckame/unify-avro-json-prop
Browse files Browse the repository at this point in the history
feat!: Merge AvroJsonProp to AvroProp
  • Loading branch information
Chuckame authored May 3, 2024
2 parents 091f36c + bb5b035 commit 325bd3f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 34 deletions.
16 changes: 3 additions & 13 deletions src/main/kotlin/com/github/avrokotlin/avro4k/annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,16 @@ annotation class AvroNamespaceOverride(
)

/**
* Adds a property to the Avro schema or field.
* Adds a property to the Avro schema or field. Its value could be any valid JSON or just a string.
*
* Ignored in inline classes.
*/
@SerialInfo
@Repeatable
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
annotation class AvroProp(val key: String, val value: String)

/**
* Adds a json property to the Avro schema or field.
*
* Ignored in inline classes.
*/
@SerialInfo
@Repeatable
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
annotation class AvroJsonProp(
annotation class AvroProp(
val key: String,
@Language("JSON") val jsonValue: String,
@Language("JSON") val value: String,
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ internal class ClassVisitor(
false
)
annotations.aliases?.value?.forEach { schema.addAlias(it) }
annotations.props.forEach { schema.addProp(it.key, it.value) }
annotations.jsonProps.forEach { schema.addProp(it.key, it.jsonNode) }
annotations.props.forEach { schema.addProp(it.key, it.jsonNode) }
schema
}
this.schemaAlreadyResolved = schemaAlreadyResolved
Expand Down Expand Up @@ -105,8 +104,7 @@ internal class ClassVisitor(
fieldDefault
)
annotations.aliases.flatMap { it.value.asSequence() }.forEach { field.addAlias(it) }
annotations.props.forEach { field.addProp(it.key, it.value) }
annotations.jsonProps.forEach { field.addProp(it.key, it.jsonNode) }
annotations.props.forEach { field.addProp(it.key, it.jsonNode) }
return field
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ internal class ValueVisitor internal constructor(
.symbols(*descriptor.elementNamesArray)

annotations.aliases?.value?.forEach { schema.addAlias(it) }
annotations.props.forEach { schema.addProp(it.key, it.value) }
annotations.jsonProps.forEach { schema.addProp(it.key, it.jsonNode) }
annotations.props.forEach { schema.addProp(it.key, it.jsonNode) }

setSchema(schema)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.github.avrokotlin.avro4k.AvroAlias
import com.github.avrokotlin.avro4k.AvroDefault
import com.github.avrokotlin.avro4k.AvroDoc
import com.github.avrokotlin.avro4k.AvroFixed
import com.github.avrokotlin.avro4k.AvroJsonProp
import com.github.avrokotlin.avro4k.AvroLogicalType
import com.github.avrokotlin.avro4k.AvroNamespaceOverride
import com.github.avrokotlin.avro4k.AvroProp
Expand Down Expand Up @@ -56,15 +55,13 @@ internal data class InlineClassFieldAnnotations(
*/
internal data class FieldAnnotations(
val props: Sequence<AvroProp>,
val jsonProps: Sequence<AvroJsonProp>,
val aliases: Sequence<AvroAlias>,
val doc: AvroDoc?,
val default: AvroDefault?,
val namespaceOverride: AvroNamespaceOverride?,
) {
constructor(descriptor: SerialDescriptor, elementIndex: Int) : this(
descriptor.findElementAnnotations<AvroProp>(elementIndex).asSequence(),
descriptor.findElementAnnotations<AvroJsonProp>(elementIndex).asSequence(),
descriptor.findElementAnnotations<AvroAlias>(elementIndex).asSequence(),
descriptor.findElementAnnotation<AvroDoc>(elementIndex),
descriptor.findElementAnnotation<AvroDefault>(elementIndex),
Expand Down Expand Up @@ -126,13 +123,11 @@ internal data class SimpleAnnotatedLocation(
*/
internal data class TypeAnnotations(
val props: Sequence<AvroProp>,
val jsonProps: Sequence<AvroJsonProp>,
val aliases: AvroAlias?,
val doc: AvroDoc?,
) {
constructor(descriptor: SerialDescriptor) : this(
descriptor.findAnnotations<AvroProp>().asSequence(),
descriptor.findAnnotations<AvroJsonProp>().asSequence(),
descriptor.findAnnotation<AvroAlias>(),
descriptor.findAnnotation<AvroDoc>()
) {
Expand All @@ -156,12 +151,12 @@ internal fun ValueAnnotations?.appendAnnotations(other: ValueAnnotations) =

private val objectMapper = ObjectMapper()

internal val AvroJsonProp.jsonNode: JsonNode
internal val AvroProp.jsonNode: JsonNode
get() {
if (jsonValue.isStartingAsJson()) {
return objectMapper.readTree(jsonValue)
if (value.isStartingAsJson()) {
return objectMapper.readTree(value)
}
return TextNode.valueOf(jsonValue)
return TextNode.valueOf(value)
}

internal val AvroDefault.jsonValue: Any
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.AvroAssertions
import com.github.avrokotlin.avro4k.AvroEnumDefault
import com.github.avrokotlin.avro4k.AvroJsonProp
import com.github.avrokotlin.avro4k.AvroProp
import io.kotest.core.spec.style.StringSpec
import kotlinx.serialization.Serializable
Expand All @@ -16,11 +15,12 @@ class AvroPropsSchemaTest : StringSpec({
}) {
@Serializable
@AvroProp("hey", "there")
@AvroJsonProp("counting", """["one", "two"]""")
@AvroProp("counting", """["one", "two"]""")
private data class TypeAnnotated(
@AvroJsonProp(
@AvroProp("cold", "play")
@AvroProp(
key = "complexObject",
jsonValue = """{
value = """{
"a": "foo",
"b": 200,
"c": true,
Expand All @@ -29,13 +29,12 @@ class AvroPropsSchemaTest : StringSpec({
"f": ["bar", 404, false, null, {}]
}"""
)
@AvroProp("cold", "play")
val field: EnumAnnotated,
)

@Serializable
@AvroProp("enums", "power")
@AvroJsonProp("countingAgain", """["three", "four"]""")
@AvroProp("countingAgain", """["three", "four"]""")
private enum class EnumAnnotated {
Red,

Expand Down

0 comments on commit 325bd3f

Please sign in to comment.