Skip to content

Commit

Permalink
Add Cats Monoid and Eq instances for Kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
bpholt committed Oct 4, 2024
1 parent 7fb4977 commit c4d9156
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ lazy val commonSettings = Seq(
"org.scalameta" %%% "munit" % "1.0.0" % Test,
"org.scalameta" %%% "munit-scalacheck" % "1.0.0-M11" % Test,
"org.typelevel" %%% "munit-cats-effect" % "2.0.0" % Test,
"org.typelevel" %%% "scalacheck-effect-munit" % "2.0.0-M2" % Test
"org.typelevel" %%% "scalacheck-effect-munit" % "2.0.0-M2" % Test,
"org.typelevel" %%% "cats-kernel-laws" % "2.11.0" % Test,
"org.typelevel" %%% "cats-laws" % "2.11.0" % Test,
"org.typelevel" %%% "discipline-munit" % "2.0.0-M3" % Test
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2019-2020 by Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package natchez

import cats.kernel.laws.discipline.*
import munit.DisciplineSuite

class KernelInstancesLawSuite extends DisciplineSuite with Arbitraries {
checkAll("Kernel.MonoidLaws", MonoidTests[Kernel].monoid)
checkAll("Kernel.EqLaws", EqTests[Kernel].eqv)
}
12 changes: 10 additions & 2 deletions modules/core/shared/src/main/scala/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

package natchez

import org.typelevel.ci._
import cats.syntax.all.*
import cats.{Eq, Monoid}
import org.typelevel.ci.*

import java.util
import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*

/** An opaque hunk of data that we can hand off to another system (in the form of HTTP headers),
* which can then create new spans as children of this one. By this mechanism we allow our trace
Expand All @@ -21,4 +23,10 @@ final case class Kernel(toHeaders: Map[CIString, String]) {
object Kernel {
private[natchez] def fromJava(headers: util.Map[String, String]): Kernel =
apply(headers.asScala.map { case (k, v) => CIString(k) -> v }.toMap)

implicit val kernelMonoid: Monoid[Kernel] =
Monoid.instance(Kernel(Map.empty), (a, b) => Kernel(a.toHeaders |+| b.toHeaders))

implicit val kernelEq: Eq[Kernel] =
Eq[Map[CIString, String]].contramap(_.toHeaders)
}

0 comments on commit c4d9156

Please sign in to comment.