-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add error-related transformations + documentation #584
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f313aa0
Prepare transformation documentation
Baccata d44e82d
Merge branch 'change-service-encoding' into transformation-documentation
Baccata a6bb624
Merge remote-tracking branch 'origin/series/0.17' into transformation…
Baccata ae72f43
Clean-up
Baccata 837835f
Make 2.12 happy
Baccata d395e59
Fix dynamic module
Baccata 5017f06
Fix code highlight
kubukoz bd98c94
Addressing comments
Baccata ec29686
Force scala-xml version
Baccata 1036c62
Merge remote-tracking branch 'origin/series/0.17' into transformation…
Baccata 99cc325
headers
Baccata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,120 @@ | ||
/* | ||
* Copyright 2021-2022 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* 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 smithy4s | ||
|
||
import kinds._ | ||
|
||
/** | ||
* Heterogenous function construct, allows to abstract over various kinds of functions | ||
* whilst providing an homogenous user experience without the user having to | ||
* manually lift functions from one kind to the other. | ||
* | ||
*{{{ | ||
* // assuming Foo is a code-generated interface | ||
* val fooOption : Foo[Option] = ??? | ||
* val toList = new smithy4s.PolyFunction[Option, List]{def apply[A](fa: Option[A]): List[A] = fa.toList} | ||
* val fooList : Foo[List] = foo.transform(toList) | ||
*}}} | ||
* | ||
* It is possible to plug arbitrary transformations to mechanism, such as `cats.arrow.FunctionK` | ||
*/ | ||
trait Transformation[Func, Input, Output] { | ||
def apply(f: Func, input: Input): Output | ||
} | ||
|
||
object Transformation { | ||
|
||
/** | ||
* A transformation that turns a monofunctor algebra into a bifunctor algebra by lifting the known errors in the | ||
* returned types of the operations of the algebra. | ||
*/ | ||
trait SurfaceError[F[_], G[_, _]] { | ||
def apply[E, A](fa: F[A], projectError: Throwable => Option[E]): G[E, A] | ||
} | ||
|
||
/** | ||
* A transformation that turns a bifunctor algebra into a monofunctor algebra by absorbing known errors in a | ||
* generic error channel that handles throwables. | ||
*/ | ||
trait AbsorbError[F[_, _], G[_]] { | ||
def apply[E, A](fa: F[E, A], injectError: E => Throwable): G[A] | ||
} | ||
|
||
/** | ||
* Partially applied transformation, can be used to create methods/extensions that allow for a reasonable UX. | ||
*/ | ||
class PartiallyApplied[Input](input: Input) { | ||
def apply[Func, Output](func: Func)(implicit | ||
transform: Transformation[Func, Input, Output] | ||
) = transform(func, input) | ||
} | ||
|
||
// format: off | ||
implicit def functorK5_poly1_transformation[Alg[_[_, _, _, _, _]]: FunctorK5, F[_], G[_]]: Transformation[PolyFunction[F, G], FunctorAlgebra[Alg, F], FunctorAlgebra[Alg, G]] = | ||
new Transformation[PolyFunction[F, G], FunctorAlgebra[Alg, F], FunctorAlgebra[Alg, G]]{ | ||
def apply(func: PolyFunction[F, G], algF: FunctorAlgebra[Alg, F]) : FunctorAlgebra[Alg, G] = FunctorK5[Alg].mapK5[Kind1[F]#toKind5, Kind1[G]#toKind5](algF, toPolyFunction5(func)) | ||
} | ||
|
||
implicit def functorK5_poly2_transformation[Alg[_[_, _, _, _, _]]: FunctorK5, F[_,_], G[_, _]]: Transformation[PolyFunction2[F, G], BiFunctorAlgebra[Alg, F], BiFunctorAlgebra[Alg, G]] = | ||
new Transformation[PolyFunction2[F, G], BiFunctorAlgebra[Alg, F], BiFunctorAlgebra[Alg, G]]{ | ||
def apply(func: PolyFunction2[F, G], algF: BiFunctorAlgebra[Alg, F]) : BiFunctorAlgebra[Alg, G] = FunctorK5[Alg].mapK5[Kind2[F]#toKind5, Kind2[G]#toKind5](algF, toPolyFunction5(func)) | ||
} | ||
|
||
|
||
|
||
|
||
implicit def service_surfaceError_transformation[Alg[_[_, _, _, _, _]], F[_], G[_, _]](implicit service: Service[Alg]): Transformation[SurfaceError[F, G], FunctorAlgebra[Alg, F], BiFunctorAlgebra[Alg, G]] = | ||
new Transformation[SurfaceError[F, G], FunctorAlgebra[Alg, F], BiFunctorAlgebra[Alg, G]]{ | ||
|
||
def apply(func: SurfaceError[F, G], algF: FunctorAlgebra[Alg, F]) : BiFunctorAlgebra[Alg, G] = { | ||
val polyFunction = service.toPolyFunction[Kind1[F]#toKind5](algF) | ||
val interpreter = new PolyFunction5[service.Operation, Kind2[G]#toKind5]{ | ||
def apply[I, E, O, SI, SO](op: service.Operation[I, E, O, SI, SO]): G[E,O] = { | ||
val endpoint = service.opToEndpoint(op) | ||
val catcher : Throwable => Option[E] = endpoint.errorable match { | ||
case None => PartialFunction.empty[Throwable, Option[E]] | ||
case Some(value) => value.liftError(_) | ||
} | ||
func.apply(polyFunction(op), catcher) | ||
} | ||
} | ||
service.fromPolyFunction[Kind2[G]#toKind5](interpreter) | ||
} | ||
} | ||
|
||
implicit def service_absorbError_transformation[Alg[_[_, _, _, _, _]], F[_, _], G[_]](implicit service: Service[Alg]): Transformation[AbsorbError[F, G], BiFunctorAlgebra[Alg, F], FunctorAlgebra[Alg, G]] = | ||
new Transformation[AbsorbError[F, G], BiFunctorAlgebra[Alg, F], FunctorAlgebra[Alg, G]]{ | ||
|
||
def apply(func: AbsorbError[F, G], algF: BiFunctorAlgebra[Alg, F]) : FunctorAlgebra[Alg, G] = { | ||
val polyFunction = service.toPolyFunction[Kind2[F]#toKind5](algF) | ||
val interpreter = new PolyFunction5[service.Operation, Kind1[G]#toKind5]{ | ||
def apply[I, E, O, SI, SO](op: service.Operation[I, E, O, SI, SO]): G[O] = { | ||
val endpoint = service.opToEndpoint(op) | ||
val thrower: E => Throwable = endpoint.errorable match { | ||
case None => | ||
// This case should not happen, as an endpoint without an errorable means the operation's error type is `Nothing` | ||
_ => new RuntimeException("Error coercion problem") | ||
case Some(value) => value.unliftError(_) | ||
} | ||
func.apply(polyFunction(op), thrower) | ||
} | ||
} | ||
service.fromPolyFunction[Kind1[G]#toKind5](interpreter) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks too familiar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because it's also in the SBT plugin built by this project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep