Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# sttp module for PureConfig | ||
|
||
Adds support for [sttp](https://github.com/softwaremill/sttp). Currently supports only `com.softwaremill.sttp.Uri`. | ||
|
||
## Add pureconfig-sttp to your project | ||
|
||
In addition to [core PureConfig](https://github.com/pureconfig/pureconfig), you'll need: | ||
|
||
```scala | ||
libraryDependencies += "com.github.pureconfig" %% "pureconfig-sttp" % "0.10.2" | ||
``` | ||
|
||
## Example | ||
|
||
To load an sttp `Uri` into a configuration, create a new class: | ||
|
||
```scala | ||
import com.softwaremill.sttp.Uri | ||
import com.typesafe.config.ConfigFactory.parseString | ||
import pureconfig._ | ||
import pureconfig.generic.auto._ | ||
import pureconfig.module.sttp._ | ||
|
||
case class AppConfig(uri: Uri) | ||
``` | ||
|
||
Now, we can load the configuration with the following code: | ||
|
||
```scala | ||
val config = parseString("""{uri: "https://sttp.readthedocs.io" }""") | ||
// config: com.typesafe.config.Config = Config(SimpleConfigObject({"uri":"https://sttp.readthedocs.io"})) | ||
|
||
loadConfig[AppConfig](config) | ||
// res0: Either[pureconfig.error.ConfigReaderFailures,AppConfig] = Right(AppConfig(https://sttp.readthedocs.io)) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name := "pureconfig-sttp" | ||
|
||
libraryDependencies ++= Seq( | ||
"com.softwaremill.sttp" %% "core" % "1.5.10") | ||
|
||
developers := List( | ||
Developer("bszwej", "Bartlomiej Szwej", "bszwej@gmail.com", url("https://github.com/bszwej"))) | ||
|
||
osgiSettings | ||
|
||
OsgiKeys.exportPackage := Seq("pureconfig.module.sttp.*") | ||
OsgiKeys.privatePackage := Seq() | ||
OsgiKeys.importPackage := Seq(s"""scala.*;version="[${scalaBinaryVersion.value}.0,${scalaBinaryVersion.value}.50)"""", "*") |
18 changes: 18 additions & 0 deletions
18
modules/sttp/src/main/scala/pureconfig/module/sttp/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package pureconfig.module | ||
|
||
import com.softwaremill.sttp._ | ||
import pureconfig.ConfigReader | ||
import pureconfig.error.CannotConvert | ||
|
||
import scala.util.Try | ||
|
||
package object sttp { | ||
|
||
implicit val reader: ConfigReader[Uri] = | ||
ConfigReader.fromNonEmptyString { str ⇒ | ||
Try(uri"$str").fold( | ||
ex ⇒ Left(CannotConvert(str, "com.softwaremill.sttp.Uri", ex.getMessage)), | ||
uri ⇒ Right(uri)) | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
modules/sttp/src/test/scala/pureconfig/module/sttp/SttpSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package pureconfig.module.sttp | ||
|
||
import com.softwaremill.sttp._ | ||
import com.typesafe.config.ConfigFactory | ||
import pureconfig.BaseSuite | ||
import pureconfig.error.{ CannotConvert, ConfigReaderFailures, ConvertFailure } | ||
import pureconfig.generic.auto._ | ||
import pureconfig.syntax._ | ||
|
||
class SttpSuite extends BaseSuite { | ||
|
||
case class AppConfig(uri: Uri) | ||
|
||
behavior of "sttp module" | ||
|
||
it should "read uri" in { | ||
val config = ConfigFactory.parseString("""{uri = "https://sttp.readthedocs.io"}""") | ||
|
||
config.to[AppConfig].right.value shouldBe AppConfig(uri"https://sttp.readthedocs.io") | ||
} | ||
|
||
it should "handle error when reading uri" in { | ||
val config = ConfigFactory.parseString("""{uri = "https!!://wrong.io"}""") | ||
|
||
val failure = | ||
ConvertFailure( | ||
reason = CannotConvert( | ||
value = "https!!://wrong.io", | ||
toType = "com.softwaremill.sttp.Uri", | ||
because = "requirement failed: Scheme can only contain alphanumeric characters, +, - and ."), | ||
location = None, | ||
path = "uri") | ||
|
||
config.to[AppConfig].left.value shouldBe ConfigReaderFailures(failure) | ||
} | ||
|
||
} |