Skip to content

Commit

Permalink
Add ConfigDef.getOrParse{,OrThrow}
Browse files Browse the repository at this point in the history
Closes #125
  • Loading branch information
japgolly committed Sep 9, 2022
1 parent 53cdb08 commit d5aa755
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 3.1.0

* Add `ConfigDef.getOrParse{,OrThrow}`. They're the same as `getOrUse` except the default value is parsed instead of provided.
* In `ConfigDef` instances...
* Deprecate `.option` and add `.whenAtLeastOneKeySpecified` as a replacement
* Add `.whenFullySpecified`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,22 @@ object ConfigDef {
case None => StepResult.Success(None, Set.empty)
})

def getOrParse[A](key: String, default: String)(implicit p: ValueParser[A]): Either[String, ConfigDef[A]] =
p.parse(default).map(getOrUse[A](key, _, default))

def getOrParseOrThrow[A: ValueParser](key: String, default: String): ConfigDef[A] =
getOrParse[A](key, default) match {
case Right(d) => d
case Left(err) =>
val msg = s"Invalid default '$default' for '$key' config. $err"
throw new IllegalArgumentException(msg)
}

def getOrUse[A: ValueParser](key: String, default: A): ConfigDef[A] =
Instance.baseGetA[A, A](key, ApiMethod.GetOrUse("" + default), (_, o) => o match {
getOrUse[A](key, default, "" + default)

private def getOrUse[A: ValueParser](key: String, default: A, displayStr: String): ConfigDef[A] =
Instance.baseGetA[A, A](key, ApiMethod.GetOrUse(displayStr), (_, o) => o match {
case Some((origin, a)) => StepResult.Success(a, Set(origin))
case None => StepResult.Success(default, Set.empty)
})
Expand Down

0 comments on commit d5aa755

Please sign in to comment.