julianpeeters / avrohugger

Generate Scala case class definitions from Avro schemas
Apache License 2.0
202 stars 120 forks source link

fix bug in which using either for coproduct #178

Closed LeonPoon closed 1 year ago

LeonPoon commented 1 year ago

Fixed it as such:

 import shapeless.{:+:, CNil, Coproduct}

 final case class UnionOfTwoNonNullTypes(var r4: Int :+: String :+: CNil) extends org.apache.avro.specific.SpecificRecordBase {
-  def this() = this(Left(0))
+  def this() = this(shapeless.Coproduct[int :+: string :+: CNil](0))
   def get(field$: Int): AnyRef = {
     (field$: @switch) match {
       case 0 => {
LeonPoon commented 1 year ago

Added another fix where type names are wrong in Coproduct[...](...):

--- a/avrohugger-core/src/test/expected/specific/OptionalShapelessCoproduct/com/example/avrohugger/unions_with_coproduct_avsc2/UnionOfTwoNonNullTypes.scala
+++ b/avrohugger-core/src/test/expected/specific/OptionalShapelessCoproduct/com/example/avrohugger/unions_with_coproduct_avsc2/UnionOfTwoNonNullTypes.scala
@@ -6,7 +6,7 @@ import scala.annotation.switch
 import shapeless.{:+:, CNil, Coproduct}

 final case class UnionOfTwoNonNullTypes(var r4: Int :+: String :+: CNil) extends org.apache.avro.specific.SpecificRecordBase {
-  def this() = this(shapeless.Coproduct[int :+: string :+: CNil](0))
+  def this() = this(shapeless.Coproduct[Int :+: String :+: CNil](0))
   def get(field$: Int): AnyRef = {
     (field$: @switch) match {
       case 0 => {
diff --git a/avrohugger-core/src/test/expected/specific/com/example/avrohugger/unions_with_coproduct_avsc/s.scala b/avrohugger-core/src/test/expected/specific/com/example/avrohugger/unions_with_coproduct_avsc/s.scala
index 651fb1b..0ea503b 100644
--- a/avrohugger-core/src/test/expected/specific/com/example/avrohugger/unions_with_coproduct_avsc/s.scala
+++ b/avrohugger-core/src/test/expected/specific/com/example/avrohugger/unions_with_coproduct_avsc/s.scala
@@ -6,7 +6,7 @@ import scala.annotation.switch
 import shapeless.{:+:, CNil, Coproduct}

 final case class s(var s: String, var ns: Option[String], var multi2: Either[com.example.avrohugger.unions_with_coproduct_avsc.r1, com.example.avrohugger.unions_with_coproduct_avsc.r2], var multi2opt: Option[Either[com.example.avrohugger.unions_with_coproduct_avsc.r1, com.example.avrohugger.unions_with_coproduct_avsc.r2]], var multi3: com.example.avrohugger.unions_with_coproduct_avsc.r3 :+: com.example.avrohugger.unions_with_coproduct_avsc.r1 :+: com.example.avrohugger.unions_with_coproduct_avsc.r2 :+: CNil, var multi3opt: Option[com.example.avrohugger.unions_with_coproduct_avsc.r3 :+: com.example.avrohugger.unions_with_coproduct_avsc.r1 :+: com.example.avrohugger.unions_with_coproduct_avsc.r2 :+: CNil]) extends org.apache.avro.specific.SpecificRecordBase {
-  def this() = this("", None, Left(new r1), None, shapeless.Coproduct[r3 :+: r1 :+: r2 :+: CNil](new r3), None)
+  def this() = this("", None, Left(new r1), None, shapeless.Coproduct[com.example.avrohugger.unions_with_coproduct_avsc.r3 :+: com.example.avrohugger.unions_with_coproduct_avsc.r1 :+: com.example.avrohugger.unions_with_coproduct_avsc.r2 :+: CNil](new r3), None)
   def get(field$: Int): AnyRef = {
     (field$: @switch) match {
       case 0 => {
julianpeeters commented 1 year ago

awesome, thanks again for the attention to this. avrohugger 1.5.4 and sbt-avrohugger 2.5.4 are released and should be syncing up with maven central soon.