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
david.geirola
committed
Nov 1, 2021
1 parent
d5bb042
commit d169c32
Showing
7 changed files
with
167 additions
and
1 deletion.
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,38 @@ | ||
|
||
# Ip4s module for PureConfig | ||
|
||
Adds support for [Ip4s](https://github.com/Comcast/ip4s)'s Hostname and Port class to PureConfig. PRs adding support | ||
for other classes are welcome :) | ||
|
||
## Add pureconfig-ip4s to your project | ||
|
||
In addition to [core PureConfig](https://github.com/pureconfig/pureconfig), you'll need: | ||
|
||
```scala | ||
libraryDependencies += "com.github.pureconfig" %% "pureconfig-ip4s" % "0.17.0" | ||
``` | ||
|
||
## Example | ||
|
||
To load an `Hostname` or a `Port` into a configuration, create a class to hold it: | ||
|
||
```scala | ||
import com.comcast.ip4s.{Hostname, Port} | ||
import pureconfig._ | ||
import pureconfig.generic.auto._ | ||
import pureconfig.module.ip4s._ | ||
|
||
case class MyConfig(hostname: Hostname, port: Port) | ||
``` | ||
|
||
We can read a `MyConfig` with the following code: | ||
|
||
```scala | ||
val source = ConfigSource.string( | ||
"""{ | ||
|hostname: "0.0.0.0" | ||
|port: 8080 | ||
|}""".stripMargin) | ||
|
||
source.load[MyConfig] | ||
``` |
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,11 @@ | ||
import Dependencies.Version._ | ||
|
||
name := "pureconfig-ip4s" | ||
|
||
crossScalaVersions := Seq(scala212, scala213) | ||
|
||
libraryDependencies ++= Seq("com.comcast" %% "ip4s-core" % "3.0.4") | ||
|
||
developers := List( | ||
Developer("geirolz", "David Geirola", "david.geirolz@gmail.com", url("https://github.com/geirolz")) | ||
) |
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,38 @@ | ||
|
||
# Ip4s module for PureConfig | ||
|
||
Adds support for [Ip4s](https://github.com/Comcast/ip4s)'s Hostname and Port class to PureConfig. PRs adding support | ||
for other classes are welcome :) | ||
|
||
## Add pureconfig-ip4s to your project | ||
|
||
In addition to [core PureConfig](https://github.com/pureconfig/pureconfig), you'll need: | ||
|
||
```scala | ||
libraryDependencies += "com.github.pureconfig" %% "pureconfig-ip4s" % "@VERSION@" | ||
``` | ||
|
||
## Example | ||
|
||
To load an `Hostname` or a `Port` into a configuration, create a class to hold it: | ||
|
||
```scala mdoc:silent | ||
import com.comcast.ip4s.{Hostname, Port} | ||
import pureconfig._ | ||
import pureconfig.generic.auto._ | ||
import pureconfig.module.ip4s._ | ||
|
||
case class MyConfig(hostname: Hostname, port: Port) | ||
``` | ||
|
||
We can read a `MyConfig` with the following code: | ||
|
||
```scala mdoc:silent | ||
val source = ConfigSource.string( | ||
"""{ | ||
|hostname: "0.0.0.0" | ||
|port: 8080 | ||
|}""".stripMargin) | ||
|
||
source.load[MyConfig] | ||
``` |
31 changes: 31 additions & 0 deletions
31
modules/ip4s/src/main/scala/pureconfig/module/ip4s/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,31 @@ | ||
package pureconfig.module | ||
|
||
import com.comcast.ip4s.{Hostname, Port} | ||
|
||
import pureconfig.error.CannotConvert | ||
import pureconfig.{ConfigReader, ConfigWriter} | ||
|
||
package object ip4s { | ||
|
||
implicit val hostnameWriter: ConfigWriter[Hostname] = | ||
ConfigWriter[String].contramap(_.toString) | ||
|
||
implicit val hostnameReader: ConfigReader[Hostname] = | ||
ConfigReader.fromString(str => | ||
Hostname.fromString(str) match { | ||
case Some(hostname) => Right(hostname) | ||
case None => Left(CannotConvert(str, "Hostname", "Invalid hostname")) | ||
} | ||
) | ||
|
||
implicit val portWriter: ConfigWriter[Port] = | ||
ConfigWriter[Int].contramap(_.value) | ||
|
||
implicit val portReader: ConfigReader[Port] = | ||
ConfigReader[Int].emap(str => | ||
Port.fromInt(str) match { | ||
case Some(port) => Right(port) | ||
case None => Left(CannotConvert(str.toString, "Port", "Invalid port")) | ||
} | ||
) | ||
} |
46 changes: 46 additions & 0 deletions
46
modules/ip4s/src/test/scala/pureconfig/module/ip4s/Ip4sTest.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,46 @@ | ||
package pureconfig.module.ip4s | ||
|
||
import com.comcast.ip4s.{Hostname, Port} | ||
|
||
import pureconfig.error.{CannotConvert, ConfigReaderFailures, ConvertFailure} | ||
import pureconfig.syntax._ | ||
import pureconfig.{BaseSuite, ConfigWriter} | ||
|
||
class Ip4sTest extends BaseSuite { | ||
|
||
"reading the hostname config" should "parse the hostname" in { | ||
val hostname = "0.0.0.0" | ||
configString(hostname).to[Hostname].value shouldEqual Hostname.fromString(hostname).get | ||
} | ||
|
||
"reading the hostname config" should "get a CannotConvert error" in { | ||
configString("...").to[Hostname].left.value shouldEqual ConfigReaderFailures( | ||
ConvertFailure(CannotConvert("...", "Hostname", "Invalid hostname"), stringConfigOrigin(1), "") | ||
) | ||
} | ||
|
||
"Hostname ConfigReader and ConfigWriter" should "be able to round-trip reading/writing of Hostname" in { | ||
val expectedHostname = Hostname.fromString("0.0.0.0").get | ||
|
||
val configValue = ConfigWriter[Hostname].to(expectedHostname) | ||
configValue.to[Hostname].value shouldEqual expectedHostname | ||
} | ||
|
||
"reading the port config" should "parse the port" in { | ||
val port = "8080" | ||
configString(port).to[Port].value shouldEqual Port.fromString(port).get | ||
} | ||
|
||
"reading the port config" should "get a CannotConvert error" in { | ||
configString("-1").to[Port].left.value shouldEqual ConfigReaderFailures( | ||
ConvertFailure(CannotConvert("-1", "Port", "Invalid port"), stringConfigOrigin(1), "") | ||
) | ||
} | ||
|
||
"Port ConfigReader and ConfigWriter" should "be able to round-trip reading/writing of Port" in { | ||
val expectedPort = Port.fromInt(8080).get | ||
|
||
val configValue = ConfigWriter[Port].to(expectedPort) | ||
configValue.to[Port].value shouldEqual expectedPort | ||
} | ||
} |