Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change orElse and orNone to work on missing keys #124

Merged
merged 3 commits into from
Mar 26, 2018
Merged

Conversation

vlovgr
Copy link
Owner

@vlovgr vlovgr commented Mar 26, 2018

  • Add ConfigError#isMissingKey for checking whether an error occurred because the key was missing from the ConfigSource (for combined errors, whether all errors are due to missing keys).
  • Change ConfigValue#orElse and orNone to only use the alternative value (or None) if the error is because of missing keys, as determined by ConfigError#isMissingKey.

For example, previously, a type conversion error for PORT in the following example:

> echo $PORT
changeme

scala> env[Int]("PORT").
     |   orElse(prop[Int]("port").
     |   orNone
res0: ConfigValue[Id, Option[Int]] = ConfigValue(Right(None))

would result in using the fallback values. This is probably not what you would expect, and this pull request changes orElse and orNone to only use fallback values if all previous errors are because of missing keys. In this example, we would stop at the environment variable PORT, because it was set but not of the expected type.

scala> env[Int]("PORT").
     |   orElse(prop[Int]("port").
     |   orNone
res0: ConfigValue[Id, Option[Int]] = ConfigValue(Left(WrongType(PORT, Environment, Right(changeme), changeme, Int, java.lang.NumberFormatException: For input string: "changeme")))

scala> res0.value.left.map(_.message).toString
res1: String = Left(Environment variable [PORT] with value [changeme] cannot be converted to type [Int]: java.lang.NumberFormatException: For input string: "changeme")

@codecov
Copy link

codecov bot commented Mar 26, 2018

Codecov Report

Merging #124 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #124      +/-   ##
==========================================
+ Coverage   95.34%   95.37%   +0.03%     
==========================================
  Files          43       43              
  Lines         687      692       +5     
  Branches        4        2       -2     
==========================================
+ Hits          655      660       +5     
  Misses         32       32
Impacted Files Coverage Δ
...core/shared/src/main/scala/ciris/ConfigError.scala 100% <100%> (ø) ⬆️
...core/shared/src/main/scala/ciris/ConfigValue.scala 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6931311...0b124e0. Read the comment docs.

@vlovgr vlovgr merged commit c659747 into master Mar 26, 2018
@vlovgr vlovgr deleted the or-else-none branch March 26, 2018 13:05
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