Skip to content

Commit

Permalink
Match union position order with enums and with order in 4.x (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkorchik authored May 25, 2024
1 parent 5c97409 commit 0370be4
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ object SubtypeOrdering extends Ordering[SealedTrait.Subtype[_, _, _]] {
val priorityA = annosA.sortPriority.getOrElse(999999F)
val priorityB = annosB.sortPriority.getOrElse(999999F)

if (priorityA == priorityB) namesA.fullName.compare(namesB.fullName) else priorityA.compare(priorityB)
if (priorityA == priorityB) namesA.fullName.compare(namesB.fullName) else priorityB.compare(priorityA)
}
}
}
6 changes: 6 additions & 0 deletions avro4s-core/src/test/resources/avro_union_position_enum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "enum",
"name": "Numeric",
"namespace": "com.sksamuel.avro4s.schema.AvroUnionPositionSchemaTestContext",
"symbols": ["NaturalNumber", "RationalNumber", "RealNumber"]
}
32 changes: 32 additions & 0 deletions avro4s-core/src/test/resources/avro_union_position_union.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"type": "record",
"name": "FightingStyleWrapper",
"namespace": "com.sksamuel.avro4s.schema.AvroUnionPositionSchemaTestContext",
"fields": [
{
"name": "fightingstyle",
"type": [
{
"type": "record",
"name": "DefensiveFightingStyle",
"fields": [
{
"name": "has_armor",
"type": "boolean"
}
]
},
{
"type": "record",
"name": "AggressiveFightingStyle",
"fields": [
{
"name": "agressiveness",
"type": "float"
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
//package com.sksamuel.avro4s.schema
//
//import com.sksamuel.avro4s.{AvroSchema, AvroSortPriority}
//
//import org.scalatest.funsuite.AnyFunSuite
//import org.scalatest.matchers.should.Matchers
//
//class AvroSortPrioritySchemaTest extends AnyFunSuite with Matchers {
//
// test("enums should be sorted by descending priority") {
// val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/avro_sort_priority_enum.json"))
// val schema = AvroSchema[Numeric]
// schema.toString(true) shouldBe expected.toString(true)
// }
//
// test("unions should be sorted by descending priority") {
// val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/avro_sort_priority_union.json"))
// val schema = AvroSchema[FightingStyleWrapper]
//
// schema.toString(true) shouldBe expected.toString(true)
// }
//
package com.sksamuel.avro4s.schema

import com.sksamuel.avro4s.{AvroSchema, AvroSortPriority}

import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

class AvroSortPrioritySchemaTest extends AnyFunSuite with Matchers {

test("enums should be sorted by descending priority") {
val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/avro_sort_priority_enum.json"))
val schema = AvroSchema[Numeric]
schema.toString(true) shouldBe expected.toString(true)
}

test("unions should be sorted by descending priority") {
val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/avro_sort_priority_union.json"))
val schema = AvroSchema[FightingStyleWrapper]

schema.toString(true) shouldBe expected.toString(true)
}

// test("avrosortpriority should respect union default ordering") {
// val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/avro_sort_priority_union_with_default.json"))
// val schema = AvroSchema[FightingStyleWrapperWithDefault]
//
// schema.toString(true) shouldBe expected.toString(true)
// }
//}
//
//
//sealed trait Numeric
//@AvroSortPriority(1)
//case object RationalNumber extends Numeric
//@AvroSortPriority(0)
//case object RealNumber extends Numeric
//@AvroSortPriority(2)
//case object NaturalNumber extends Numeric
//
//
//case class FightingStyleWrapper(fightingstyle: FightingStyle)
}


sealed trait Numeric
@AvroSortPriority(1)
case object RationalNumber extends Numeric
@AvroSortPriority(0)
case object RealNumber extends Numeric
@AvroSortPriority(2)
case object NaturalNumber extends Numeric


case class FightingStyleWrapper(fightingstyle: FightingStyle)
//case class FightingStyleWrapperWithDefault(fightingstyle: FightingStyle = AggressiveFightingStyle(10))
//
//sealed trait FightingStyle
//@AvroSortPriority(2)
//case class AggressiveFightingStyle(agressiveness: Float) extends FightingStyle
//@AvroSortPriority(10)
//case class DefensiveFightingStyle(has_armor: Boolean) extends FightingStyle
//

sealed trait FightingStyle
@AvroSortPriority(2)
case class AggressiveFightingStyle(agressiveness: Float) extends FightingStyle
@AvroSortPriority(10)
case class DefensiveFightingStyle(has_armor: Boolean) extends FightingStyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.sksamuel.avro4s.schema

import com.sksamuel.avro4s.{AvroSchema, AvroUnionPosition}

import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

class AvroUnionPositionSchemaTest extends AnyFunSuite with Matchers with AvroUnionPositionSchemaTestContext {

test("enums should be sorted by ascending union position") {
val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/avro_union_position_enum.json"))
val schema = AvroSchema[Numeric]
schema.toString(true) shouldBe expected.toString(true)
}

test("unions should be sorted by ascending union position") {
val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/avro_union_position_union.json"))
val schema = AvroSchema[FightingStyleWrapper]

schema.toString(true) shouldBe expected.toString(true)
}

// test("avrosortpriority should respect union default ordering") {
// val expected = new org.apache.avro.Schema.Parser().parse(getClass.getResourceAsStream("/avro_sort_priority_union_with_default.json"))
// val schema = AvroSchema[FightingStyleWrapperWithDefault]
//
// schema.toString(true) shouldBe expected.toString(true)
// }
}

trait AvroUnionPositionSchemaTestContext {

sealed trait Numeric
@AvroUnionPosition(2)
case object RationalNumber extends Numeric
@AvroUnionPosition(3)
case object RealNumber extends Numeric
@AvroUnionPosition(1)
case object NaturalNumber extends Numeric

case class FightingStyleWrapper(fightingstyle: FightingStyle)
// case class FightingStyleWrapperWithDefault(fightingstyle: FightingStyle = AggressiveFightingStyle(10))

sealed trait FightingStyle
@AvroUnionPosition(2)
case class AggressiveFightingStyle(agressiveness: Float) extends FightingStyle
@AvroUnionPosition(1)
case class DefensiveFightingStyle(has_armor: Boolean) extends FightingStyle
}

0 comments on commit 0370be4

Please sign in to comment.