Skip to content

Commit b8d3be4

Browse files
committed
Router Support [UUID]
1 parent 1eca3f3 commit b8d3be4

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

IHP/RouterSupport.hs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ parseFuncs parseIdType = [
179179
Nothing -> Right []
180180
Nothing -> Left NotMatched,
181181

182+
-- Try and parse a raw [UUID]
183+
\queryValue -> case eqT :: Maybe (d :~: [UUID]) of
184+
Just Refl -> case queryValue of
185+
Just queryValue -> queryValue
186+
|> cs
187+
|> Text.splitOn ","
188+
|> map (fromASCIIBytes . cs)
189+
|> catMaybes
190+
|> Right
191+
Nothing -> Right []
192+
Nothing -> Left NotMatched,
193+
182194
-- Try and parse a raw UUID
183195
\queryValue -> case eqT :: Maybe (d :~: UUID) of
184196
Just Refl -> case queryValue of
@@ -618,12 +630,12 @@ post path action = do
618630
-- >
619631
-- > updateRecordAction = do
620632
-- > onlyAllowMethods [PATCH]
621-
-- >
633+
-- >
622634
-- > table <- parseText
623635
-- > string "/"
624636
-- > id <- parseUUID
625637
-- > pure UpdateRecordAction { table, id }
626-
-- >
638+
-- >
627639
-- > createRecordAction <|> updateRecordAction
628640
--
629641
onlyAllowMethods :: (?context :: RequestContext) => [StdMethod] -> Parser ()
@@ -800,4 +812,4 @@ parseIntegerId queryVal = let
800812
--
801813
-- See https://forum.ihpapp.com/ShowThread?threadId=ad73d6a5-2481-4e2f-af46-9bf8849f998b
802814
-- See https://github.com/digitallyinduced/ihp/issues/840
803-
instance ((T.TypeError (T.Text "Looks like you forgot to pass a " :<>: (T.ShowType argument) :<>: T.Text " to this " :<>: (T.ShowType controller))), Data argument, Data controller) => AutoRoute (argument -> controller) where
815+
instance ((T.TypeError (T.Text "Looks like you forgot to pass a " :<>: (T.ShowType argument) :<>: T.Text " to this " :<>: (T.ShowType controller))), Data argument, Data controller) => AutoRoute (argument -> controller) where

Test/RouterSupportSpec.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ data TestController
6565
| TestInteger { p1 :: Integer, p2 :: Maybe Integer, p3 :: [Integer] }
6666
| TestIntegerId { integerId :: Id Band }
6767
| TestUUIDId { uuidId :: Id Performance }
68+
| TestUUIDList { uuidList :: [UUID] }
6869
deriving (Eq, Show, Data)
6970

7071
instance Controller TestController where
@@ -103,6 +104,8 @@ instance Controller TestController where
103104
renderPlain (cs $ ClassyPrelude.show integerId)
104105
action TestUUIDId { .. } = do
105106
renderPlain (cs $ ClassyPrelude.show uuidId)
107+
action TestUUIDList { .. } = do
108+
renderPlain $ cs $ ClassyPrelude.show uuidList
106109

107110
instance AutoRoute TestController where
108111
autoRoute = autoRouteWithIdType (parseIntegerId @(Id Band))
@@ -186,6 +189,14 @@ tests = beforeAll (mockContextNoDatabase WebApplication config) do
186189
runSession (testGet "test/TestIntegerId?integerId=123") Server.application >>= assertSuccess "123"
187190
it "parses Id with UUID param" $ withContext do
188191
runSession (testGet "test/TestUUIDId?uuidId=8dd57d19-490a-4323-8b94-6081ab93bf34") Server.application >>= assertSuccess "8dd57d19-490a-4323-8b94-6081ab93bf34"
192+
it "parses [UUID] param: empty" $ withContext do
193+
runSession (testGet "test/TestUUIDList") Server.application >>= assertSuccess "[]"
194+
it "parses [UUID] param: one element" $ withContext do
195+
runSession (testGet "test/TestUUIDList?uuidList=8dd57d19-490a-4323-8b94-6081ab93bf34") Server.application >>= assertSuccess "[8dd57d19-490a-4323-8b94-6081ab93bf34]"
196+
it "parses [UUID] param: multiple elements" $ withContext do
197+
runSession (testGet "test/TestUUIDList?uuidList=8dd57d19-490a-4323-8b94-6081ab93bf34,8dd57d19-490a-4323-8b94-6081ab93bf34") Server.application >>= assertSuccess "[8dd57d19-490a-4323-8b94-6081ab93bf34,8dd57d19-490a-4323-8b94-6081ab93bf34]"
198+
it "parses [UUID] param: multiple elements, ignoring non UUID" $ withContext do
199+
runSession (testGet "test/TestUUIDList?uuidList=8dd57d19-490a-4323-8b94-6081ab93bf34,423423432432432") Server.application >>= assertSuccess "[8dd57d19-490a-4323-8b94-6081ab93bf34]"
189200
describe "pathTo" $ do
190201
it "generates correct path for empty route" $ withContext do
191202
pathTo TestAction `shouldBe` "/test/Test"

0 commit comments

Comments
 (0)