Skip to content

Commit

Permalink
Merge 3fd1855 into 1bccfd5
Browse files Browse the repository at this point in the history
  • Loading branch information
derekmorr committed May 18, 2017
2 parents 1bccfd5 + 3fd1855 commit b8c009f
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
6 changes: 6 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ lazy val javax = (project in file("modules/javax")).
settings(settings).
dependsOn(core)

// akka 2.4 isn't published for Scala 2.10
lazy val akka = (project in file("modules/akka")).
settings(settings).
settings(crossScalaVersions ~= { oldVersions => oldVersions.filterNot(_.startsWith("2.10")) }).
dependsOn(core)

lazy val allVersionCompilerLintSwitches = Seq(
"-deprecation",
"-encoding", "UTF-8", // yes, this is 2 args
Expand Down
35 changes: 35 additions & 0 deletions modules/akka/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Akka module for PureConfig

Adds support for selected [Akka](http://akka.io/) classes to PureConfig.

## Add pureconfig-akka to your project

In addition to [core pureconfig](https://github.com/melrief/pureconfig), you'll need:

```scala
libraryDependencies += "com.github.melrief" %% "pureconfig-akka" % "0.6.0"
```

## Example

To load a `Timeout` into a configuration, we need a class to hold our configuration:

```scala
import akka.util.Timeout
import com.typesafe.config.ConfigFactory.parseString
import pureconfig._
import pureconfig.module.akka._

case class MyConfig(timeout: Timeout)
```

We can read a `MyConfig` like:
```scala
val conf = parseString("""{ timeout: 5 seconds }""")
// conf: com.typesafe.config.Config = Config(SimpleConfigObject({"timeout":"5 seconds"}))

loadConfig[MyConfig](conf)
// res1: Either[pureconfig.error.ConfigReaderFailures,MyConfig] = Right(MyConfig(Timeout(5 seconds)))
```


51 changes: 51 additions & 0 deletions modules/akka/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name := "pureconfig-akka"

organization := "com.github.melrief"

homepage := Some(url("https://github.com/melrief/pureconfig"))

licenses := Seq("Mozilla Public License, version 2.0" -> url("https://www.mozilla.org/MPL/2.0/"))

resolvers ++= Seq(
Resolver.sonatypeRepo("releases"),
Resolver.sonatypeRepo("snapshots")
)

libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.17",
Dependencies.scalaMacrosParadise,
Dependencies.scalaTest
)

publishMavenStyle := true

publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}

publishArtifact in Test := false

pomExtra := (
<scm>
<url>git@github.com:melrief/pureconfig.git</url>
<connection>scm:git:git@github.com:melrief/pureconfig.git</connection>
</scm>
<developers>
<developer>
<id>derekmorr</id>
<name>Derek Morr</name>
<url>https://github.com/derekmorr</url>
</developer>
</developers>)

osgiSettings

OsgiKeys.exportPackage := Seq("pureconfig.module.javax.*")

OsgiKeys.privatePackage := Seq()

OsgiKeys.importPackage := Seq(s"""scala.*;version="[${scalaBinaryVersion.value}.0,${scalaBinaryVersion.value}.50)"""", "*")
18 changes: 18 additions & 0 deletions modules/akka/src/main/scala/pureconfig/module/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pureconfig.module

import scala.concurrent.duration.FiniteDuration
import _root_.akka.util.Timeout
import com.typesafe.config.ConfigValue
import pureconfig.ConfigConvert
import pureconfig.error.ConfigReaderFailures

/**
* ConfigConvert instances for Akka value classes.
*/
package object akka {
implicit val timeoutCC: ConfigConvert[Timeout] = new ConfigConvert[Timeout] {
def from(c: ConfigValue): Either[ConfigReaderFailures, Timeout] = ConfigConvert[FiniteDuration].from(c).right.map(new Timeout(_))
def to(t: Timeout): ConfigValue = ConfigConvert[FiniteDuration].to(t.duration)
}

}
32 changes: 32 additions & 0 deletions modules/akka/src/main/tut/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Akka module for PureConfig

Adds support for selected [Akka](http://akka.io/) classes to PureConfig.

## Add pureconfig-akka to your project

In addition to [core pureconfig](https://github.com/melrief/pureconfig), you'll need:

```scala
libraryDependencies += "com.github.melrief" %% "pureconfig-akka" % "0.6.0"
```

## Example

To load a `Timeout` into a configuration, we need a class to hold our configuration:

```tut:silent
import akka.util.Timeout
import com.typesafe.config.ConfigFactory.parseString
import pureconfig._
import pureconfig.module.akka._
case class MyConfig(timeout: Timeout)
```

We can read a `MyConfig` like:
```tut:book
val conf = parseString("""{ timeout: 5 seconds }""")
loadConfig[MyConfig](conf)
```


18 changes: 18 additions & 0 deletions modules/akka/src/test/scala/pureconfig/module/akka/AkkaSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pureconfig.module.akka

import scala.concurrent.duration._
import akka.util.Timeout
import com.typesafe.config.ConfigFactory
import org.scalatest._
import pureconfig.syntax._

class AkkaSuite extends FlatSpec with Matchers with EitherValues {

case class AkkaConf(timeout: Timeout)

it should "be able to read a config with a Timeout" in {
val expected = 5.seconds
val config = ConfigFactory.parseString(s"""{ timeout: $expected }""")
config.to[AkkaConf].right.value shouldEqual AkkaConf(Timeout(expected))
}
}

0 comments on commit b8c009f

Please sign in to comment.