Skip to content

improve zio config support for Map where key is a String WrappedType#413

Open
loathwine wants to merge 1 commit intokitlangton:mainfrom
loathwine:zio-config-map-support
Open

improve zio config support for Map where key is a String WrappedType#413
loathwine wants to merge 1 commit intokitlangton:mainfrom
loathwine:zio-config-map-support

Conversation

@loathwine
Copy link

Summary

Adds DeriveConfig instances for Map[B, V] where B is a newtype or subtype wrapping String.

Problem

Previously, if you had a newtype key for a Map in your config, derivation would fail:

type UserId = UserId.Type
object UserId extends Newtype[String]

case class Permissions(userAccess: Map[UserId, String])

// This would fail to derive:
// deriveConfig[Permissions]

Even though DeriveConfig[Map[String, V]] exists, there was no way to derive DeriveConfig[Map[UserId, V]].

Solution

Two new given instances in ZioConfigInstances:

  • validatedMap — for validated newtypes/subtypes, applies validation to each map key
  • simpleMap — for simple (unvalidated) newtypes/subtypes, wraps each map key directly

These also compose for stacked types like Newtype[ValidatedSubtype].

@loathwine
Copy link
Author

Related zio-config#1381
I wonder if the fix should be in zio-config instead. I'll take a look if it is easy to add there.

@loathwine
Copy link
Author

Created PR zio-config#1658. It is arguably a better fix. Then neotype would just need to derive ConfigKeyDecoder in the usual straight forward way. Not sure if/when it will be approved and released though.

Should I close this PR or leave it open?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant