ccadllc / cedi-config Goto Github PK
View Code? Open in Web Editor NEWProvides a safe API for parsing settings objects out of Typesafe Config.
License: Apache License 2.0
Provides a safe API for parsing settings objects out of Typesafe Config.
License: Apache License 2.0
So, I'm trying to manually parse sealed trait
families. I'd like to avoid the automatic derivation.
Here's how it looks like now:
sealed trait Timeframe
object Timeframe {
case class Absolute(from: Long, to: Long) extends Timeframe
case class Relative(since: String) extends Timeframe
}
val timeframeParser: ConfigParser[Timeframe] =
string("type").bind {
case "absolute" => (long("from") :: long("to")).as[Timeframe.Absolute]
case "relative" => string("since").map(Timeframe.Relative)
case otherwise =>
anonymous(_ => Left(ConfigErrors.of(ConfigError.BadValue(ConfigKey.Relative("type"), otherwise, None))))
}
I'm pretty happy with it, apart from the error case for the discriminator. It'd be really helpful if the library provided a ConfigParser.failure
method (A cats.MonadError
instance would be even better :-)).
Hi-
At present, this library does not seem to support full derivation of nested case classes: i.e. - given the following set of case classes
final case class Both(left: Wibble, right: That)
final case class Wibble(value: Int)
final case class Wobble(value: List[String])
then simply calling derived[Both]
without explicitly defining implicit values for both derived[Wibble]
and derived[Wobble]
, will yield compilation errors indicating that a ConfigParser needs to be in scope for each component type x of Both.
Would you welcome a PR supporting the ability to fully auto-derive nested case classes?
Thanks
Alex
I have an ADT as given below:
abstract sealed class Location(val name: String)
object Location {
final case object AustraliaEast extends Location("australiaeast")
def fromString(name: String): Location = name match {
case AustraliaEast.name => AustraliaEast
}
}
I derive the ConfigParser for Location by specifying the DerivedConfigFieldParser for location.
implicit val locDerivedConfigFieldParser: DerivedConfigFieldParser[Location] =
implicitly[DerivedConfigFieldParser[String]].map(Location.fromString)
Is there already a better way of doing it?
If not, it would be more intuitive to define just a ConfigParser[Location] (using the ConfigParser[String]). Something along the below lines.
implicit val configParser: ConfigParser[Location] =
ConfigParser[String].map(Location.fromString)
Looking at the code, it feels we could bring this behavior.
Enhance API be adding support for NonEmptyList and NonEmptyVector:
An example of code would be:
def nonEmptyList[A](key: String)(cpl: String => ConfigParser[List[A]]): ConfigParser[NonEmptyList[A]] = cpl(key) bind { case head :: tail => ConfigParser.pure(NonEmptyList(head, tail)) case Nil => ConfigParser(s"non-empty-list failure for $key") { _ => Left(ConfigErrors.of(ConfigError.WrongType(ConfigKey.Relative(key), "expected non-empty list", None))) } }
and
def nonEmptyVector[A](key: String)(cpv: String => ConfigParser[Vector[A]]): ConfigParser[NonEmptyVector[A]] = cpv(key) bind { case Vector() => ConfigParser(s"non-empty-vector failure for $key") { _ => Left(ConfigErrors.of(ConfigError.WrongType(ConfigKey.Relative(key), "expected non-empty vector", None))) } case v => ConfigParser.pure(NonEmptyVector(v.head, v.tail)) }
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.