-
Notifications
You must be signed in to change notification settings - Fork 76
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
Adds Handlers constructs and functions #1522
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec083ba
Adds Handlers constructs and functions
Baccata 303a8da
Correct documentation
Baccata 22fee7d
Make 2.12 happy
Baccata 19df39f
Merge branch 'series/0.18' into handlers
kubukoz a9ab4b1
add links to PRs
kubukoz 103981e
Update modules/core/src/smithy4s/EndpointHandler.scala
Baccata e813d05
Rename orElse to or
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package smithy4s.other | ||
|
||
import munit.FunSuite | ||
import smithy4s.example.KVStoreOperation | ||
import cats.Id | ||
import java.util.concurrent.atomic.AtomicReference | ||
import smithy4s.example.Value | ||
import smithy4s.example.KVStore | ||
import smithy4s.example.Key | ||
|
||
class HandlerSpec extends FunSuite { | ||
|
||
def get(ref: AtomicReference[Map[String, String]]) = | ||
KVStoreOperation.Get.handler[Id] { input => | ||
Value(ref.get().get(input.key).getOrElse("unknown")) | ||
} | ||
|
||
def put(ref: AtomicReference[Map[String, String]]) = | ||
KVStoreOperation.Put.handler[Id] { input => | ||
val _ = ref.updateAndGet(_ + (input.key -> input.value)) | ||
} | ||
|
||
// Handlers can also be created by inheriting from a class. | ||
case class delete(ref: AtomicReference[Map[String, String]]) | ||
extends KVStoreOperation.Delete.Handler[Id] { | ||
def run(input: Key): Id[Unit] = { | ||
val _ = ref.updateAndGet(_ - input.key) | ||
} | ||
} | ||
|
||
test("Handler composition: orElse ") { | ||
|
||
val ref = new AtomicReference(Map.empty[String, String]) | ||
val kvStore: KVStore[Id] = get(ref) | ||
.orElse(put(ref)) | ||
.orElse(delete(ref)) | ||
.asService(KVStore) | ||
.throwing | ||
|
||
kvStore.put("a", "b") | ||
val b = kvStore.get("a").value | ||
kvStore.delete("a") | ||
|
||
assertEquals(b, "b") | ||
assertEquals(ref.get(), Map.empty[String, String]) | ||
} | ||
|
||
test("Handler composition: combineAll ") { | ||
val ref = new AtomicReference(Map.empty[String, String]) | ||
val kvStore: KVStore[Id] = KVStore | ||
.fromFunctorHandlers[Id]( | ||
get(ref), | ||
put(ref), | ||
delete(ref) | ||
) | ||
.throwing | ||
|
||
kvStore.put("a", "b") | ||
val b = kvStore.get("a").value | ||
kvStore.delete("a") | ||
|
||
assertEquals(b, "b") | ||
assertEquals(ref.get(), Map.empty[String, String]) | ||
} | ||
|
||
} |
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,135 @@ | ||
/* | ||
* Copyright 2021-2024 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 smithy4s.kinds.PolyFunction5 | ||
import smithy4s.kinds.Kind5 | ||
import smithy4s.EndpointHandler.AsService | ||
|
||
/** | ||
* Composable handler that allows to implement a specific endpoint in isolation. | ||
* | ||
* Handlers are composable and can be reconciled into the service the operations belong to. | ||
*/ | ||
trait EndpointHandler[Op[_, _, _, _, _], F[_, _, _, _, _]] { | ||
import EndpointHandler.Combined | ||
|
||
protected[smithy4s] def lift[Alg[_[_, _, _, _, _]]]( | ||
service: Service.Aux[Alg, Op] | ||
): PolyFunction5[Op, Kind5[F]#optional] | ||
|
||
final def asService[Alg[_[_, _, _, _, _]]]( | ||
service: Service.Aux[Alg, Op] | ||
): AsService[Alg, F] = | ||
new EndpointHandler.AsServiceImpl[Alg, Op, F](this, service) | ||
|
||
final def orElse(other: EndpointHandler[Op, F]): EndpointHandler[Op, F] = | ||
(this, other) match { | ||
case (Combined(left), Combined(right)) => Combined(left ++ right) | ||
case (other, Combined(right)) => Combined(other +: right) | ||
case (Combined(left), other) => Combined(left :+ other) | ||
case (left, right) => Combined(Vector(left, right)) | ||
} | ||
} | ||
|
||
// scalafmt: { maxColumn = 120 } | ||
object EndpointHandler { | ||
|
||
/** | ||
* Partial step when handlers are transformed into a service, allowing them to decide how to handle | ||
* un-implemented endpoints. | ||
*/ | ||
trait AsService[Alg[_[_, _, _, _, _]], F[_, _, _, _, _]] { | ||
|
||
/** | ||
* Returns an instance of the algebra that throws when one of the methods doesn't have a matching endpoint | ||
* handler | ||
*/ | ||
def throwing: Alg[F] | ||
|
||
/** | ||
* Returns an instance of the algebra that raises an error in an effect when one of the methods doesn't have a matching | ||
* endpoint handler | ||
*/ | ||
def failingWith(f: ShapeId => F[Any, Nothing, Nothing, Nothing, Nothing]): Alg[F] | ||
|
||
/** | ||
* Returns an instance of the algebra that wraps implemented methods in `Some` and return `None` on unimplemented methods | ||
*/ | ||
def partial: Alg[Kind5[F]#optional] | ||
} | ||
|
||
private class AsServiceImpl[Alg[_[_, _, _, _, _]], Op[_, _, _, _, _], F[_, _, _, _, _]]( | ||
handler: EndpointHandler[Op, F], | ||
service: Service.Aux[Alg, Op] | ||
) extends AsService[Alg, F] { | ||
def throwing: Alg[F] = { | ||
val lifted = handler.lift(service) | ||
val interpreter = new PolyFunction5[Op, F] { | ||
def apply[I, E, O, SI, SO](op: Op[I, E, O, SI, SO]) = | ||
lifted(op).getOrElse { | ||
val endpointId = service.endpoint(op).id | ||
throw new NotImplementedError(endpointId.show) | ||
} | ||
} | ||
service.fromPolyFunction(interpreter) | ||
} | ||
|
||
def failingWith(f: ShapeId => F[Any, Nothing, Nothing, Nothing, Nothing]): Alg[F] = { | ||
val lifted = handler.lift(service) | ||
val interpreter = new PolyFunction5[Op, F] { | ||
def apply[I, E, O, SI, SO](op: Op[I, E, O, SI, SO]) = | ||
lifted(op).getOrElse { | ||
val endpointId = service.endpoint(op).id | ||
f(endpointId).asInstanceOf[F[I, E, O, SI, SO]] | ||
} | ||
} | ||
service.fromPolyFunction(interpreter) | ||
} | ||
|
||
def partial: Alg[Kind5[F]#optional] = | ||
service.fromPolyFunction[Kind5[F]#optional](handler.lift(service)) | ||
} | ||
|
||
private[smithy4s] def combineAll[Op[_, _, _, _, _], F[_, _, _, _, _]]( | ||
handlers: EndpointHandler[Op, F]* | ||
): EndpointHandler[Op, F] = | ||
Combined(handlers.toVector) | ||
Baccata marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private case class Combined[Op[_, _, _, _, _], F[_, _, _, _, _]]( | ||
handlers: Vector[EndpointHandler[Op, F]] | ||
) extends EndpointHandler[Op, F] { | ||
protected[smithy4s] def lift[Alg[_[_, _, _, _, _]]]( | ||
service: Service.Aux[Alg, Op] | ||
): PolyFunction5[Op, Kind5[F]#optional] = | ||
new PolyFunction5[Op, Kind5[F]#optional] { | ||
val lifted = handlers.map(_.lift(service)) | ||
|
||
def apply[I, E, O, SI, SO]( | ||
op: Op[I, E, O, SI, SO] | ||
): Option[F[I, E, O, SI, SO]] = { | ||
var result: Option[F[I, E, O, SI, SO]] = None | ||
var i = 0 | ||
while (result == None && i < lifted.size) { | ||
result = lifted(i)(op) | ||
i += 1 | ||
} | ||
result | ||
} | ||
} | ||
} | ||
} |
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.
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.
nitpick:
orElse
is used in cats/CE for error handling and could be confusing, perhaps we can find a name that isn't, e.g. justor
?other ideas off the top of my head:
merge
,add
,combine
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.
e813d05