-
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
23 changed files
with
378 additions
and
383 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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
/* | ||
* 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 => | ||
type Key[A] <: context.Key[A] | ||
|
||
def get[A](ctx: C)(key: Key[A]): Option[A] | ||
|
||
def getOrElse[A](ctx: C)(key: Key[A], default: => A): A = | ||
get(ctx)(key).getOrElse(default) | ||
|
||
def updated[A](ctx: C)(key: Key[A], value: A): C | ||
|
||
def root: C | ||
|
||
class Ops(ctx: C) { | ||
def get[A](key: Key[A]): Option[A] = | ||
outer.get(ctx)(key) | ||
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 { | ||
type Keyed[C, K[_]] = Context[C] { type Key[A] = K[A] } | ||
|
||
def apply[C](implicit c: Context[C]): Context[C] = c | ||
|
||
object Implicits { | ||
implicit def mkOps[C](ctx: C)(implicit c: Context[C]): c.Ops = | ||
new c.Ops(ctx) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
/* | ||
* 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 | ||
|
||
trait Key[A] { | ||
val name: String | ||
|
||
override def toString: String = s"Key($name)" | ||
} | ||
|
||
object Key { | ||
trait Provider[F[_], K[_]] { | ||
def uniqueKey[A](name: String): F[K[A]] | ||
} | ||
object Provider { | ||
def apply[F[_], K[_]](implicit p: Provider[F, K]): Provider[F, K] = p | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
core/common/src/main/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,62 @@ | ||
/* | ||
* 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 | ||
|
||
final case class VaultContext(vault: Vault) { | ||
def get[A](key: VaultContext.Key[A]): Option[A] = | ||
vault.lookup(key.underlying) | ||
def getOrElse[A](key: VaultContext.Key[A], default: => A): A = | ||
get(key).getOrElse(default) | ||
def updated[A](key: VaultContext.Key[A], value: A): VaultContext = | ||
VaultContext(vault.insert(key.underlying, value)) | ||
} | ||
|
||
object VaultContext extends (Vault => VaultContext) { | ||
final class Key[A] private ( | ||
val name: String, | ||
private[VaultContext] val underlying: VaultKey[A] | ||
) extends context.Key[A] | ||
object Key { | ||
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) | ||
} | ||
} | ||
|
||
val root: VaultContext = apply(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 | ||
} | ||
} |
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
Oops, something went wrong.