Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! Redesign context implementation
Browse files Browse the repository at this point in the history
Key.Provider and Ops/syntax
  • Loading branch information
NthPortal committed Aug 11, 2023
1 parent b44b5ca commit 59dde4e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package org.typelevel.otel4s
package context

trait Context[C] {
trait Context[C] { outer =>
type Key[A] <: context.Key[A]
type KeyCreationBounds[_[_]]
type KeyTypeBounds[_]

def get[A](ctx: C)(key: Key[A]): Option[A]

Expand All @@ -31,11 +29,23 @@ trait Context[C] {

def root: C

def uniqueKey[F[_]: KeyCreationBounds, A: KeyTypeBounds](
name: String
): F[Key[A]]
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ trait Key[A] {
}

object Key {
sealed trait NoBounds[+A]
object NoBounds {
implicit object Instance extends NoBounds[Nothing]
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
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ package vault
import cats.Functor
import cats.effect.Unique
import cats.syntax.functor._
import org.typelevel.vault.{Key => VaultKey}
import org.typelevel.vault.Vault
import org.typelevel.vault.{Vault, Key => VaultKey}

final case class VaultContext(vault: Vault) {
def get[A](key: VaultContext.Key[A]): Option[A] =
Expand All @@ -41,33 +40,22 @@ object VaultContext extends (Vault => VaultContext) {
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)

final case class UniqueFunctor[F[_]](functor: Functor[F], unique: Unique[F])
object UniqueFunctor {
implicit def summon[F[_]: Functor: Unique]: UniqueFunctor[F] =
apply(implicitly, implicitly)
}

implicit object Ctx extends Context[VaultContext] {
type Key[A] = VaultContext.Key[A]
type KeyCreationBounds[F[_]] = UniqueFunctor[F]
type KeyTypeBounds[A] = context.Key.NoBounds[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
def uniqueKey[F[_]: UniqueFunctor, A: context.Key.NoBounds](
name: String
): F[Key[A]] = {
val uf = implicitly[UniqueFunctor[F]]
implicit def functor: Functor[F] = uf.functor
implicit def unique: Unique[F] = uf.unique
VaultContext.Key.unique(name)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ object Context {
object Key {
def unique[F[_]: Sync, A](name: String): F[Key[A]] =
Sync[F].delay(new Key(name))

implicit def provider[F[_]: Sync]: context.Key.Provider[F, Key] =
new context.Key.Provider[F, Key] {
def uniqueKey[A](name: String): F[Key[A]] = unique(name)
}
}

def wrap(context: JContext): Context = {
Expand All @@ -69,17 +74,11 @@ object Context {

implicit object Ctx extends context.Context[Context] {
type Key[A] = Context.Key[A]
type KeyCreationBounds[F[_]] = Sync[F]
type KeyTypeBounds[A] = context.Key.NoBounds[A]

def get[A](ctx: Context)(key: Key[A]): Option[A] =
ctx.get(key)
def updated[A](ctx: Context)(key: Key[A], value: A): Context =
ctx.updated(key, value)
def root: Context = Context.root
def uniqueKey[F[_]: Sync, A: context.Key.NoBounds](
name: String
): F[Key[A]] =
Context.Key.unique(name)
}
}

0 comments on commit 59dde4e

Please sign in to comment.