Special support for key-value pairs in route attributes #762
This commit is contained in:
parent
4858f0837b
commit
4c31714a25
@ -19,6 +19,8 @@ import qualified System.IO as SIO
|
|||||||
import Yesod.Routes.TH
|
import Yesod.Routes.TH
|
||||||
import Yesod.Routes.Overlap (findOverlapNames)
|
import Yesod.Routes.Overlap (findOverlapNames)
|
||||||
import Data.List (foldl')
|
import Data.List (foldl')
|
||||||
|
import Data.Maybe (mapMaybe)
|
||||||
|
import qualified Data.Set as Set
|
||||||
|
|
||||||
-- | A quasi-quoter to parse a string into a list of 'Resource's. Checks for
|
-- | A quasi-quoter to parse a string into a list of 'Resource's. Checks for
|
||||||
-- overlapping routes, failing if present; use 'parseRoutesNoCheck' to skip the
|
-- overlapping routes, failing if present; use 'parseRoutesNoCheck' to skip the
|
||||||
@ -107,7 +109,21 @@ addAttrs attrs =
|
|||||||
goTree (ResourceLeaf res) = ResourceLeaf (goRes res)
|
goTree (ResourceLeaf res) = ResourceLeaf (goRes res)
|
||||||
goTree (ResourceParent x y z) = ResourceParent x y (map goTree z)
|
goTree (ResourceParent x y z) = ResourceParent x y (map goTree z)
|
||||||
|
|
||||||
goRes res = res { resourceAttrs = attrs ++ resourceAttrs res }
|
goRes res =
|
||||||
|
res { resourceAttrs = noDupes ++ resourceAttrs res }
|
||||||
|
where
|
||||||
|
usedKeys = Set.fromList $ map fst $ mapMaybe toPair $ resourceAttrs res
|
||||||
|
used attr =
|
||||||
|
case toPair attr of
|
||||||
|
Nothing -> False
|
||||||
|
Just (key, _) -> key `Set.member` usedKeys
|
||||||
|
noDupes = filter (not . used) attrs
|
||||||
|
|
||||||
|
toPair s =
|
||||||
|
case break (== '=') s of
|
||||||
|
(x, '=':y) -> Just (x, y)
|
||||||
|
_ -> Nothing
|
||||||
|
|
||||||
|
|
||||||
-- | Take attributes out of the list and put them in the first slot in the
|
-- | Take attributes out of the list and put them in the first slot in the
|
||||||
-- result tuple.
|
-- result tuple.
|
||||||
|
|||||||
@ -99,8 +99,8 @@ do
|
|||||||
/post Post3 POST
|
/post Post3 POST
|
||||||
-- /#Int Delete3 DELETE
|
-- /#Int Delete3 DELETE
|
||||||
|
|
||||||
/afterwards AfterR:
|
/afterwards AfterR !parent !key=value1:
|
||||||
/ After GET
|
/ After GET !child !key=value2
|
||||||
|
|
||||||
-- /trailing-nest TrailingNestR:
|
-- /trailing-nest TrailingNestR:
|
||||||
-- /foo TrailingFooR GET
|
-- /foo TrailingFooR GET
|
||||||
@ -204,3 +204,5 @@ hierarchy = describe "hierarchy" $ do
|
|||||||
parseRoute (["admin!", "5"], []) @?= (Nothing :: Maybe (Route Hierarchy))
|
parseRoute (["admin!", "5"], []) @?= (Nothing :: Maybe (Route Hierarchy))
|
||||||
it "inherited attributes" $ do
|
it "inherited attributes" $ do
|
||||||
routeAttrs (NestR SpacedR) @?= Set.fromList ["NestingAttr", "NonNested"]
|
routeAttrs (NestR SpacedR) @?= Set.fromList ["NestingAttr", "NonNested"]
|
||||||
|
it "pair attributes" $
|
||||||
|
routeAttrs (AfterR After) @?= Set.fromList ["parent", "child", "key=value2"]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user