Skip to content

Commit

Permalink
Merge pull request #423 from armanbilge/topic/unplatform-host
Browse files Browse the repository at this point in the history
Unplatform `Host`
  • Loading branch information
mpilquist committed Aug 19, 2022
2 parents 1e4fda5 + fdf60e8 commit d84a662
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 75 deletions.
19 changes: 0 additions & 19 deletions js/src/main/scala/com/comcast/ip4s/HostPlatform.scala

This file was deleted.

52 changes: 0 additions & 52 deletions jvm/src/main/scala/com/comcast/ip4s/HostPlatform.scala

This file was deleted.

3 changes: 0 additions & 3 deletions jvm/src/main/scala/com/comcast/ip4s/IpAddressPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 38 additions & 1 deletion shared/src/main/scala/com/comcast/ip4s/Host.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] {

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d84a662

Please sign in to comment.