Skip to content

Commit 6392dce

Browse files
authored
Document CaptureHint in Capture[All]Router (#1634)
Co-authored-by: Nicolas BACQUEY <[email protected]>
1 parent 8f081bd commit 6392dce

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

servant-server/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Compatibility with GHC 9.4, see [PR #1592](https://github.com/haskell-servant/se
1313

1414
- Add `MonadFail` instance for `Handler` wrt [#1545](https://github.com/haskell-servant/servant/issues/1545)
1515
- Support GHC 9.2 [#1525](https://github.com/haskell-servant/servant/issues/1525)
16+
- Add capture hints in `Router` type for debug and display purposes [PR #1556] (https://github.com/haskell-servant/servant/pull/1556)
1617

1718
0.19
1819
----

servant-server/src/Servant/Server/Internal/Router.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ import Servant.Server.Internal.ServerError
2828

2929
type Router env = Router' env RoutingApplication
3030

31+
-- | Holds information about pieces of url that are captured as variables.
3132
data CaptureHint = CaptureHint
3233
{ captureName :: Text
34+
-- ^ Holds the name of the captured variable
3335
, captureType :: TypeRep
36+
-- ^ Holds the type of the captured variable
3437
}
3538
deriving (Show, Eq)
3639

@@ -54,10 +57,21 @@ data Router' env a =
5457
-- for the empty path, to be tried in order
5558
| CaptureRouter [CaptureHint] (Router' (Text, env) a)
5659
-- ^ first path component is passed to the child router in its
57-
-- environment and removed afterwards
60+
-- environment and removed afterwards.
61+
-- The first argument is a list of hints for all variables that can be
62+
-- captured by the router. The fact that it is a list is counter-intuitive,
63+
-- because the 'Capture' combinator only allows to capture a single varible,
64+
-- with a single name and a single type. However, the 'choice' smart
65+
-- constructor may merge two 'Capture' combinators with different hints, thus
66+
-- forcing the type to be '[CaptureHint]'.
67+
-- Because 'CaptureRouter' is built from a 'Capture' combinator, the list of
68+
-- hints should always be non-empty.
5869
| CaptureAllRouter [CaptureHint] (Router' ([Text], env) a)
5970
-- ^ all path components are passed to the child router in its
6071
-- environment and are removed afterwards
72+
-- The first argument is a hint for the list of variables that can be
73+
-- captured by the router. Note that the 'captureType' field of the hint
74+
-- should always be '[a]' for some 'a'.
6175
| RawRouter (env -> a)
6276
-- ^ to be used for routes we do not know anything about
6377
| Choice (Router' env a) (Router' env a)
@@ -101,6 +115,10 @@ choice router1 router2 = Choice router1 router2
101115
data RouterStructure =
102116
StaticRouterStructure (Map Text RouterStructure) Int
103117
| CaptureRouterStructure [CaptureHint] RouterStructure
118+
-- ^ The first argument holds information about variables
119+
-- that are captured by the router. There may be several hints
120+
-- if several routers have been aggregated by the 'choice'
121+
-- smart constructor.
104122
| RawRouterStructure
105123
| ChoiceStructure RouterStructure RouterStructure
106124
deriving (Eq, Show)

0 commit comments

Comments
 (0)