-
Notifications
You must be signed in to change notification settings - Fork 332
Added rough readme for multiverb's AsUnion. #2554
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,84 @@ | ||
| # Multiverb | ||
|
|
||
| We offer the typeclass `AsUnion` to convert a handler return type to a union type | ||
| including all possible responses of a `MultiVerb` endpoint. | ||
|
|
||
| Any glue code necessary to convert application types to and from the | ||
| canonical `Union` type corresponding to a `MultiVerb` endpoint should be | ||
| packaged into an `AsUnion` instance. | ||
|
|
||
| When using flat sum types, you can use Generics to automatically derive this instance, | ||
| and for nested types, the following example code should help clarify usage. | ||
|
|
||
| It assumes some understanding of [Data.SOP](https://hackage.haskell.org/package/sop-core-0.5.0.2/docs/Data-SOP.html) | ||
|
|
||
| ```haskell | ||
| data Success = Success | ||
|
|
||
| data Failure | ||
| = InvalidEntry | ||
| | AccessDenied | ||
|
|
||
| -- We need a way to map errors to servant and swagger. | ||
| instance KnownError (MapError e) => IsSwaggerError (e :: Failure) where | ||
| addToSwagger = addStaticErrorToSwagger @(MapError e) | ||
|
|
||
| type instance MapError 'InvalidEntry = 'StaticError 400 "invalid-entry" "Invalid data entered" | ||
|
|
||
| type instance MapError 'AccessDenied = 'StaticError 403 "access-denied" "Access denied" | ||
|
|
||
| data FailureSuccess = SFFailure Failure | SFSuccess Success | ||
|
|
||
| sfToEither :: FailureSuccess -> Either Failure Success | ||
| sfToEither (SFFailure b) = Left b | ||
| sfToEither (SFSuccess d) = Right d | ||
|
|
||
| sfFromEither :: Either Failure Success -> FailureSuccess | ||
| sfFromEither (Left b) = SFFailure b | ||
| sfFromEither (Right d) = SFSuccess d | ||
|
|
||
| -- type instance ResponseType Failure = Failure | ||
| type instance ResponseType Success = Success | ||
|
|
||
| -- ErrorResponse offers facilities to create errors | ||
| type MyErrorResponses = | ||
| '[ ErrorResponse 'InvalidEntry, | ||
| ErrorResponse 'AccessDenied | ||
| ] | ||
|
|
||
| -- Responses is a list of errors and a list of success cases. | ||
| type MyResponses = | ||
| MyErrorResponses .++ '[Success] | ||
|
|
||
| accessDenied :: DynError | ||
| accessDenied = dynError @(MapError 'AccessDenied) | ||
|
|
||
| invalidEntry :: DynError | ||
| invalidEntry = dynError @(MapError 'InvalidEntry) | ||
|
|
||
| failToError :: Failure -> NS I (DynError : DynError : xs) | ||
| failToError = \case | ||
| -- Z . I wraps the first value in the error list. | ||
| -- They come from Data.SOP | ||
| InvalidEntry -> Z . I $ accessDenied | ||
| -- we wrap the value using I (identity), Z (zero) and S (successor) to indicate second item in the response list | ||
| AccessDenied -> S . Z . I $ invalidEntry | ||
|
|
||
| instance (res ~ MyResponses) => AsUnion res FailureSuccess where | ||
| toUnion = | ||
| -- Type application here tells the type engine that we use DynError from ErrorResponse | ||
| -- as the return type, and no Failure. | ||
| eitherToUnion @'[DynError, DynError] @'[Success] | ||
| failToError -- Maps a Failure case to its DynError equivalent | ||
| (Z . I) -- [Success] only has one item. | ||
| . sfToEither | ||
|
|
||
| fromUnion = | ||
| sfFromEither | ||
| . eitherFromUnion @'[DynError, DynError] @'[Success] | ||
| ( \case | ||
| Z _ -> InvalidEntry | ||
| S _ -> AccessDenied | ||
| ) | ||
| (unI . unZ) | ||
| ``` | ||
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.
Uh oh!
There was an error while loading. Please reload this page.