zio-config icon indicating copy to clipboard operation
zio-config copied to clipboard

Support automatic derivation of value classes

Open leszekgruchala opened this issue 4 years ago • 4 comments

The main goal is to have automatic support for config descriptors that are value classes.

import zio.config.magnolia.DeriveConfigDescriptor.Descriptor

case class Port(value: Int) extends AnyVal
implicit val portDescriptor: Descriptor[Port] = Descriptor[Int].transform(Port, _.value)

Sample failing code https://scastie.scala-lang.org/9UDHZDYDRtmqiYybxGQwaA

leszekgruchala avatar Nov 09 '20 07:11 leszekgruchala

with credits from https://discord.com/channels/629491597070827530/633028431000502273/775153057965015050.

import cats.evidence.As
import shapeless.ops.hlist.IsHCons
import shapeless.{Generic, HList, HNil}
import zio.config.magnolia.DeriveConfigDescriptor.Descriptor

implicit def getDescriptorForValueClasses[A, T <: HList, H](
    implicit I: A As AnyVal,
    G: Generic.Aux[A, T],
    IsH : IsHCons.Aux[T, H, HNil],
    CD: Descriptor[H]
 ): Descriptor[A] = CD.transform(value => G.to(value), t => G.from(t))

should work, but it would be preferable to avoid bringing in the shapeless dependency. a macro, with the inspiration from io.circe.generic.extras.deriveUnwrappedCodec would be great :)

remiguittaut avatar Nov 09 '20 08:11 remiguittaut

The solution to support value classes will be baked into existing magnolia derivation. On Mon, 9 Nov 2020 at 7:05 PM, Remi Guittaut [email protected] wrote:

with credits from https://discord.com/channels/629491597070827530/633028431000502273/775153057965015050 .

import cats.evidence.As

import shapeless.ops.hlist.IsHCons

import shapeless.{Generic, HList, HNil}

import zio.config.magnolia.DeriveConfigDescriptor.Descriptor

implicit def getDescriptorForValueClasses[A, T <: HList, H](

implicit I: A As AnyVal,

G: Generic.Aux[A, T],

IsH : IsHCons.Aux[T, H, HNil],

CD: Descriptor[H]

): Descriptor[A] = CD.transform(value => G.to(value), t => G.from(t))

should work, but it would be preferable to avoid bringing in the shapeless dependency. a macro, with the inspiration from

io.circe.generic.extras.deriveUnwrappedCodec would be great :)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zio/zio-config/issues/439#issuecomment-723840384, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABY2QJMYCRD3IQ66TJHYJ2DSO6PFBANCNFSM4TO6VWWQ .

afsalthaj avatar Nov 09 '20 08:11 afsalthaj

Thanks for reporting this issue. I will take a look at it

On Mon, 9 Nov 2020 at 6:26 PM, Leszek Gruchała [email protected] wrote:

The main goal is to have automatic support for config descriptors that are value classes.

import zio.config.magnolia.DeriveConfigDescriptor.Descriptor

case class Port(value: Int) extends AnyVal

implicit val portDescriptor: Descriptor[Port] = Descriptor[Int].transform(Port, _.value)

Sample failing code https://scastie.scala-lang.org/9UDHZDYDRtmqiYybxGQwaA

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zio/zio-config/issues/439, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABY2QJKHTTBMWZBA4PA7GRTSO6KSHANCNFSM4TO6VWWQ .

afsalthaj avatar Nov 09 '20 08:11 afsalthaj

Looks like this was fixed in Magnolia: https://github.com/softwaremill/magnolia/issues/225 I'd love this as a feature, otherwise I have tons of Descriptor[Int].transform(Port, _.value) litter around

soujiro32167 avatar Nov 05 '21 12:11 soujiro32167