Last active
December 5, 2019 23:23
-
-
Save pomadchin/6c74c588c1d3fa56637edb56fba4e87c to your computer and use it in GitHub Desktop.
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
package com.azavea.scube | |
import com.azavea.scube.Test.WidgetId | |
import io.circe.syntax._ | |
import io.circe.{Decoder, Encoder} | |
import io.circe.generic.extras.Configuration | |
import shapeless.newtype | |
import shapeless.newtype.Newtype | |
import shapeless._ | |
case class WidgetIdOps(toInt: Int) | |
object WidgetIdOps { | |
implicit def newtypeCoercion(i: Int): WidgetIdOps = WidgetIdOps(i) | |
implicit def newtypeCoercion2(i: Int): WidgetId = WidgetId(i) | |
} | |
object Test2 { | |
trait Coercible[A, B] { | |
final def apply(a: A): B = a.asInstanceOf[B] | |
} | |
object Coercible { | |
implicit def newTypeToCoercible[R, O]: Coercible[R, Newtype[R, O]] = new Coercible[R, Newtype[R, O]] { } | |
implicit def newTypeToCoercibleK[F[_], R: Coercible[*, O], O]: Coercible[F[R], F[O]] = new Coercible[F[R], F[O]] { } | |
} | |
implicit val customConfig: Configuration = Configuration.default.withDiscriminator("classType") | |
type WidgetId = Newtype[Int, WidgetIdOps] | |
def WidgetId(i: Int): WidgetId = newtype(i) | |
implicit def coercibleEncoderWidgetIt[R, N](implicit ev: Coercible[Encoder[R], Encoder[N]], R: Encoder[R]): Encoder[N] = ev(R) | |
implicit def coercibleDecoderWidgetIt[R, N](implicit ev: Coercible[Decoder[R], Decoder[N]], R: Decoder[R]): Decoder[N] = ev(R) | |
WidgetId(2).asJson | |
} |
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
package com.azavea.scube | |
import io.circe.syntax._ | |
import io.circe.{Decoder, Encoder} | |
import io.circe.generic.extras.Configuration | |
import shapeless.newtype | |
import shapeless.newtype.Newtype | |
case class WidgetIdOps(toInt: Int) | |
object WidgetIdOps { | |
implicit def newtypeCoercion(i: Int): WidgetIdOps = WidgetIdOps(i) | |
} | |
object Test2 { | |
trait Coercible[A, B] { | |
@inline final def apply(a: A): B = a.asInstanceOf[B] | |
} | |
implicit def coericbleF[F[_]] = new Coercible[F[Int], F[WidgetId]] { } | |
implicit val customConfig: Configuration = Configuration.default.withDiscriminator("classType") | |
type WidgetId = Newtype[Int, WidgetIdOps] | |
def WidgetId(i: Int): WidgetId = newtype(i) | |
implicit def coercibleEncoderWidgetIt[R, N](implicit ev: Coercible[Encoder[R], Encoder[N]], R: Encoder[R]): Encoder[N] = ev(R) | |
implicit def coercibleDecoderWidgetIt[R, N](implicit ev: Coercible[Decoder[R], Decoder[N]], R: Decoder[R]): Decoder[N] = ev(R) | |
WidgetId(2).asJson | |
} |
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
import io.circe.{Decoder, Encoder} | |
import io.circe.syntax._ | |
import io.circe.generic.extras.{Configuration, ConfiguredJsonCodec} | |
import io.estatico.newtype.Coercible | |
import io.estatico.newtype.macros.newtype | |
object Test { | |
implicit val customConfig: Configuration = Configuration.default.withDiscriminator("classType") | |
implicit def coercibleEncoderWidgetIt[R, N](implicit ev: Coercible[Encoder[R], Encoder[N]], R: Encoder[R]): Encoder[N] = ev(R) | |
implicit def coercibleDecoderWidgetIt[R, N](implicit ev: Coercible[Decoder[R], Decoder[N]], R: Decoder[R]): Decoder[N] = ev(R) | |
@newtype | |
@ConfiguredJsonCodec | |
case class WidgetId(toInt: Int) | |
object WidgetId | |
WidgetId(2).asJson | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment