Skip to content

Commit

Permalink
Fix and make the example in the README testable
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas committed Feb 27, 2017
1 parent 66e2d19 commit e529214
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
49 changes: 23 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,31 @@ libraryDependencies += "io.github.jonas" %% "circe-config" % "0.1.1"
## Example

```scala
import com.typesafe.config.ConfigFactory
import io.circe.generic.auto._
import io.github.jonas.circe.config.syntax._

case class ServerSettings(host: String, port: Int, ssl: Option[String])
case class HttpSettings(server: ServerSettings, version: Double)
case class AppSettings(http: HttpSettings)

val config = ConfigFactory.parseString("""
http {
version = 1.1
server {
host = localhost
port = 8080
}
}
""")

val serverSettings = config.as[ServerSettings]("http.server")
// => serverSettings: Either[io.circe.Error,ServerSettings] = Right(ServerSettings(localhost,8080,None))

val httpSettings = config.as[ServerSettings]("http")
// => httpSettings: Either[io.circe.Error,HttpSettings] = Right(HttpSettings(ServerSettings(localhost,8080,None),1.1))

val appSettings = config.as[AppSettings]
// => appSettings: Either[io.circe.Error,AppSettings] = Right(AppSettings(HttpSettings(ServerSettings(localhost,8080,None),1.1)))
scala> import com.typesafe.config.{ ConfigFactory, ConfigMemorySize }
scala> import io.circe.generic.auto._
scala> import io.github.jonas.circe.config.syntax._
scala> import scala.concurrent.duration.FiniteDuration

scala> case class ServerSettings(host: String, port: Int, timeout: FiniteDuration, maxUpload: ConfigMemorySize)
scala> case class HttpSettings(server: ServerSettings, version: Option[Double])
scala> case class AppSettings(http: HttpSettings)

scala> val config = ConfigFactory.load()

scala> config.as[ServerSettings]("http.server")
res0: Either[io.circe.Error,ServerSettings] = Right(ServerSettings(localhost,8080,5 seconds,ConfigMemorySize(5242880)))

scala> config.as[HttpSettings]("http")
res1: Either[io.circe.Error,HttpSettings] = Right(HttpSettings(ServerSettings(localhost,8080,5 seconds,ConfigMemorySize(5242880)),Some(1.1)))

scala> config.as[AppSettings]
res2: Either[io.circe.Error,AppSettings] = Right(AppSettings(HttpSettings(ServerSettings(localhost,8080,5 seconds,ConfigMemorySize(5242880)),Some(1.1))))
```

Based on this [application.conf].

[application.conf]: https://github.com/jonas/circe-config/tree/master/src/test/resources/application.conf

## Releasing

To release version `x.y.z` run:
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ libraryDependencies ++= Seq(

autoAPIMappings := true
doctestTestFramework := DoctestTestFramework.ScalaTest
doctestMarkdownEnabled := true
doctestWithDependencies := false
scalacOptions in (Compile,doc) := Seq(
"-groups",
Expand Down
9 changes: 9 additions & 0 deletions src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
http {
version = 1.1
server {
host = localhost
port = 8080
timeout = 5 s
maxUpload = 5M
}
}

0 comments on commit e529214

Please sign in to comment.