Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support List[InetSocketAddress] #156

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ package object javanet {
implicit val inetSocketAddressConfigConvert: ConfigConvert[InetSocketAddress] =
ConfigConvert.viaNonEmptyString(s => parseHostAndPort(s), address => s"${address.getHostString}:${address.getPort}")

implicit val inetSocketAddressListConfigConvert: ConfigConvert[Seq[InetSocketAddress]] =
implicit val inetSocketAddressSeqConfigConvert: ConfigConvert[Seq[InetSocketAddress]] =
ConfigConvert.viaNonEmptyString(
s =>
s.split(", *")
Expand All @@ -59,4 +59,7 @@ package object javanet {
s"${address.getHostString}:${address.getPort}"
}.mkString(",")
)

implicit val inetSocketAddressListConfigConvert: ConfigConvert[List[InetSocketAddress]] =
inetSocketAddressSeqConfigConvert.xmap(_.toList, _.toList)
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ class JavanetSuite extends munit.FunSuite {

val conf = parseString("""hosts: "localhost:65535,127.0.0.1:80,localhost:443"""")

assert(
conf
.to[Config]
.contains(
Config(
Seq(
InetSocketAddress.createUnresolved("localhost", 65535),
InetSocketAddress.createUnresolved("127.0.0.1", 80),
InetSocketAddress.createUnresolved("localhost", 443)
)
)
)
)
}

test("can read multiple addresses as a list") {
case class Config(hosts: List[InetSocketAddress])

val conf = parseString("""hosts: "localhost:65535,127.0.0.1:80,localhost:443"""")

assert(
conf
.to[Config]
Expand All @@ -158,7 +178,7 @@ class JavanetSuite extends munit.FunSuite {

val conf = parseString("""hosts: "localhost:65535"""")

assert(conf.to[Config].contains(Config(List(InetSocketAddress.createUnresolved("localhost", 65535)))))
assert(conf.to[Config].contains(Config(Seq(InetSocketAddress.createUnresolved("localhost", 65535)))))
}

test("is lenient about whitespace") {
Expand All @@ -171,7 +191,7 @@ class JavanetSuite extends munit.FunSuite {
.to[Config]
.contains(
Config(
List(
Seq(
InetSocketAddress.createUnresolved("localhost", 65535),
InetSocketAddress.createUnresolved("127.0.0.1", 80),
InetSocketAddress.createUnresolved("localhost", 443)
Expand All @@ -193,6 +213,18 @@ class JavanetSuite extends munit.FunSuite {
)
}

test("can read back a written List[InetSocketAddress]") {
val addresses = List(
InetSocketAddress.createUnresolved("localhost", 65535),
InetSocketAddress.createUnresolved("127.0.0.1", 80),
InetSocketAddress.createUnresolved("localhost", 443)
)

assert(
ConfigReader[List[InetSocketAddress]].from(ConfigWriter[List[InetSocketAddress]].to(addresses)).contains(addresses)
)
}

test("validates the supplied setting") {
case class Config(hosts: Seq[InetSocketAddress])

Expand Down
Loading