-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use zio.Config.Switch for discriminated sum types #1167
Conversation
Fixed Scala 3 derivation, ignoring failing |
ConfigDocs.DynamicMap( | ||
loop(descriptions, c, latestPath, alreadySeen), | ||
map.map { case (k, v) => | ||
k.toString -> loop(descriptions, v, latestPath, alreadySeen) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In most cases k
will be String
, but I wonder if there could be a better support for polymorphic key type of Config.Switch[A, B]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's going to be hard at DynamicMap level in zio core, as this key gets propagated all over.
@@ -9,7 +9,7 @@ import java.time.{LocalDate, LocalDateTime, LocalTime} | |||
import java.util.UUID | |||
import scala.collection.immutable | |||
|
|||
final case class DeriveConfig[T](desc: Config[T], isObject: Boolean = false) { | |||
final case class DeriveConfig[T](desc: Config[T], isObject: Boolean = false, constValue: Option[T] = None) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary to support Config.Switch
, but I wonder if there is a better way, or at least replace isObject
with constValue.isDefined
.
|
||
object Metadata { | ||
final case class Object(name: ProductName) extends Metadata | ||
final case class Object[T](name: ProductName, constValue: T) extends Metadata |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this T
look okay?
@@ -210,7 +210,7 @@ object MarkdownSpec extends BaseSpec { | |||
|
|||
assert(result)(equalTo(expectedMarkdown)) | |||
} | |||
) | |||
) @@ TestAspect.ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, what's the purpose of this spec which exists only in Scala 3?
sealed trait Credentials | ||
|
||
case class Password(value: String) extends Credentials | ||
|
||
case class Token(value: String) extends Credentials | ||
|
||
case object InstanceProfile extends Credentials |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to preventing match errors, this PR enables this specific use case - discriminated sealed traits having case objects as children.
case config: FallbackWith[B] => FallbackWith(loop(config.first), loop(config.second), config.f) | ||
case config: Fallback[B] => Fallback(loop(config.first), loop(config.second)) | ||
case Sequence(config) => Sequence(loop(config)) | ||
case Switch(config, map) => Switch(config, map.map { case (k, v) => k -> loop(v) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, I wonder if Config#mapKey
extension method is still necessary, because now I found it more idiomatic to adjust the keys using ConfigProvider
. (e.g. ConfigProvider#kebabCase
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we will keep it for now.
@@ -343,8 +344,7 @@ trait ConfigSyntax { | |||
// override def flatten: IndexedFlat = indexedFlat | |||
// } | |||
|
|||
|
|||
//Disabled until next version of ZIO: https://github.com/zio/zio-config/blob/avoid_custom_index_until_3/README.md#indexed-map-array-datatype-and-a-some-implementation-notes | |||
// Disabled until next version of ZIO: https://github.com/zio/zio-config/blob/avoid_custom_index_until_3/README.md#indexed-map-array-datatype-and-a-some-implementation-notes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the change was unrelated, but this link appears to be dead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the whole function is obsolete as ConfigProvider.fromMap
is builtin now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe it's still relevant. The section seems to be moved to here:
https://github.com/zio/zio-config/blob/v4.0.0-RC15/docs/read-from-various-sources.md#indexed-map-array-datatype-and-a-some-implementation-notes
Could you resolve conflicts? May be that will fix the format changes, so we could focus only the specific changes. |
@afsalthaj Resolved conflicts. |
@@ -26,13 +26,17 @@ object AutomaticConfigTest extends ZIOSpecDefault { | |||
) | |||
} | |||
|
|||
object AutomaticConfigTestUtils { | |||
object AutomaticConfigSpecUtils { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For API consistency, I'd like to make this spec shared between Scala 2 and 3. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, something that was in my radar as well. Agree.
* as `keyName`. | ||
* | ||
* Example: | ||
* {{{ | ||
* @nameWithLabel("type") | ||
* @discriminator("type") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍
Thanks for the contribution. |
zio.Config.Switch
for discriminated sum typeszio.Config.Switch
match error in docs and syntaxConfig[Either[A, B]]
derivation in Scala 3