Created
June 9, 2018 23:24
-
-
Save muradm/110b7c38711d704ed134f29ce54fd134 to your computer and use it in GitHub Desktop.
Using AvroType
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
type COPArg = Long :+: String :+: Int :+: CNil | |
case class NonADT(name: String, surname: String) | |
object NonADT { | |
implicit val nonAdt: AvroType[NonADT] = record2("NonADT")(NonADT.apply)( | |
"name" -> member(string, _.name), | |
"surname" -> member(string, _.surname) | |
) | |
} | |
sealed trait ADT extends Product with Serializable | |
case class ADTCase1(arg: Int) extends ADT | |
case class ADTCase2(arg: String) extends ADT | |
case object ADTCase3 extends ADT | |
object ADT { | |
implicit val avrc1: AvroType[ADTCase1] = record1("ADTCase1", Some("ADT"))(ADTCase1.apply)("arg" -> member(int, _.arg)) | |
implicit val avrc2: AvroType[ADTCase2] = record1("ADTCase2", Some("ADT"))(ADTCase2.apply)("arg" -> member(string, _.arg)) | |
implicit val avrc3: AvroType[ADTCase3.type] = record0("ADTCase3", Some("ADT"))(ADTCase3) | |
} | |
case class TestInner1(fixed: Array[Byte], bin: List[String], map: java.util.Map[String, String], adt: ADT) | |
object TestInner1 { | |
implicit val avro: AvroType[TestInner1] = record4("TestInner1", Some("test"))(TestInner1.apply)( | |
"fixed" -> member(fixed(5, "tag"), _.fixed), | |
"bin" -> member(array(string), _.bin, withDefault(List("default"))), | |
"map" -> member(map(string).imap(_.asJava)(_.asScala.toMap), _.map), | |
"adt" -> member(union[ADT], _.adt) | |
) | |
} | |
case class Basic(arg: Int, argInt: Option[COPArg], inner: Option[TestInner1], adtOpt: Option[ADT]) | |
object Basic { | |
implicit val avro: AvroType[Basic] = record4("Basic", Some("test"))(Basic.apply)( | |
"arg" -> member(string.imap(_.toInt)(_.toString), _.arg), | |
"argInt" -> member(option(union(string | int | long)), _.argInt, withDefault(Some(Coproduct[COPArg](5)))), | |
"inner" -> member(option(TestInner1.avro), _.inner, withDefault(None)), | |
"adtOpt" -> member(option(union[ADT]), _.adtOpt, withDefault(None)) | |
) | |
} | |
case class TestInner2(argLong: Long) | |
object TestInner2 { | |
implicit val avro: AvroType[TestInner2] = record1("TestInner1", Some("test"))(TestInner2.apply)( | |
"argLong" -> member(long, _.argLong) | |
) | |
} | |
case class Basic1(arg: String, argInt: Int, inner: TestInner2) | |
object Basic1 { | |
implicit val avro: AvroType[Basic1] = record3("Basic", Some("test"))(Basic1.apply)( | |
"arg" -> member(string, _.arg), | |
"argInt" -> member(int, _.argInt), | |
"inner" -> member(TestInner2.avro, _.inner) | |
) | |
} | |
val target = Basic(2, Some(Coproduct[COPArg](5)), | |
Some(TestInner1("hello1".getBytes, List("hello", "hello2"), Map("m1" -> "v1", "m2" -> "v2").asJava | |
, ADTCase3 | |
)), | |
Some(ADTCase2("hel")) | |
) | |
val targetRec = AvroEncoder[Basic].encode(schema[Basic], target).asInstanceOf[GenericRecord] | |
val r = (encode(target) match { | |
case Right(res) => res | |
case Left(ex) => ex.printStackTrace(); System.exit(0) | |
}).asInstanceOf[Array[Byte]] | |
println(r) | |
val b = decode[Basic](r) | |
println(b) | |
b match { | |
case Right(r1) => println(r1) | |
case Left(e) => e.printStackTrace() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment