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

Unplatform Host #423

Merged
merged 3 commits into from
Aug 19, 2022
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
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