zio / zio-config

Easily use and document any config from anywhere in ZIO apps
https://zio.dev/zio-config
Apache License 2.0
233 stars 113 forks source link

Support automatic derivation of value classes #439

Open leszekgruchala opened 4 years ago

leszekgruchala commented 4 years ago

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

remiguittaut commented 4 years ago

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 :)

afsalthaj commented 4 years ago

The solution to support value classes will be baked into existing magnolia derivation. On Mon, 9 Nov 2020 at 7:05 PM, Remi Guittaut notifications@github.com 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 commented 4 years ago

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

On Mon, 9 Nov 2020 at 6:26 PM, Leszek Gruchała notifications@github.com 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 .

soujiro32167 commented 3 years ago

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