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

properties with list of values #152

Merged
merged 5 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AnnotationExtractor(private val annotations: List<Annotation>) {
annotations.filterIsInstance<AvroAliases>().flatMap { it.value.toList() }
}
fun props(): List<Pair<String, String>> = annotations.filterIsInstance<AvroProp>().map { it.key to it.value }
fun json(): List<Pair<String, String>> = annotations.filterIsInstance<AvroJsonProp>().map { it.key to it.jsonValue }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun json(): List<Pair<String, String>> = annotations.filterIsInstance<AvroJsonProp>().map { it.key to it.jsonValue }
fun jsonProps(): List<Pair<String, String>> = annotations.filterIsInstance<AvroJsonProp>().map { it.key to it.jsonValue }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function renamed

fun default(): String? = annotations.filterIsInstance<AvroDefault>().firstOrNull()?.value
fun enumDefault(): String? = annotations.filterIsInstance<AvroEnumDefault>().firstOrNull()?.value
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/github/avrokotlin/avro4k/annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package com.github.avrokotlin.avro4k

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo
import org.intellij.lang.annotations.Language

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

@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
annotation class AvroJsonProp(val key: String, @Language("json") val jsonValue: String)

@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
annotation class AvroNamespace(val value: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.avrokotlin.avro4k.schema
import com.github.avrokotlin.avro4k.AnnotationExtractor
import com.github.avrokotlin.avro4k.Avro
import com.github.avrokotlin.avro4k.AvroConfiguration
import com.github.avrokotlin.avro4k.AvroJsonProp
import com.github.avrokotlin.avro4k.AvroProp
import com.github.avrokotlin.avro4k.RecordNaming
import kotlinx.serialization.ExperimentalSerializationApi
Expand Down Expand Up @@ -66,6 +67,7 @@ class ClassSchemaFor(
record.fields = fields
entityAnnotations.aliases().forEach { record.addAlias(it) }
entityAnnotations.props().forEach { (k, v) -> record.addProp(k, v) }
entityAnnotations.json().forEach { (k, v) -> record.addProp(k, json.parseToJsonElement(v).convertToAvroDefault()) }

return record
}
Expand Down Expand Up @@ -122,6 +124,9 @@ class ClassSchemaFor(
this.descriptor.getElementAnnotations(index)
.filterIsInstance<AvroProp>()
.forEach { field.addProp(it.key, it.value) }
this.descriptor.getElementAnnotations(index)
.filterIsInstance<AvroJsonProp>()
.forEach { field.addProp(it.key, json.parseToJsonElement(it.jsonValue).convertToAvroDefault()) }
annos.aliases().forEach { field.addAlias(it) }

return field
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.avrokotlin.avro4k.schema

import com.github.avrokotlin.avro4k.Avro
import com.github.avrokotlin.avro4k.AvroJsonProp
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.shouldBe
import kotlinx.serialization.Serializable

class AvroJsonPropSchemaTest : WordSpec() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some tests for object properties? Just to make sure that this is also correctly processed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean adding tests for a JSON value that is an object with properties?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ask because there's already the @AvroJsonProp should support props annotation on field test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean adding tests for a JSON value that is an object with properties?

Exactly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simply extended that test by annotating a property using a JSON object with heterogeneous property types.


enum class Colours {
Red, Green, Blue
}

init {
"@AvroJsonProp" should {
"support props annotation on class" {

val expected = org.apache.avro.Schema.Parser()
.parse(javaClass.getResourceAsStream("/props_json_annotation_class.json"))
val schema = Avro.default.schema(TypeAnnotated.serializer())
schema.toString(true) shouldBe expected.toString(true)
}
"support props annotation on field" {

val expected = org.apache.avro.Schema.Parser()
.parse(javaClass.getResourceAsStream("/props_json_annotation_field.json"))
val schema = Avro.default.schema(AnnotatedProperties.serializer())
schema.toString(true) shouldBe expected.toString(true)
}
"support props annotations on enums" {

val expected = org.apache.avro.Schema.Parser()
.parse(javaClass.getResourceAsStream("/props_json_annotation_scala_enum.json"))
val schema = Avro.default.schema(EnumAnnotated.serializer())
schema.toString(true) shouldBe expected.toString(true)
}
}
}

@Serializable
@AvroJsonProp("guns", """["and", "roses"]""")
data class TypeAnnotated(val str: String)

@Serializable
data class AnnotatedProperties(
@AvroJsonProp("guns", """["and", "roses"]""") val str: String,
@AvroJsonProp("jean", """["michel", "jarre"]""") val long: Long,
val int: Int
)

@Serializable
data class EnumAnnotated(@AvroJsonProp("guns", """["and", "roses"]""") val colours: Colours)
}
12 changes: 12 additions & 0 deletions src/test/resources/props_json_annotation_class.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "record",
"name": "TypeAnnotated",
"namespace": "com.github.avrokotlin.avro4k.schema.AvroJsonPropSchemaTest",
"fields": [
{
"name": "str",
"type": "string"
}
],
"guns": ["and", "roses"]
}
21 changes: 21 additions & 0 deletions src/test/resources/props_json_annotation_field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "record",
"name": "AnnotatedProperties",
"namespace": "com.github.avrokotlin.avro4k.schema.AvroJsonPropSchemaTest",
"fields": [
{
"name": "str",
"type": "string",
"guns": ["and", "roses"]
},
{
"name": "long",
"type": "long",
"jean": ["michel", "jarre"]
},
{
"name": "int",
"type": "int"
}
]
}
20 changes: 20 additions & 0 deletions src/test/resources/props_json_annotation_scala_enum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"type": "record",
"name": "EnumAnnotated",
"namespace": "com.github.avrokotlin.avro4k.schema.AvroJsonPropSchemaTest",
"fields": [
{
"name": "colours",
"type": {
"type": "enum",
"name": "Colours",
"symbols": [
"Red",
"Green",
"Blue"
]
},
"guns": ["and", "roses"]
}
]
}