Skip to content

Commit

Permalink
feat!: Merge AvroJsonProp to AvroProp #201
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuckame committed May 3, 2024
1 parent a6b4817 commit bb5b035
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 @@ -48,8 +48,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 @@ -10,7 +10,6 @@ import com.github.avrokotlin.avro4k.AvroDefault
import com.github.avrokotlin.avro4k.AvroDoc
import com.github.avrokotlin.avro4k.AvroEnumDefault
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 @@ -57,15 +56,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 @@ -127,14 +124,12 @@ internal data class SimpleAnnotatedLocation(
*/
internal data class TypeAnnotations(
val props: Sequence<AvroProp>,
val jsonProps: Sequence<AvroJsonProp>,
val aliases: AvroAlias?,
val doc: AvroDoc?,
val enumDefault: AvroEnumDefault?,
) {
constructor(descriptor: SerialDescriptor) : this(
descriptor.findAnnotations<AvroProp>().asSequence(),
descriptor.findAnnotations<AvroJsonProp>().asSequence(),
descriptor.findAnnotation<AvroAlias>(),
descriptor.findAnnotation<AvroDoc>(),
descriptor.findAnnotation<AvroEnumDefault>()
Expand Down Expand Up @@ -163,12 +158,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,14 +29,13 @@ class AvroPropsSchemaTest : StringSpec({
"f": ["bar", 404, false, null, {}]
}"""
)
@AvroProp("cold", "play")
val field: EnumAnnotated,
)

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

0 comments on commit bb5b035

Please sign in to comment.