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

Match Union's sort order with enum's and with sort order in 4.x #830

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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
}
Loading