-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Match union position order with enums and with order in 4.x (#830)
- Loading branch information
Showing
5 changed files
with
130 additions
and
44 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
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
32
avro4s-core/src/test/resources/avro_union_position_union.json
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,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" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
83 changes: 41 additions & 42 deletions
83
avro4s-core/src/test/scala/com/sksamuel/avro4s/schema/AvroSortPrioritySchemaTest.scala
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 |
---|---|---|
@@ -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 |
49 changes: 49 additions & 0 deletions
49
avro4s-core/src/test/scala/com/sksamuel/avro4s/schema/AvroUnionPositionSchemaTest.scala
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,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 | ||
} |