Skip to content

Commit

Permalink
Merge pull request #475 from armanbilge/topic/dns-for-io
Browse files Browse the repository at this point in the history
Implicit `Dns` for `IO` only
  • Loading branch information
mpilquist authored Mar 22, 2023
2 parents 15b75ec + 4e14485 commit 0a8551a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
5 changes: 2 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import com.typesafe.tools.mima.core._

ThisBuild / tlBaseVersion := "3.2"
ThisBuild / tlBaseVersion := "3.3"

ThisBuild / organization := "com.comcast"
ThisBuild / organizationName := "Comcast Cable Communications Management, LLC"
Expand Down Expand Up @@ -46,7 +46,6 @@ lazy val testKit = crossProject(JVMPlatform, JSPlatform, NativePlatform)
libraryDependencies ++= Seq(
"org.scalacheck" %%% "scalacheck" % "1.17.0",
"org.scalameta" %%% "munit-scalacheck" % "1.0.0-M7" % Test,
"org.typelevel" %%% "cats-effect" % "3.4.8" % Test,
"org.typelevel" %%% "munit-cats-effect" % "2.0.0-M3" % Test
)
)
Expand Down Expand Up @@ -78,7 +77,7 @@ lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
libraryDependencies ++= Seq(
"org.typelevel" %%% "literally" % "1.1.0",
"org.typelevel" %%% "cats-core" % "2.9.0",
"org.typelevel" %%% "cats-effect-kernel" % "3.4.8",
"org.typelevel" %%% "cats-effect" % "3.4.8",
"org.scalacheck" %%% "scalacheck" % "1.17.0" % Test
)
)
Expand Down
2 changes: 1 addition & 1 deletion js/src/main/scala/com/comcast/ip4s/DnsPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import scala.scalajs.js.|
import scala.scalajs.js.annotation.JSImport

private[ip4s] trait DnsCompanionPlatform {
implicit def forAsync[F[_]](implicit F: Async[F]): Dns[F] = new UnsealedDns[F] {
def forAsync[F[_]](implicit F: Async[F]): Dns[F] = new UnsealedDns[F] {
def resolve(hostname: Hostname): F[IpAddress] =
F.fromPromise(F.delay(dnsPromises.lookup(hostname.toString, LookupOptions(all = false))))
.flatMap { address =>
Expand Down
5 changes: 4 additions & 1 deletion jvm-native/src/main/scala/com/comcast/ip4s/DnsPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package com.comcast.ip4s

import cats.effect.kernel.Async
import cats.effect.kernel.Sync
import cats.syntax.all._

import java.net.InetAddress

private[ip4s] trait DnsCompanionPlatform {
implicit def forSync[F[_]](implicit F: Sync[F]): Dns[F] = new UnsealedDns[F] {
def forAsync[F[_]: Async]: Dns[F] = forSync // alias for cross-compiling w/ JS

def forSync[F[_]](implicit F: Sync[F]): Dns[F] = new UnsealedDns[F] {
def resolve(hostname: Hostname): F[IpAddress] =
F.blocking {
val addr = InetAddress.getByName(hostname.toString)
Expand Down
4 changes: 4 additions & 0 deletions shared/src/main/scala/com/comcast/ip4s/Dns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.comcast.ip4s

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.
Expand Down Expand Up @@ -66,4 +68,6 @@ private[ip4s] trait UnsealedDns[F[_]] extends Dns[F]

object Dns extends DnsCompanionPlatform {
def apply[F[_]](implicit F: Dns[F]): F.type = F

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

0 comments on commit 0a8551a

Please sign in to comment.