@@ -9,6 +9,8 @@ import Prelude.Compat
9
9
10
10
import Data.Function
11
11
(on )
12
+ import Data.List
13
+ (nub )
12
14
import Data.Map
13
15
(Map )
14
16
import qualified Data.Map as M
@@ -24,6 +26,21 @@ import Servant.Server.Internal.ServerError
24
26
25
27
type Router env = Router' env RoutingApplication
26
28
29
+ data CaptureHint = CaptureHint
30
+ { captureName :: Text
31
+ , captureType :: CaptureType
32
+ }
33
+ deriving (Show , Eq )
34
+
35
+ data CaptureType = CaptureList | CaptureSingle
36
+ deriving (Show , Eq )
37
+
38
+ toCaptureTag :: CaptureHint -> Text
39
+ toCaptureTag hint = captureName hint <> " ::" <> (T. pack . show ) (captureType hint)
40
+
41
+ toCaptureTags :: [CaptureHint ] -> Text
42
+ toCaptureTags hints = " <" <> T. intercalate " |" (map toCaptureTag hints) <> " >"
43
+
27
44
-- | Internal representation of a router.
28
45
--
29
46
-- The first argument describes an environment type that is
@@ -36,10 +53,10 @@ data Router' env a =
36
53
-- ^ the map contains routers for subpaths (first path component used
37
54
-- for lookup and removed afterwards), the list contains handlers
38
55
-- for the empty path, to be tried in order
39
- | CaptureRouter (Router' (Text , env ) a )
56
+ | CaptureRouter [ CaptureHint ] (Router' (Text , env ) a )
40
57
-- ^ first path component is passed to the child router in its
41
58
-- environment and removed afterwards
42
- | CaptureAllRouter (Router' ([Text ], env ) a )
59
+ | CaptureAllRouter [ CaptureHint ] (Router' ([Text ], env ) a )
43
60
-- ^ all path components are passed to the child router in its
44
61
-- environment and are removed afterwards
45
62
| RawRouter (env -> a )
@@ -69,8 +86,8 @@ leafRouter l = StaticRouter M.empty [l]
69
86
choice :: Router' env a -> Router' env a -> Router' env a
70
87
choice (StaticRouter table1 ls1) (StaticRouter table2 ls2) =
71
88
StaticRouter (M. unionWith choice table1 table2) (ls1 ++ ls2)
72
- choice (CaptureRouter router1) (CaptureRouter router2) =
73
- CaptureRouter (choice router1 router2)
89
+ choice (CaptureRouter hints1 router1) (CaptureRouter hints2 router2) =
90
+ CaptureRouter (nub $ hints1 ++ hints2) ( choice router1 router2)
74
91
choice router1 (Choice router2 router3) = Choice (choice router1 router2) router3
75
92
choice router1 router2 = Choice router1 router2
76
93
@@ -84,7 +101,7 @@ choice router1 router2 = Choice router1 router2
84
101
--
85
102
data RouterStructure =
86
103
StaticRouterStructure (Map Text RouterStructure ) Int
87
- | CaptureRouterStructure RouterStructure
104
+ | CaptureRouterStructure [ CaptureHint ] RouterStructure
88
105
| RawRouterStructure
89
106
| ChoiceStructure RouterStructure RouterStructure
90
107
deriving (Eq , Show )
@@ -98,11 +115,11 @@ data RouterStructure =
98
115
routerStructure :: Router' env a -> RouterStructure
99
116
routerStructure (StaticRouter m ls) =
100
117
StaticRouterStructure (fmap routerStructure m) (length ls)
101
- routerStructure (CaptureRouter router) =
102
- CaptureRouterStructure $
118
+ routerStructure (CaptureRouter hints router) =
119
+ CaptureRouterStructure hints $
103
120
routerStructure router
104
- routerStructure (CaptureAllRouter router) =
105
- CaptureRouterStructure $
121
+ routerStructure (CaptureAllRouter hints router) =
122
+ CaptureRouterStructure hints $
106
123
routerStructure router
107
124
routerStructure (RawRouter _) =
108
125
RawRouterStructure
@@ -111,11 +128,21 @@ routerStructure (Choice r1 r2) =
111
128
(routerStructure r1)
112
129
(routerStructure r2)
113
130
114
- -- | Compare the structure of two routers.
131
+ -- | Compare the structure of two routers. Ignores capture hints.
115
132
--
116
133
sameStructure :: Router' env a -> Router' env b -> Bool
117
- sameStructure r1 r2 =
118
- routerStructure r1 == routerStructure r2
134
+ sameStructure router1 router2 =
135
+ routerStructure router1 `almostEq` routerStructure router2
136
+ where
137
+ almostEq :: RouterStructure -> RouterStructure -> Bool
138
+ almostEq (StaticRouterStructure m1 l1) (StaticRouterStructure m2 l2) =
139
+ l1 == l2 && M. isSubmapOfBy almostEq m1 m2 && M. isSubmapOfBy almostEq m2 m1
140
+ almostEq (CaptureRouterStructure _ r1) (CaptureRouterStructure _ r2) =
141
+ r1 `almostEq` r2
142
+ almostEq RawRouterStructure RawRouterStructure = True
143
+ almostEq (ChoiceStructure r1 r1') (ChoiceStructure r2 r2') =
144
+ r1 `almostEq` r2 && r1' `almostEq` r2'
145
+ almostEq _ _ = False
119
146
120
147
-- | Provide a textual representation of the
121
148
-- structure of a router.
@@ -126,7 +153,8 @@ routerLayout router =
126
153
where
127
154
mkRouterLayout :: Bool -> RouterStructure -> [Text ]
128
155
mkRouterLayout c (StaticRouterStructure m n) = mkSubTrees c (M. toList m) n
129
- mkRouterLayout c (CaptureRouterStructure r) = mkSubTree c " <capture>" (mkRouterLayout False r)
156
+ mkRouterLayout c (CaptureRouterStructure hints r) =
157
+ mkSubTree c (toCaptureTags hints) (mkRouterLayout False r)
130
158
mkRouterLayout c RawRouterStructure =
131
159
if c then [" ├─ <raw>" ] else [" └─ <raw>" ]
132
160
mkRouterLayout c (ChoiceStructure r1 r2) =
@@ -169,15 +197,15 @@ runRouterEnv fmt router env request respond =
169
197
-> let request' = request { pathInfo = rest }
170
198
in runRouterEnv fmt router' env request' respond
171
199
_ -> respond $ Fail $ fmt request
172
- CaptureRouter router' ->
200
+ CaptureRouter _ router' ->
173
201
case pathInfo request of
174
202
[] -> respond $ Fail $ fmt request
175
203
-- This case is to handle trailing slashes.
176
204
[" " ] -> respond $ Fail $ fmt request
177
205
first : rest
178
206
-> let request' = request { pathInfo = rest }
179
207
in runRouterEnv fmt router' (first, env) request' respond
180
- CaptureAllRouter router' ->
208
+ CaptureAllRouter _ router' ->
181
209
let segments = pathInfo request
182
210
request' = request { pathInfo = [] }
183
211
in runRouterEnv fmt router' (segments, env) request' respond
0 commit comments