All checks were successful
/ Build and Release Library (push) Successful in 2m11s
Reviewed-on: #2
32 lines
974 B
Scala
32 lines
974 B
Scala
package gs.config.v0
|
|
|
|
/** Base class for most [[Configuration]] implementations. Provides standard
|
|
* support for properly handling default values and parsing strings to the
|
|
* correct type, returning the correct errors.
|
|
*/
|
|
abstract class BaseConfiguration[F[_]] extends Configuration[F]:
|
|
|
|
protected def handleMissingValue[A](
|
|
key: ConfigKey[A]
|
|
): Either[ConfigError, A] =
|
|
key match
|
|
case ConfigKey.WithDefaultValue(_, defaultValue) =>
|
|
Right(defaultValue())
|
|
case _ =>
|
|
Left(ConfigError.MissingValue(key.name))
|
|
|
|
protected def parse[A: Configurable](
|
|
key: ConfigKey[A],
|
|
raw: String,
|
|
source: Option[String]
|
|
): Either[ConfigError, A] =
|
|
Configurable[A].parse(raw) match
|
|
case None =>
|
|
Left(
|
|
ConfigError.CannotParseValue(
|
|
configName = key.name,
|
|
candidateValue = raw,
|
|
source = source.getOrElse("")
|
|
)
|
|
)
|
|
case Some(parsed) => Right(parsed)
|