Revert "Fix various Macro issues for 2.12"
This reverts commit 683828f133
.
This commit is contained in:
Родитель
993f0f55f2
Коммит
3c41075b43
|
@ -1,59 +0,0 @@
|
|||
package com.sksamuel.avro4s
|
||||
|
||||
import org.scalatest.{WordSpec, Matchers}
|
||||
|
||||
class GithubIssue187And152 extends WordSpec with Matchers {
|
||||
import GithubIssue187And152._
|
||||
|
||||
"SchemaFor" should {
|
||||
"work for a simple ADT" in {
|
||||
SchemaFor[Product1]
|
||||
}
|
||||
}
|
||||
"Encoding and decoding a nested ADT" should {
|
||||
"work" in {
|
||||
val data = Product1(5, Product1.Coproduct1.Coproduct1Summand2(7, Product1.Coproduct1.Coproduct1Summand2.Coproduct2.One))
|
||||
import java.io.File
|
||||
val file: File = new File("product1.avro")
|
||||
val os = AvroOutputStream.data[Product1](file)
|
||||
os.write(data)
|
||||
os.close
|
||||
val is = AvroInputStream.data[Product1](file)
|
||||
val decoded = is.iterator.toList
|
||||
|
||||
decoded shouldEqual List(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object GithubIssue187And152 {
|
||||
final case class Product1(i: Int, j: Product1.Coproduct1)
|
||||
|
||||
object Product1 {
|
||||
sealed trait Coproduct1
|
||||
|
||||
object Coproduct1 {
|
||||
final case class Coproduct1Summand2(i: Int, j: Coproduct1Summand2.Coproduct2) extends Coproduct1
|
||||
|
||||
object Coproduct1Summand2 {
|
||||
sealed trait Coproduct2
|
||||
|
||||
object Coproduct2 {
|
||||
final case object One extends Coproduct2
|
||||
final case object Two extends Coproduct2
|
||||
final case class Three(s: String) extends Coproduct2
|
||||
final case class Four() extends Coproduct2
|
||||
}
|
||||
}
|
||||
case object Coproduct1Summand1 extends Coproduct1
|
||||
case class Coproduct1Summand3() extends Coproduct1
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
sealed trait Foo
|
||||
case class Bar(foo: Option[String]) extends Foo
|
||||
case class Baz(foo: String) extends Foo
|
||||
|
||||
|
||||
}
|
|
@ -40,6 +40,5 @@ class MacrosTest extends WordSpec with Matchers {
|
|||
|
||||
pepperoni2 shouldBe pepperoni
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,8 @@ import java.time.{LocalDate, LocalDateTime}
|
|||
import java.util.UUID
|
||||
|
||||
import com.sksamuel.avro4s.ToSchema.defaultScaleAndPrecisionAndRoundingMode
|
||||
import org.apache.avro.generic.{GenericData, GenericRecord}
|
||||
import org.apache.avro.Schema.Field
|
||||
import org.apache.avro.generic.{GenericData, GenericRecord}
|
||||
import org.apache.avro.util.Utf8
|
||||
import org.apache.avro.{Conversions, LogicalTypes}
|
||||
import shapeless.ops.coproduct.Reify
|
||||
|
@ -27,12 +27,12 @@ trait FromValue[T] {
|
|||
trait LowPriorityFromValue {
|
||||
|
||||
implicit def genCoproduct[T, C <: Coproduct](implicit gen: Generic.Aux[T, C],
|
||||
fromCoproduct: Lazy[FromValue[C]]): FromValue[T] = new FromValue[T] {
|
||||
fromCoproduct: FromValue[C]): FromValue[T] = new FromValue[T] {
|
||||
override def apply(value: Any, field: Field): T =
|
||||
gen.from(fromCoproduct.value(value, field))
|
||||
gen.from(fromCoproduct(value, field))
|
||||
}
|
||||
|
||||
implicit def applyUsingMacro[T](implicit fromRecord: FromRecord[T]): FromValue[T] = new FromValue[T] {
|
||||
implicit def apply[T](implicit fromRecord: FromRecord[T]): FromValue[T] = new FromValue[T] {
|
||||
override def apply(value: Any, field: Field): T = value match {
|
||||
case record: GenericRecord => fromRecord(record)
|
||||
}
|
||||
|
@ -172,11 +172,11 @@ object FromValue extends LowPriorityFromValue {
|
|||
|
||||
import scala.reflect.runtime.universe.WeakTypeTag
|
||||
|
||||
private def safeFrom[T: WeakTypeTag](value: Any)(implicit fromValue: Lazy[FromValue[T]]): Option[T] = {
|
||||
private def safeFrom[T: WeakTypeTag : FromValue](value: Any): Option[T] = {
|
||||
import scala.reflect.runtime.universe.typeOf
|
||||
|
||||
val tpe = implicitly[WeakTypeTag[T]].tpe
|
||||
val from = fromValue.value
|
||||
val from = implicitly[FromValue[T]]
|
||||
|
||||
def typeName: String = {
|
||||
val nearestPackage = Stream.iterate(tpe.typeSymbol.owner)(_.owner).dropWhile(!_.isPackage).head
|
||||
|
@ -251,10 +251,10 @@ object FromValue extends LowPriorityFromValue {
|
|||
// rest of the coproduct type T.
|
||||
|
||||
// thus, the bulk of the logic here is shared with reading Eithers, in `safeFrom`.
|
||||
implicit def CoproductFromValue[S: WeakTypeTag, T <: Coproduct](implicit fromValueS: FromValue[S], fromValueT: FromValue[T]): FromValue[S :+: T] = new FromValue[S :+: T] {
|
||||
implicit def CoproductFromValue[S: WeakTypeTag : FromValue, T <: Coproduct : FromValue]: FromValue[S :+: T] = new FromValue[S :+: T] {
|
||||
override def apply(value: Any, field: Field): S :+: T =
|
||||
safeFrom[S](value).map(Coproduct[S :+: T](_))
|
||||
.getOrElse(Inr(fromValueT(value, field)))
|
||||
.getOrElse(Inr(implicitly[FromValue[T]].apply(value, field)))
|
||||
}
|
||||
|
||||
implicit def genTraitObjectEnum[T, C <: Coproduct, L <: HList](implicit gen: Generic.Aux[T, C],
|
||||
|
@ -343,19 +343,15 @@ object FromRecord {
|
|||
}
|
||||
}
|
||||
|
||||
def fromRecord(t: Tree) =
|
||||
c.Expr[FromRecord[T]](
|
||||
q"""new _root_.com.sksamuel.avro4s.FromRecord[$tpe] {
|
||||
private val converters: Array[_root_.shapeless.Lazy[_root_.com.sksamuel.avro4s.FromValue[_]]] = Array(..$converters)
|
||||
def apply(record: _root_.org.apache.avro.generic.GenericRecord): $tpe = {$t}
|
||||
c.Expr[FromRecord[T]](
|
||||
q"""new _root_.com.sksamuel.avro4s.FromRecord[$tpe] {
|
||||
private val converters: Array[_root_.shapeless.Lazy[_root_.com.sksamuel.avro4s.FromValue[_]]] = Array(..$converters)
|
||||
def apply(record: _root_.org.apache.avro.generic.GenericRecord): $tpe = {
|
||||
$companion.apply(..$fromValues)
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
if(fromValues.isEmpty && companion == NoSymbol)
|
||||
fromRecord(q"${tpe.termSymbol}")
|
||||
else
|
||||
fromRecord(q"$companion.apply(..$fromValues)")
|
||||
}
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
def lazyConverter[T](implicit fromValue: Lazy[FromValue[T]]): Lazy[FromValue[T]] = fromValue
|
||||
|
|
|
@ -26,11 +26,11 @@ trait ToSchema[T] {
|
|||
trait LowPriorityToSchema {
|
||||
|
||||
implicit def genCoproduct[T, C <: Coproduct](implicit gen: Generic.Aux[T, C],
|
||||
coproductSchema: Lazy[ToSchema[C]]): ToSchema[T] = new ToSchema[T] {
|
||||
protected val schema: Schema = coproductSchema.value()
|
||||
coproductSchema: ToSchema[C]): ToSchema[T] = new ToSchema[T] {
|
||||
protected val schema: Schema = coproductSchema()
|
||||
}
|
||||
|
||||
implicit def applyUsingMacro[T: Manifest](implicit schemaFor: SchemaFor[T]): ToSchema[T] = new ToSchema[T] {
|
||||
implicit def apply[T: Manifest](implicit schemaFor: SchemaFor[T]): ToSchema[T] = new ToSchema[T] {
|
||||
protected val schema = schemaFor()
|
||||
}
|
||||
}
|
||||
|
@ -192,13 +192,13 @@ object ToSchema extends LowPriorityToSchema {
|
|||
// Shapeless's implementation builds up the type recursively,
|
||||
// (i.e., it's actually A :+: (B :+: (C :+: CNil)))
|
||||
// so here we define the schema for the base case of the recursion, C :+: CNil
|
||||
implicit def CoproductBaseSchema[S](implicit subschema: Lazy[ToSchema[S]]): ToSchema[S :+: CNil] = new ToSchema[S :+: CNil] {
|
||||
protected val schema = createUnion(subschema.value())
|
||||
implicit def CoproductBaseSchema[S](implicit subschema: ToSchema[S]): ToSchema[S :+: CNil] = new ToSchema[S :+: CNil] {
|
||||
protected val schema = createUnion(subschema())
|
||||
}
|
||||
|
||||
// And here we continue the recursion up.
|
||||
implicit def CoproductSchema[S, T <: Coproduct](implicit subschema: Lazy[ToSchema[S]], coproductSchema: Lazy[ToSchema[T]]): ToSchema[S :+: T] = new ToSchema[S :+: T] {
|
||||
protected val schema = createUnion(subschema.value(), coproductSchema.value())
|
||||
implicit def CoproductSchema[S, T <: Coproduct](implicit subschema: ToSchema[S], coproductSchema: ToSchema[T]): ToSchema[S :+: T] = new ToSchema[S :+: T] {
|
||||
protected val schema = createUnion(subschema(), coproductSchema())
|
||||
}
|
||||
|
||||
// This ToSchema is used for sealed traits of objects
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package com.sksamuel.avro4s
|
||||
|
||||
import java.nio.ByteBuffer
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.{LocalDate, LocalDateTime}
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.UUID
|
||||
|
||||
import com.sksamuel.avro4s.ToSchema.defaultScaleAndPrecisionAndRoundingMode
|
||||
|
@ -22,11 +22,11 @@ trait ToValue[A] {
|
|||
trait LowPriorityToValue {
|
||||
|
||||
implicit def genCoproduct[T, C <: Coproduct](implicit gen: Generic.Aux[T, C],
|
||||
coproductToValue: Lazy[ToValue[C]]): ToValue[T] = new ToValue[T] {
|
||||
override def apply(value: T): Any = coproductToValue.value(gen.to(value))
|
||||
coproductToValue: ToValue[C]): ToValue[T] = new ToValue[T] {
|
||||
override def apply(value: T): Any = coproductToValue(gen.to(value))
|
||||
}
|
||||
|
||||
implicit def applyUsingMacro[T](implicit toRecord: ToRecord[T]): ToValue[T] = new ToValue[T] {
|
||||
implicit def apply[T](implicit toRecord: ToRecord[T]): ToValue[T] = new ToValue[T] {
|
||||
override def apply(value: T): GenericRecord = toRecord(value)
|
||||
}
|
||||
|
||||
|
@ -167,10 +167,10 @@ object ToValue extends LowPriorityToValue {
|
|||
}
|
||||
|
||||
// A :+: B is either Inl(value: A) or Inr(value: B), continuing the recursion
|
||||
implicit def CoproductToValue[S, T <: Coproduct](implicit curToValue: Lazy[ToValue[S]], restToValue: Lazy[ToValue[T]]): ToValue[S :+: T] = new ToValue[S :+: T] {
|
||||
implicit def CoproductToValue[S, T <: Coproduct](implicit curToValue: ToValue[S], restToValue: ToValue[T]): ToValue[S :+: T] = new ToValue[S :+: T] {
|
||||
override def apply(value: S :+: T): Any = value match {
|
||||
case Inl(s) => curToValue.value(s)
|
||||
case Inr(t) => restToValue.value(t)
|
||||
case Inl(s) => curToValue(s)
|
||||
case Inr(t) => restToValue(t)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,11 +6,11 @@ publishArtifact := false
|
|||
publish := {}
|
||||
|
||||
val `avro4s-macros` = project.in(file("avro4s-macros"))
|
||||
.settings(libraryDependencies += "com.chuusai" %% "shapeless" % "2.3.3")
|
||||
.settings(libraryDependencies += "com.chuusai" %% "shapeless" % "2.3.2")
|
||||
|
||||
val `avro4s-core` = project.in(file("avro4s-core"))
|
||||
.dependsOn(`avro4s-macros`)
|
||||
|
||||
val `avro4s-json` = project.in(file("avro4s-json"))
|
||||
.dependsOn(`avro4s-core`)
|
||||
.settings(libraryDependencies += "org.json4s" %% "json4s-native" % "3.5.3")
|
||||
.settings(libraryDependencies += "org.json4s" %% "json4s-native" % "3.5.3")
|
|
@ -1,8 +1,8 @@
|
|||
import com.typesafe.sbt.pgp.PgpKeys
|
||||
import com.typesafe.sbt.SbtPgp
|
||||
import sbt.Keys._
|
||||
import sbt.{Global, _}
|
||||
import sbt.Keys._
|
||||
import sbtrelease.ReleasePlugin
|
||||
import com.typesafe.sbt.pgp.PgpKeys
|
||||
|
||||
/** Adds common settings automatically to all subprojects */
|
||||
object GlobalPlugin extends AutoPlugin {
|
||||
|
@ -12,7 +12,7 @@ object GlobalPlugin extends AutoPlugin {
|
|||
val AvroVersion = "1.8.2"
|
||||
val Log4jVersion = "1.2.17"
|
||||
val ScalatestVersion = "3.0.1"
|
||||
val ScalaVersion = "2.12.4"
|
||||
val ScalaVersion = "2.11.12"
|
||||
val Slf4jVersion = "1.7.12"
|
||||
|
||||
override def requires = ReleasePlugin
|
||||
|
@ -20,7 +20,7 @@ object GlobalPlugin extends AutoPlugin {
|
|||
override def projectSettings = publishingSettings ++ Seq(
|
||||
organization := org,
|
||||
scalaVersion := ScalaVersion,
|
||||
crossScalaVersions := Seq("2.11.12", "2.12.5"),
|
||||
crossScalaVersions := Seq("2.11.12", "2.12.4"),
|
||||
resolvers += Resolver.mavenLocal,
|
||||
parallelExecution in Test := false,
|
||||
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8", "-Ywarn-unused-import",
|
||||
|
@ -74,4 +74,4 @@ object GlobalPlugin extends AutoPlugin {
|
|||
</developers>
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
|
@ -2,4 +2,4 @@ resolvers += Classpaths.sbtPluginReleases
|
|||
|
||||
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.0")
|
||||
|
||||
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.7")
|
||||
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.7")
|
Загрузка…
Ссылка в новой задаче