-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from NthPortal/context-redesign/PR
Redesign context implementation
- Loading branch information
Showing
33 changed files
with
561 additions
and
391 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
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
82 changes: 82 additions & 0 deletions
82
core/common/src/main/scala/org/typelevel/otel4s/context/Contextual.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 | ||
|
||
trait Contextual[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 ContextSyntax(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) | ||
|
||
/** Creates a copy of this context with the given value associated with the | ||
* given key. | ||
*/ | ||
def updated[A](key: Key[A], value: A): C = | ||
outer.updated(ctx)(key, value) | ||
} | ||
} | ||
|
||
object Contextual { | ||
|
||
/** A type alias for a [[`Contextual`]] explicitly parameterized by its | ||
* [[Contextual.Key `Key`]] type. | ||
*/ | ||
type Keyed[C, K[X] <: Key[X]] = Contextual[C] { type Key[A] = K[A] } | ||
|
||
/** Summons a [[`Contextual`]] that is available implicitly. */ | ||
def apply[C](implicit c: Contextual[C]): Contextual[C] = c | ||
|
||
trait Syntax { | ||
implicit def toContextSyntax[C](ctx: C)(implicit | ||
c: Contextual[C] | ||
): c.ContextSyntax = | ||
new c.ContextSyntax(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 | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
core/common/src/main/scala/org/typelevel/otel4s/context/syntax.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,19 @@ | ||
/* | ||
* 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 | ||
|
||
object syntax extends Contextual.Syntax |
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.syntax._ | ||
import org.typelevel.otel4s.context.vault.VaultContext | ||
|
||
class ContextSuite extends FunSuite { | ||
test("implicit syntax") { | ||
def check[C, K[X] <: Key[X]](implicit | ||
c: Contextual.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] | ||
} | ||
} |
Oops, something went wrong.