-
-
Notifications
You must be signed in to change notification settings - Fork 419
Display capture hints in router layout #1556
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
gdeest
merged 3 commits into
haskell-servant:master
from
nbacquey:router_layout_captures
Mar 25, 2022
Merged
Changes from all commits
Commits
Show all changes
3 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,81 @@ | ||
synopsis: Display capture hints in router layout | ||
prs: #1556 | ||
|
||
description: { | ||
|
||
This PR enhances the `Servant.Server.layout` function, which produces a textual description of the routing layout of an API. More precisely, it changes `<capture>` blocks, so that they display the name and type of the variable being captured instead. | ||
|
||
Example: | ||
|
||
For the following API | ||
```haskell | ||
type API = | ||
"a" :> "d" :> Get '[JSON] NoContent | ||
:<|> "b" :> Capture "x" Int :> Get '[JSON] Bool | ||
:<|> "a" :> "e" :> Get '[JSON] Int | ||
``` | ||
|
||
we previously got the following output: | ||
|
||
``` | ||
/ | ||
├─ a/ | ||
│ ├─ d/ | ||
│ │ └─• | ||
│ └─ e/ | ||
│ └─• | ||
└─ b/ | ||
└─ <capture>/ | ||
├─• | ||
┆ | ||
└─• | ||
``` | ||
|
||
now we get: | ||
|
||
``` | ||
/ | ||
├─ a/ | ||
│ ├─ d/ | ||
│ │ └─• | ||
│ └─ e/ | ||
│ └─• | ||
└─ b/ | ||
└─ <x::Int>/ | ||
├─• | ||
┆ | ||
└─• | ||
``` | ||
|
||
This change is achieved by the introduction of a CaptureHint type, which is passed as an extra argument to the CaptureRouter and CaptureAllRouter constructors for the Router' type. | ||
CaptureHint values are then used in routerLayout, to display the name and type of captured values, instead of just `<capture>` previously. | ||
|
||
N.B.: | ||
Because the choice smart constructor for routers can aggregate Capture combinators with different capture hints, the Capture*Router constructors actually take a list of CaptureHint, instead of a single one. | ||
|
||
This PR also introduces Spec tests for the routerLayout function. | ||
|
||
Warning: | ||
This change is potentially breaking, because it adds the constraint `Typeable a` to all types that are to be captured. Because all types are typeable since GHC 7.10, this is not as bad as it sounds ; it only break expressions where `a` is quantified in an expression with `Capture a`. | ||
In those cases, the fix is easy: it suffices to add `Typeable a` to the left-hand side of the quantification constraint. | ||
|
||
For instance, the following code will no longer compile: | ||
```haskell | ||
type MyAPI a = Capture "foo" a :> Get '[JSON] () | ||
|
||
myServer :: forall a. Server (MyAPI a) | ||
myServer = const $ return () | ||
|
||
myApi :: forall a. Proxy (MyAPI a) | ||
myApi = Proxy | ||
|
||
app :: forall a. (FromHttpApiData a) => Application | ||
app = serve (myApi @a) (myServer @a) | ||
``` | ||
|
||
Indeed, `app` should be replaced with: | ||
```haskell | ||
app :: forall a. (FromHttpApiData a, Typeable a) => Application | ||
app = serve (myApi @a) (myServer @a) | ||
``` | ||
} |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.