From 5229a9bfbbdefc4d4f688e96f1c5031798ce82ae Mon Sep 17 00:00:00 2001 From: Shockk Date: Thu, 28 Jan 2016 18:47:33 +0000 Subject: [PATCH] PathInfo: Write generalized instance for [a] This patch replaces the two specialized instances of PathInfo for [String] and [Text] with an instance for [a] for all types a. The generalized instance has identical behaviour for [String] and [Text] to the removed instances. For example, in combination with the web-routes-th package, the following data type is possible: data Route = Search | ViewSubTree [Integer] | NewLeaf [Integer] String This would match for the following URLs: /search /view-sub-tree///..etc.. /new-leaf///..etc../ --- Web/Routes/PathInfo.hs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) mode change 100644 => 100755 Web/Routes/PathInfo.hs diff --git a/Web/Routes/PathInfo.hs b/Web/Routes/PathInfo.hs old mode 100644 new mode 100755 index 2df2967..d22d027 --- a/Web/Routes/PathInfo.hs +++ b/Web/Routes/PathInfo.hs @@ -308,18 +308,10 @@ instance PathInfo Text where toPathSegments = (:[]) fromPathSegments = anySegment -instance PathInfo [Text] where - toPathSegments = id - fromPathSegments = many anySegment - instance PathInfo String where toPathSegments = (:[]) . pack fromPathSegments = unpack <$> anySegment -instance PathInfo [String] where - toPathSegments = id . map pack - fromPathSegments = many (unpack <$> anySegment) - instance PathInfo Int where toPathSegments i = [pack $ show i] fromPathSegments = pToken (const "Int") checkInt @@ -340,3 +332,7 @@ instance PathInfo Integer where | Text.null r -> Just n | otherwise -> Nothing + +instance PathInfo a => PathInfo [a] where + toPathSegments = concat . fmap toPathSegments + fromPathSegments = many fromPathSegments