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

Add Dns#mapK, instances for EitherT, Kleisli #478

Merged
merged 1 commit into from
Mar 26, 2023
Merged
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
24 changes: 23 additions & 1 deletion shared/src/main/scala/com/comcast/ip4s/Dns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

package com.comcast.ip4s

import cats.~>
import cats.Functor
import cats.data.EitherT
import cats.data.Kleisli
import cats.effect.IO

/** Capability for an effect `F[_]` which can do DNS lookups.
*
* An instance is available for any effect which has a `Sync` instance on JVM and `Async` on Node.js.
*/
sealed trait Dns[F[_]] {
sealed trait Dns[F[_]] { outer =>

/** Resolves the supplied hostname to an ip address using the platform DNS resolver.
*
Expand Down Expand Up @@ -62,6 +66,17 @@ sealed trait Dns[F[_]] {

/** Gets an IP address representing the loopback interface. */
def loopback: F[IpAddress]

/** Translates effect type from `F` to `G` using the supplied `FunctionK` */
final def mapK[G[_]](fk: F ~> G): Dns[G] = new Dns[G] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some libraries call this translate.

def resolve(hostname: Hostname) = fk(outer.resolve(hostname))
def resolveOption(hostname: Hostname) = fk(outer.resolveOption(hostname))
def resolveAll(hostname: Hostname) = fk(outer.resolveAll(hostname))
def reverse(address: IpAddress) = fk(outer.reverse(address))
def reverseOption(address: IpAddress) = fk(outer.reverseOption(address))
def reverseAll(address: IpAddress) = fk(outer.reverseAll(address))
def loopback = fk(outer.loopback)
}
}

private[ip4s] trait UnsealedDns[F[_]] extends Dns[F]
Expand All @@ -70,4 +85,11 @@ object Dns extends DnsCompanionPlatform {
def apply[F[_]](implicit F: Dns[F]): F.type = F

implicit def forIO: Dns[IO] = forAsync[IO]

implicit def forEitherT[F[_]: Functor, A](implicit dns: Dns[F]): Dns[EitherT[F, A, *]] =
dns.mapK(EitherT.liftK)

implicit def forKleisli[F[_], A](implicit dns: Dns[F]): Dns[Kleisli[F, A, *]] =
dns.mapK(Kleisli.liftK)

}