Comments (3)
from pureconfig.
This is related to #1218. For default values, generic derivation using shapeless relies on a Default
type class that exposes an HList
of the default parameter values on the type constructor. This means that default values will be computed once we derive a Default
instance. In your example, in order to derive a ConfigReader
for MiddleConf
we need a Default
instance for MiddleConf
. Once we have a Default
instance for MiddleConf
we've already eagerly evaluated the list of default values that will be used on all instances read from the derived ConfigReader
.
If you're OK with not using generic derivation for MiddleConf
you can define your own ConfigReader
for it with the logic you pretend:
import pureconfig._
import pureconfig.generic.auto._
case class InnerConf() {
lazy val value = {
val value2 = scala.util.Random.nextInt(1000000)
println(s"InnerConf has following value: $value2")
value2
}
}
case class OuterConf(middle: Map[String, MiddleConf] = Map.empty)
case class MiddleConf(inner: InnerConf = InnerConf()) {
println(s"inner-value: ${inner.value}, inner-hashcode ${inner.hashCode()}")
}
object MiddleConf {
implicit val middleConfReader: ConfigReader[MiddleConf] =
ConfigReader.forProduct1[MiddleConf, Option[InnerConf]]("inner")(_.fold(MiddleConf())(MiddleConf(_)))
}
val res = ConfigSource.string("{middle.1 {}, middle.2 {}}").load[OuterConf]
However, my recommendation would be to avoid impure logic when loading the config. For your use case that means keeping your config domain models pure and handle the generation of random key-pairs separately.
from pureconfig.
Thanks a lot for the clarifications! In my case, the easiest (backwards-compatible) solution I found is declaring inner
as optional.
So something like the following:
import pureconfig._
import pureconfig.generic.auto._
case class InnerConf() {
lazy val value = {
val value2 = scala.util.Random.nextInt(1000000)
println(s"InnerConf has following value: $value2")
value2
}
}
case class OuterConf(middle: Map[String, MiddleConf] = Map.empty)
case class MiddleConf(inner: Option[InnerConf] = None) {
val innerValue = inner.getOrElse(InnerConf()).value
}
val res = ConfigSource.string("{middle.1 {}, middle.2 {}}").load[OuterConf]
println(res.map(_.middle.values.map(_.innerValue)))
Example output:
InnerConf has following value: 18615
InnerConf has following value: 459547
Right(List(18615, 459547))
Apologies for the delayed response. From my side, this issue is resolved and it could be closed.
from pureconfig.
Related Issues (20)
- Ignore NULL values when reading maps HOT 3
- How to use Either as a config type HOT 1
- Key not found with digits in fieldname HOT 1
- Review request HOT 1
- Allow custom ConfigParseOptions
- Is it possible to define ConfigReader for "polymorphic" inputs? HOT 3
- Vulnerability from SnakeYAML CVE-2022-1471 HOT 3
- Scala 3 derivation of ConfigReader doesn't work with default values HOT 3
- Dependency issue with http4s (0.22 vs 0.23) HOT 3
- Tricky case object names HOT 1
- Cannot find an implicit instance of pureconfig.ConfigReader
- Publish Scala 3 Artifacts? HOT 1
- Create a Scala 3 release for pureconfig-circe
- Map unique keys HOT 2
- deriveReader for Scala 3 HOT 1
- Create a release to prevent `version conflict(s) in library dependencies` HOT 2
- sealed trait with families and enums HOT 2
- Usage question: Loading into case class without derive in Scala 3 HOT 4
- pureconfig-generic-scala3 derivation of ConfigReader doesn't work with default values HOT 2
- unable to compile nested case class with many fields in scala3 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pureconfig.