Skip to content

Commit

Permalink
Merge pull request #337 from NthPortal/context-key-show-hash/PR
Browse files Browse the repository at this point in the history
Add `Hash` and `Show` for `Key`
  • Loading branch information
iRevive authored Oct 18, 2023
2 parents 7df1630 + 4364db9 commit 73f3ba4
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 2 deletions.
23 changes: 21 additions & 2 deletions core/common/src/main/scala/org/typelevel/otel4s/context/Key.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@

package org.typelevel.otel4s.context

/** A key for a context. */
import cats.Hash
import cats.Show

/** A key for a context.
*
* @note
* Implementations MUST treat different instances as non-equal.
*/
trait Key[A] {

/** The debug name of the key. */
val name: String

override def toString: String = s"Key($name)"
override def toString: String = Key.defaultShowKey(this)
}

object Key {
Expand All @@ -45,4 +52,16 @@ object Key {
p: Provider[F, K]
): Provider[F, K] = p
}

@inline private def defaultShowKey(key: Key[_]): String =
s"Key(${key.name})"

implicit def showKey[K[X] <: Key[X], A]: Show[K[A]] =
defaultShowKey(_)

implicit def hashKey[K[X] <: Key[X], A]: Hash[K[A]] =
new Hash[K[A]] {
def hash(x: K[A]): Int = System.identityHashCode(x)
def eqv(x: K[A], y: K[A]): Boolean = x eq y
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.otel4s.context

import cats.effect.SyncIO
import org.scalacheck.Arbitrary
import org.scalacheck.Cogen
import org.scalacheck.Gen
import org.typelevel.otel4s.context.vault.VaultContext

trait KeyInstances {
protected def genVaultKey[A]: Gen[VaultContext.Key[A]] =
Gen.delay {
for (name <- Arbitrary.arbitrary[String])
yield VaultContext.Key.unique[SyncIO, A](name).unsafeRunSync()
}

protected implicit def arbitraryVaultKey[A]: Arbitrary[VaultContext.Key[A]] =
Arbitrary(genVaultKey)
protected implicit def arbitraryKey[A]: Arbitrary[Key[A]] =
Arbitrary(genVaultKey)

protected implicit def cogenVaultKey[A]: Cogen[VaultContext.Key[A]] =
Cogen[String].contramap(_.name)
protected implicit def cogenKey[A]: Cogen[Key[A]] =
Cogen[String].contramap(_.name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.otel4s.context

import cats.kernel.laws.discipline.HashTests
import munit.DisciplineSuite
import org.typelevel.otel4s.context.vault.VaultContext

class KeyLawTests extends DisciplineSuite with KeyInstances {
checkAll("HashLaws[Key[String]]", HashTests[Key[String]].hash)
checkAll(
"HashLaws[VaultContext.Key[String]]",
HashTests[VaultContext.Key[String]].hash
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.otel4s.context

import cats.syntax.show._
import munit.ScalaCheckSuite
import org.scalacheck.Arbitrary
import org.scalacheck.Prop.forAll
import org.typelevel.otel4s.context.vault.VaultContext

class KeyProps extends ScalaCheckSuite with KeyInstances {
property("Show[Key[String]]") {
forAll(Arbitrary.arbitrary[Key[String]]) { key =>
assert(key.show.contains(key.name))
}
}

property("Show[VaultContext.Key[String]]") {
forAll(Arbitrary.arbitrary[VaultContext.Key[String]]) { key =>
assert(key.show.contains(key.name))
}
}

property("Key#toString") {
forAll(Arbitrary.arbitrary[Key[String]]) { key =>
assert(key.toString.contains(key.name))
}
}

property("VaultContext.Key#toString") {
forAll(Arbitrary.arbitrary[VaultContext.Key[String]]) { key =>
assert(key.toString.contains(key.name))
}
}
}

0 comments on commit 73f3ba4

Please sign in to comment.