-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
mapK
to Tracer
, SpanBuilder
, etc.
Add `mapK` to `Tracer`, `Tracer.Meta`, `SpanBuilder`, `SpanOps`, `Span`, `Span.Backend`, and `InstrumentMeta`. Add `KindTransformer` type used by `mapK` implementation, allowing for a single `mapK` implementation that supports many transformation target types (`OptionT`, `EitherT`, `IorT`, `StateT`, `Kliesli`, `Resource`) with very little work needed to add additional or custom ones.
- Loading branch information
Showing
7 changed files
with
569 additions
and
4 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
core/common/src/main/scala/org/typelevel/otel4s/KindTransformer.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,123 @@ | ||
/* | ||
* 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 | ||
|
||
import cats.Applicative | ||
import cats.Functor | ||
import cats.Monad | ||
import cats.data.EitherT | ||
import cats.data.IorT | ||
import cats.data.Kleisli | ||
import cats.data.Nested | ||
import cats.data.OptionT | ||
import cats.data.StateT | ||
import cats.effect.MonadCancelThrow | ||
import cats.effect.Resource | ||
import cats.implicits.toFunctorOps | ||
import cats.syntax.all._ | ||
import cats.~> | ||
|
||
/** A utility for transforming the higher-kinded type `F` to another | ||
* higher-kinded type `G`. | ||
*/ | ||
@annotation.implicitNotFound("No transformer defined from ${F} to ${G}") | ||
trait KindTransformer[F[_], G[_]] { | ||
|
||
/** A higher-kinded function that lifts the kind `F` into a `G`. | ||
* | ||
* @note | ||
* This method is usually best implemented by a `liftK` method on `G`'s | ||
* companion object. | ||
*/ | ||
val liftK: F ~> G | ||
|
||
/** Modify the context of `G[A]` using the natural transformation `f`. | ||
* | ||
* This method is "limited" in the sense that while most `mapK` methods can | ||
* modify the context using arbitrary transformations, this method can only | ||
* modify the context using natural transformations. | ||
* | ||
* @note | ||
* This method is usually best implemented by a `mapK` method on `G`. | ||
*/ | ||
def limitedMapK[A](ga: G[A])(f: F ~> F): G[A] | ||
|
||
/** Lifts a natural transformation from `F` to `F` into a natural | ||
* transformation from `G` to `G`. | ||
*/ | ||
final def liftFunctionK(f: F ~> F): G ~> G = | ||
new (G ~> G) { | ||
def apply[A](ga: G[A]): G[A] = limitedMapK(ga)(f) | ||
} | ||
} | ||
|
||
object KindTransformer { | ||
implicit def optionT[F[_]: Functor]: KindTransformer[F, OptionT[F, *]] = | ||
new KindTransformer[F, OptionT[F, *]] { | ||
val liftK: F ~> OptionT[F, *] = OptionT.liftK | ||
def limitedMapK[A](ga: OptionT[F, A])(f: F ~> F): OptionT[F, A] = | ||
ga.mapK(f) | ||
} | ||
|
||
implicit def eitherT[F[_]: Functor, L]: KindTransformer[F, EitherT[F, L, *]] = | ||
new KindTransformer[F, EitherT[F, L, *]] { | ||
val liftK: F ~> EitherT[F, L, *] = EitherT.liftK | ||
def limitedMapK[R](ga: EitherT[F, L, R])(f: F ~> F): EitherT[F, L, R] = | ||
ga.mapK(f) | ||
} | ||
|
||
implicit def iorT[F[_]: Functor, L]: KindTransformer[F, IorT[F, L, *]] = | ||
new KindTransformer[F, IorT[F, L, *]] { | ||
val liftK: F ~> IorT[F, L, *] = IorT.liftK | ||
def limitedMapK[R](ga: IorT[F, L, R])(f: F ~> F): IorT[F, L, R] = | ||
ga.mapK(f) | ||
} | ||
|
||
implicit def kleisli[F[_], A]: KindTransformer[F, Kleisli[F, A, *]] = | ||
new KindTransformer[F, Kleisli[F, A, *]] { | ||
val liftK: F ~> Kleisli[F, A, *] = Kleisli.liftK | ||
def limitedMapK[B](ga: Kleisli[F, A, B])(f: F ~> F): Kleisli[F, A, B] = | ||
ga.mapK(f) | ||
} | ||
|
||
implicit def stateT[F[_]: Monad, S]: KindTransformer[F, StateT[F, S, *]] = | ||
new KindTransformer[F, StateT[F, S, *]] { | ||
val liftK: F ~> StateT[F, S, *] = StateT.liftK | ||
def limitedMapK[A](ga: StateT[F, S, A])(f: F ~> F): StateT[F, S, A] = | ||
ga.mapK(f) | ||
} | ||
|
||
implicit def resource[F[_]: MonadCancelThrow] | ||
: KindTransformer[F, Resource[F, *]] = | ||
new KindTransformer[F, Resource[F, *]] { | ||
val liftK: F ~> Resource[F, *] = Resource.liftK | ||
def limitedMapK[A](ga: Resource[F, A])(f: F ~> F): Resource[F, A] = | ||
ga.mapK(f) | ||
} | ||
|
||
implicit def nested[F[_]: Functor, G[_]: Applicative] | ||
: KindTransformer[F, Nested[F, G, *]] = | ||
new KindTransformer[F, Nested[F, G, *]] { | ||
val liftK: F ~> Nested[F, G, *] = | ||
new (F ~> Nested[F, G, *]) { | ||
def apply[A](fa: F[A]): Nested[F, G, A] = | ||
fa.map(_.pure[G]).nested | ||
} | ||
def limitedMapK[A](ga: Nested[F, G, A])(f: F ~> F): Nested[F, G, A] = | ||
ga.mapK(f) | ||
} | ||
} |
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
Oops, something went wrong.