-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from avro-kotlin/polymorphic
Adding support for sealed interfaces and mixed polymorphic class structures
- Loading branch information
Showing
15 changed files
with
522 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/test/kotlin/com/github/avrokotlin/avro4k/schema/MixedPolymorphicSchemaTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.github.avrokotlin.avro4k.schema | ||
|
||
import com.github.avrokotlin.avro4k.Avro | ||
import com.github.avrokotlin.avro4k.AvroDefault | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.polymorphic | ||
import kotlinx.serialization.modules.subclass | ||
import org.apache.avro.Schema | ||
|
||
sealed interface Expr | ||
sealed interface UnaryExpr : Expr { | ||
val value : Int | ||
} | ||
@Serializable | ||
sealed class BinaryExpr : Expr { | ||
abstract val left: Int | ||
abstract val right: Int | ||
} | ||
@Serializable | ||
object NullaryExpr : Expr | ||
@Serializable | ||
data class NegateExpr(override val value : Int) : UnaryExpr | ||
@Serializable | ||
data class AddExpr(override val left : Int, override val right : Int) : BinaryExpr() | ||
@Serializable | ||
data class SubstractExpr(override val left : Int, override val right : Int) : BinaryExpr() | ||
@Serializable | ||
abstract class OtherBinaryExpr : BinaryExpr() | ||
@Serializable | ||
data class MultiplicationExpr(override val left : Int, override val right : Int) : OtherBinaryExpr() | ||
@Serializable | ||
abstract class OtherUnaryExpr : UnaryExpr | ||
@Serializable | ||
data class ConstExpr(override val value : Int) : OtherUnaryExpr() | ||
|
||
@Serializable | ||
data class ReferencingMixedPolymorphic( | ||
val notNullable: Expr | ||
) | ||
@Serializable | ||
data class ReferencingNullableMixedPolymorphic( | ||
@AvroDefault(Avro.NULL) | ||
val nullable : Expr? | ||
) | ||
class MixedPolymorphicSchemaTest : StringSpec({ | ||
val module = SerializersModule { | ||
polymorphic(OtherUnaryExpr::class) { | ||
subclass(ConstExpr::class) | ||
} | ||
polymorphic(OtherBinaryExpr::class) { | ||
subclass(MultiplicationExpr::class) | ||
} | ||
} | ||
"referencing a mixed sealed hierarchy"{ | ||
val schema = Avro(module).schema(ReferencingMixedPolymorphic.serializer()) | ||
val expected = Schema.Parser().parse(javaClass.getResourceAsStream("/mixed_polymorphic_referenced.json")) | ||
schema shouldBe expected | ||
} | ||
"referencing a mixed nullable sealed hierarchy"{ | ||
val schema = Avro(module).schema(ReferencingNullableMixedPolymorphic.serializer()) | ||
val expected = Schema.Parser().parse(javaClass.getResourceAsStream("/mixed_polymorphic_nullable_referenced.json")) | ||
schema shouldBe expected | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/kotlin/com/github/avrokotlin/avro4k/schema/SealedInterfaceSchemaTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.github.avrokotlin.avro4k.schema | ||
|
||
import com.github.avrokotlin.avro4k.Avro | ||
import com.github.avrokotlin.avro4k.AvroDefault | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.serialization.Serializable | ||
import org.apache.avro.Schema | ||
|
||
sealed interface Calculable | ||
sealed interface UnaryCalculable : Calculable { | ||
val value : Int | ||
} | ||
sealed interface BinaryCalculable : Calculable { | ||
val left: Int | ||
val right: Int | ||
} | ||
@Serializable | ||
object NullaryCalculable : Calculable | ||
@Serializable | ||
data class NegateCalculable(override val value : Int) : UnaryCalculable | ||
@Serializable | ||
data class AddCalculable(override val left : Int, override val right : Int) : BinaryCalculable | ||
@Serializable | ||
data class SubstractCalculable(override val left : Int, override val right : Int) : BinaryCalculable | ||
|
||
|
||
@Serializable | ||
data class ReferencingSealedInterface( | ||
val notNullable: Calculable | ||
) | ||
@Serializable | ||
data class ReferencingNullableSealedInterface( | ||
@AvroDefault(Avro.NULL) | ||
val nullable : Calculable? | ||
) | ||
|
||
class SealedInterfaceSchemaTest : StringSpec({ | ||
|
||
"referencing a sealed hierarchy"{ | ||
val schema = Avro.default.schema(ReferencingSealedInterface.serializer()) | ||
val expected = Schema.Parser().parse(javaClass.getResourceAsStream("/sealed_interface_referenced.json")) | ||
schema shouldBe expected | ||
} | ||
"referencing a nullable sealed hierarchy"{ | ||
val schema = Avro.default.schema(ReferencingNullableSealedInterface.serializer()) | ||
val expected = Schema.Parser().parse(javaClass.getResourceAsStream("/sealed_interface_nullable_referenced.json")) | ||
schema shouldBe expected | ||
} | ||
}) |
Oops, something went wrong.