-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesign context implementation to use a `Context` type class that supports arbitrary types for the context rather than specifically `Vault`. In particular, it supports different context implementations for different backends, and parameterises several types by a `C` parameter that refers to the type of the context.
- Loading branch information
Showing
25 changed files
with
522 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
core/common/src/main/scala/org/typelevel/otel4s/context/Context.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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 | ||
package context | ||
|
||
trait Context[C] { outer => | ||
|
||
/** The type of [[context.Key `Key`]] used by contexts of type `C`. */ | ||
type Key[A] <: context.Key[A] | ||
|
||
/** Retrieves the value associated with the given key from the context, if | ||
* such a value exists. | ||
*/ | ||
def get[A](ctx: C)(key: Key[A]): Option[A] | ||
|
||
/** Retrieves the value associated with the given key from the context, if | ||
* such a value exists; otherwise, returns the provided default value. | ||
*/ | ||
def getOrElse[A](ctx: C)(key: Key[A], default: => A): A = | ||
get(ctx)(key).getOrElse(default) | ||
|
||
/** Creates a copy of this context with the given value associated with the | ||
* given key. | ||
*/ | ||
def updated[A](ctx: C)(key: Key[A], value: A): C | ||
|
||
/** The root context, from which all other contexts are derived. */ | ||
def root: C | ||
|
||
class ContextOps(ctx: C) { | ||
|
||
/** Retrieves the value associated with the given key from the context, if | ||
* such a value exists. | ||
*/ | ||
def get[A](key: Key[A]): Option[A] = | ||
outer.get(ctx)(key) | ||
|
||
/** Retrieves the value associated with the given key from the context, if | ||
* such a value exists; otherwise, returns the provided default value. | ||
*/ | ||
def getOrElse[A](key: Key[A], default: => A): A = | ||
outer.getOrElse(ctx)(key, default) | ||
def updated[A](key: Key[A], value: A): C = | ||
outer.updated(ctx)(key, value) | ||
} | ||
} | ||
|
||
object Context { | ||
|
||
/** A type alias for a [[`Context`]] explicitly parameterized by its | ||
* [[Context.Key `Key`]] type. | ||
*/ | ||
type Keyed[C, K[X] <: Key[X]] = Context[C] { type Key[A] = K[A] } | ||
|
||
/** Summons a [[`Context`]] that is available implicitly. */ | ||
def apply[C](implicit c: Context[C]): Context[C] = c | ||
|
||
object Implicits { | ||
implicit def mkOps[C](ctx: C)(implicit c: Context[C]): c.ContextOps = | ||
new c.ContextOps(ctx) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
core/common/src/main/scala/org/typelevel/otel4s/context/Key.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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 | ||
|
||
/** A key for a context. */ | ||
trait Key[A] { | ||
|
||
/** The debug name of the key. */ | ||
val name: String | ||
|
||
override def toString: String = s"Key($name)" | ||
} | ||
|
||
object Key { | ||
|
||
/** Something that provides context keys. | ||
* | ||
* @tparam K | ||
* the type of keys | ||
*/ | ||
trait Provider[F[_], K[X] <: Key[X]] { | ||
|
||
/** Creates a unique key with the given (debug) name. */ | ||
def uniqueKey[A](name: String): F[K[A]] | ||
} | ||
|
||
object Provider { | ||
|
||
/** Summons a [[`Provider`]] that is available implicitly. */ | ||
def apply[F[_], K[X] <: Key[X]](implicit | ||
p: Provider[F, K] | ||
): Provider[F, K] = p | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
core/common/src/test/scala/org/typelevel/otel4s/context/ContextSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 munit.FunSuite | ||
import org.typelevel.otel4s.context.Context.Implicits._ | ||
import org.typelevel.otel4s.context.vault.VaultContext | ||
|
||
class ContextSuite extends FunSuite { | ||
test("implicit syntax") { | ||
def check[C, K[X] <: Key[X]](implicit | ||
c: Context.Keyed[C, K], | ||
kp: Key.Provider[SyncIO, K] | ||
): Unit = { | ||
val key1 = kp.uniqueKey[String]("key1").unsafeRunSync() | ||
val key2 = kp.uniqueKey[Int]("key2").unsafeRunSync() | ||
|
||
var ctx = c.root | ||
assertEquals(ctx.get(key1), None) | ||
assertEquals(ctx.get(key2), None) | ||
|
||
ctx = ctx.updated(key1, "1") | ||
assertEquals(ctx.get(key1), Some("1")) | ||
assertEquals(ctx.get(key2), None) | ||
|
||
ctx = ctx.updated(key1, "2") | ||
assertEquals(ctx.get(key1), Some("2")) | ||
assertEquals(ctx.get(key2), None) | ||
|
||
ctx = ctx.updated(key2, 1) | ||
assertEquals(ctx.get(key1), Some("2")) | ||
assertEquals(ctx.get(key2), Some(1)) | ||
} | ||
|
||
check[VaultContext, VaultContext.Key] | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
core/common/src/test/scala/org/typelevel/otel4s/context/vault/VaultContext.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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 | ||
package context | ||
package vault | ||
|
||
import cats.Functor | ||
import cats.effect.Unique | ||
import cats.syntax.functor._ | ||
import org.typelevel.vault.{Key => VaultKey} | ||
import org.typelevel.vault.Vault | ||
|
||
/** A context implementation backed by a [[org.typelevel.vault.Vault `Vault`]]. | ||
*/ | ||
final class VaultContext private (vault: Vault) { | ||
|
||
/** Retrieves the value associated with the given key from the context, if | ||
* such a value exists. | ||
*/ | ||
def get[A](key: VaultContext.Key[A]): Option[A] = | ||
vault.lookup(key.underlying) | ||
|
||
/** Retrieves the value associated with the given key from the context, if | ||
* such a value exists; otherwise, returns the provided default value. | ||
*/ | ||
def getOrElse[A](key: VaultContext.Key[A], default: => A): A = | ||
get(key).getOrElse(default) | ||
|
||
/** Creates a copy of this context with the given value associated with the | ||
* given key. | ||
*/ | ||
def updated[A](key: VaultContext.Key[A], value: A): VaultContext = | ||
new VaultContext(vault.insert(key.underlying, value)) | ||
} | ||
|
||
object VaultContext { | ||
|
||
/** A key for use with a [[`VaultContext`]]. */ | ||
final class Key[A] private ( | ||
val name: String, | ||
private[VaultContext] val underlying: VaultKey[A] | ||
) extends context.Key[A] | ||
|
||
object Key { | ||
|
||
/** Creates a unique key with the given (debug) name. */ | ||
def unique[F[_]: Functor: Unique, A](name: String): F[Key[A]] = | ||
VaultKey.newKey[F, A].map(new Key[A](name, _)) | ||
|
||
implicit def provider[F[_]: Functor: Unique]: context.Key.Provider[F, Key] = | ||
new context.Key.Provider[F, Key] { | ||
def uniqueKey[A](name: String): F[Key[A]] = unique(name) | ||
} | ||
} | ||
|
||
/** The root [[`VaultContext`]], from which all other contexts are derived. */ | ||
val root: VaultContext = new VaultContext(Vault.empty) | ||
|
||
implicit object Ctx extends Context[VaultContext] { | ||
type Key[A] = VaultContext.Key[A] | ||
|
||
def get[A](ctx: VaultContext)(key: Key[A]): Option[A] = | ||
ctx.get(key) | ||
def updated[A](ctx: VaultContext)(key: Key[A], value: A): VaultContext = | ||
ctx.updated(key, value) | ||
def root: VaultContext = VaultContext.root | ||
} | ||
} |
Oops, something went wrong.