Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Created April 14, 2019 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasuabe/41c08c4ea5bb2f6b7be1c6925af739a3 to your computer and use it in GitHub Desktop.
Save yasuabe/41c08c4ea5bb2f6b7be1c6925af739a3 to your computer and use it in GitHub Desktop.
Ciris with type safe libraries
import ciris.{ConfigKeyType, ConfigSource}
// Coproduct
import shapeless.{:+:, CNil}
import ciris.generic._
val gType = ConfigKeyType[String]("generic key")
ConfigSource.fromEntries(gType)("key" -> "5.0")
.read("key").decodeValue[Float :+: String :+: CNil].result
// Right(Inl(5.0))
ConfigSource.fromEntries(gType)("key" -> "abc")
.read("key").decodeValue[Float :+: String :+: CNil].result
// Right(Inr(Inl(abc)))
ConfigSource.fromEntries(gType)("key" -> "123")
.read("key").decodeValue[String :+: Float :+: CNil].result
// Right(Inl(abc))
// Squants
import squants.{Energy, Temperature}
import ciris.squants._
val qType = ConfigKeyType[String]("squants key")
ConfigSource.fromEntries(qType)("___" -> "3°C")
.read("key").decodeValue[Temperature].result
// Left(ConfigErrors(MissingKey(key, ConfigKeyType(squants key))))
ConfigSource.fromEntries(qType)("key" -> "3°C")
.read("key").decodeValue[Temperature].result
// Right(3.0°C)
ConfigSource.fromEntries(qType)("key" -> "3J")
.read("key").decodeValue[Temperature].result
// Left(ConfigErrors(WrongType(key, ConfigKeyType(squants key), Right(3J), 3J, Temperature, squants.QuantityParseException: Unable to parse Temperature:3J)))
ConfigSource.fromEntries(qType)("key" -> "3J")
.read("key").decodeValue[Energy].result
// Right(3.0 J)
// Spire
import spire.math.Rational
import ciris.spire._
val sType = ConfigKeyType[String]("spire key")
ConfigSource.fromEntries(sType)("key" -> "123/456")
.read("key").decodeValue[Rational].result
// Right(41/152)
ConfigSource.fromEntries(sType)("key" -> "123/0")
.read("key").decodeValue[Rational].result
// Left(ConfigErrors(WrongType(key, ConfigKeyType(spire key), Right(123/0), 123/0, Rational, IllegalArgumentException: 0 denominator)))
// Refined
import ciris.refined._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Interval
import eu.timepit.refined.W
val rType = ConfigKeyType[String]("refined key")
type NonSystemPortNumber = Int Refined Interval.Closed[W.`1024`.T, W.`65535`.T]
ConfigSource.fromEntries(rType)("key" -> "123")
.read("key").decodeValue[NonSystemPortNumber].result
// Left(ConfigErrors(WrongType(key, ConfigKeyType(refined key), Right(123), 123, eu.timepit.refined.api.Refined[Int,eu.timepit.refined.numeric.Interval.Closed[Int(1024),Int(65535)]], Left predicate of (!(123 < 1024) && !(123 > 65535)) failed: Predicate (123 < 1024) did not fail.)))
ConfigSource.fromEntries(rType)("key" -> "8080")
.read("key").decodeValue[NonSystemPortNumber].result
// Right(8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment