Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (55 sloc) 2.34 KB
package org.scalax.asuna.circe
import io.circe.Encoder
import io.circe.syntax._
import org.scalax.asuna.implements.circe.encoder.CirceHelper
import org.scalax.asuna.mapper.common.{SingleName, SingleRepContent}
import org.scalax.asuna.mapper.decoder.EmptyLazyModel
object Test03 extends App with CirceHelper {
def field(name: String): SingleName = SingleName.name(name)
def fieldWithEncoder[T](name: String, encoder: Encoder[T]): SingleRepContent[Encoder[T], T] = new SingleRepContent[Encoder[T], T] {
override val rep = encoder
override val singleModelName = name
}
case class Foo(i1: String, i2: Int, i3: String, IIII4: Long, i6: Int, IIII7: Int)
case class Bar(i1: String, i2: Int, i3: String, i4: Long, i6: String, i7: String)
object FooTable {
val IIII4 = field("i4")
val i6 = Encoder.encodeString.contramap { value: Int =>
value.toString
}
val IIII7 = fieldWithEncoder("i7", i6)
}
val foo = Foo(i1 = "i1", i2 = 2, i3 = "i3", IIII4 = 4L, i6 = 6, IIII7 = 7)
val bar = Bar(i1 = "i1", i2 = 2, i3 = "i3", i4 = 4L, i6 = "6", i7 = "7")
implicit val fooJson = valEncoder.effect(valEncoder.singleModel[Foo](FooTable).compile).encoder
implicit val barJson = {
import io.circe.generic.auto._
implicitly[Encoder[Bar]]
}
assert(foo.asJson == bar.asJson)
println(s"result1: ${foo.asJson.noSpaces}")
println(s"equals1: ${foo.asJson == bar.asJson}")
object EmptyTable
case class CacheTest[T](model: T, success: Int, message: String)
case class Input[T](model: T)
trait CacheTestWrap {
type T
def unused: Input[T] = Input(model.model)
val model: CacheTest[T]
val input = EmptyLazyModel.value
}
type AAux[T1] = CacheTestWrap { type T = T1 }
def wrap[T1](model1: CacheTest[T1]): AAux[T1] = new CacheTestWrap {
override type T = T1
override val model: CacheTest[T1] = model1
}
val cacheWrapJsonPre = valEncoder.effect(valEncoder.unusedModel[CacheTestWrap](EmptyTable).compile)
implicit def cacheWrapJson[T1](implicit e1: Encoder[T1]): Encoder[CacheTest[T1]] = {
defEncoder.effect(defEncoder.singleModel[Input[T1]](FooTable).compile).zip(cacheWrapJsonPre).encoder.contramapObject { m: CacheTest[T1] =>
val temp = wrap(m)
(temp.unused, temp)
}
}
val fooWrapJsonModel = CacheTest(foo, 2, "It's OK.").asJson
println(fooWrapJsonModel.noSpaces)
}