Skip to content

Commit

Permalink
Merge pull request #430 from armanbilge/topic/socket-address-resolve
Browse files Browse the repository at this point in the history
Move `resolve` method to `SocketAddress`
  • Loading branch information
mpilquist authored Oct 7, 2022
2 parents 043159f + 73da7c1 commit d5899d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.comcast.ip4s

import cats.Applicative
import cats.syntax.all._

import java.net.InetSocketAddress

Expand All @@ -30,15 +29,14 @@ private[ip4s] trait SocketAddressPlatform[+A <: Host] {
}

private[ip4s] trait SocketAddressCompanionPlatform {
implicit class ResolveOps(private val self: SocketAddress[Host]) {
// bincompat
@deprecated("resolve is now defined on SocketAddress", "3.2.1")
def ResolveOps(self: SocketAddress[Host]): ResolveOps = new ResolveOps(self)
@deprecated("resolve is now defined on SocketAddress", "3.2.1")
class ResolveOps(private val self: SocketAddress[Host]) {

/** Resolves this `SocketAddress[Hostname]` to a `SocketAddress[IpAddress]`. */
def resolve[F[_]: Dns: Applicative]: F[SocketAddress[IpAddress]] =
self.host match {
case ip: IpAddress => Applicative[F].pure(SocketAddress(ip, self.port))
case hostname: Hostname => Dns[F].resolve(hostname).map(ip => SocketAddress(ip, self.port))
case idn: IDN => Dns[F].resolve(idn.hostname).map(ip => SocketAddress(ip, self.port))
}
def resolve[F[_]: Dns: Applicative]: F[SocketAddress[IpAddress]] = self.resolve
}

/** Converts an `InetSocketAddress` to a `SocketAddress[IpAddress]`. */
Expand Down
7 changes: 6 additions & 1 deletion shared/src/main/scala/com/comcast/ip4s/SocketAddress.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package com.comcast.ip4s

import cats.{Order, Show}
import cats.{Applicative, Order, Show}
import cats.syntax.all._

/** An IP address of the specified type and a port number. Used to describe the source or destination of a socket.
*/
Expand All @@ -26,6 +27,10 @@ final case class SocketAddress[+A <: Host](host: A, port: Port) extends SocketAd
case _: Ipv6Address => s"[$host]:$port"
case _ => s"$host:$port"
}

/** Resolves this `SocketAddress[Hostname]` to a `SocketAddress[IpAddress]`. */
final def resolve[F[_]: Dns: Applicative]: F[SocketAddress[IpAddress]] =
host.resolve.map(SocketAddress(_, port))
}

object SocketAddress extends SocketAddressCompanionPlatform {
Expand Down

0 comments on commit d5899d7

Please sign in to comment.