diff --git a/js/src/main/scala/com/comcast/ip4s/HostPlatform.scala b/js/src/main/scala/com/comcast/ip4s/HostPlatform.scala deleted file mode 100644 index 36ed73ff..00000000 --- a/js/src/main/scala/com/comcast/ip4s/HostPlatform.scala +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2018 Comcast Cable Communications Management, LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.comcast.ip4s - -private[ip4s] trait HostPlatform diff --git a/jvm/src/main/scala/com/comcast/ip4s/HostPlatform.scala b/jvm/src/main/scala/com/comcast/ip4s/HostPlatform.scala deleted file mode 100644 index 15713b3e..00000000 --- a/jvm/src/main/scala/com/comcast/ip4s/HostPlatform.scala +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2018 Comcast Cable Communications Management, LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.comcast.ip4s - -import cats.{Applicative, ApplicativeThrow} -import cats.syntax.all._ - -private[ip4s] trait HostPlatform { self: Host => - - /** Resolves this host to an ip address using the platform DNS resolver. - * - * If the host cannot be resolved, the effect fails with a `java.net.UnknownHostException`. - */ - def resolve[F[_]: Dns: Applicative]: F[IpAddress] = - self match { - case ip: IpAddress => Applicative[F].pure(ip) - case hostname: Hostname => Dns[F].resolve(hostname) - case idn: IDN => Dns[F].resolve(idn.hostname) - } - - /** Resolves this host to an ip address using the platform DNS resolver. - * - * If the host cannot be resolved, a `None` is returned. - */ - def resolveOption[F[_]: Dns: ApplicativeThrow]: F[Option[IpAddress]] = - resolve[F].map(Some(_): Option[IpAddress]).recover { case _: UnknownHostException => None } - - /** Resolves this host to all ip addresses known to the platform DNS resolver. - * - * If the host cannot be resolved, an empty list is returned. - */ - def resolveAll[F[_]: Dns: Applicative]: F[List[IpAddress]] = - self match { - case ip: IpAddress => Applicative[F].pure(List(ip)) - case hostname: Hostname => Dns[F].resolveAll(hostname) - case idn: IDN => Dns[F].resolveAll(idn.hostname) - } -} diff --git a/jvm/src/main/scala/com/comcast/ip4s/IpAddressPlatform.scala b/jvm/src/main/scala/com/comcast/ip4s/IpAddressPlatform.scala index a725f4fd..78e69bda 100644 --- a/jvm/src/main/scala/com/comcast/ip4s/IpAddressPlatform.scala +++ b/jvm/src/main/scala/com/comcast/ip4s/IpAddressPlatform.scala @@ -30,9 +30,6 @@ private[ip4s] trait IpAddressCompanionPlatform { def fromInetAddress(address: InetAddress): IpAddress = IpAddress.fromBytes(address.getAddress).get - /** Gets an IP address repesenting the loopback interface. */ - def loopback[F[_]](implicit F: Dns[F]): F[IpAddress] = - F.loopback } private[ip4s] trait Ipv4AddressPlatform extends IpAddressPlatform { diff --git a/shared/src/main/scala/com/comcast/ip4s/Host.scala b/shared/src/main/scala/com/comcast/ip4s/Host.scala index f991ad03..2f9827dc 100644 --- a/shared/src/main/scala/com/comcast/ip4s/Host.scala +++ b/shared/src/main/scala/com/comcast/ip4s/Host.scala @@ -16,10 +16,14 @@ package com.comcast.ip4s -import cats.{Order, Show} +import cats.{Applicative, ApplicativeThrow, Order, Show} +import cats.syntax.all._ import scala.util.hashing.MurmurHash3 +// annoying bincompat shim +private[ip4s] trait HostPlatform + /** ADT representing either an `IpAddress`, `Hostname`, or `IDN`. */ sealed trait Host extends HostPlatform with Ordered[Host] { @@ -52,6 +56,35 @@ sealed trait Host extends HostPlatform with Ordered[Host] { case y: IDN => x.hostname.toString.compare(y.hostname.toString) } } + + /** Resolves this host to an ip address using the platform DNS resolver. + * + * If the host cannot be resolved, the effect fails with a `com.comcast.ip4s.UnknownHostException`. + */ + def resolve[F[_]: Dns: Applicative]: F[IpAddress] = + this match { + case ip: IpAddress => Applicative[F].pure(ip) + case hostname: Hostname => Dns[F].resolve(hostname) + case idn: IDN => Dns[F].resolve(idn.hostname) + } + + /** Resolves this host to an ip address using the platform DNS resolver. + * + * If the host cannot be resolved, a `None` is returned. + */ + def resolveOption[F[_]: Dns: ApplicativeThrow]: F[Option[IpAddress]] = + resolve[F].map(Some(_): Option[IpAddress]).recover { case _: UnknownHostException => None } + + /** Resolves this host to all ip addresses known to the platform DNS resolver. + * + * If the host cannot be resolved, an empty list is returned. + */ + def resolveAll[F[_]: Dns: Applicative]: F[List[IpAddress]] = + this match { + case ip: IpAddress => Applicative[F].pure(List(ip)) + case hostname: Hostname => Dns[F].resolveAll(hostname) + case idn: IDN => Dns[F].resolveAll(idn.hostname) + } } object Host { @@ -255,6 +288,10 @@ object IpAddress extends IpAddressCompanionPlatform { def fromBytes(bytes: Array[Byte]): Option[IpAddress] = Ipv4Address.fromBytes(bytes) orElse Ipv6Address.fromBytes(bytes) + /** Gets an IP address repesenting the loopback interface. */ + def loopback[F[_]](implicit F: Dns[F]): F[IpAddress] = + F.loopback + private[ip4s] def compareBytes(x: IpAddress, y: IpAddress): Int = { var i, result = 0 val xb = x.bytes