web-routes-th-0.22.8.1/0000755000000000000000000000000007346545000012640 5ustar0000000000000000web-routes-th-0.22.8.1/LICENSE0000644000000000000000000000275707346545000013660 0ustar0000000000000000Copyright (c)2010, Jeremy Shaw All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Jeremy Shaw nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. web-routes-th-0.22.8.1/Setup.hs0000644000000000000000000000005607346545000014275 0ustar0000000000000000import Distribution.Simple main = defaultMain web-routes-th-0.22.8.1/Web/Routes/0000755000000000000000000000000007346545000014636 5ustar0000000000000000web-routes-th-0.22.8.1/Web/Routes/TH.hs0000644000000000000000000001461607346545000015515 0ustar0000000000000000{-# LANGUAGE CPP, TemplateHaskell #-} module Web.Routes.TH ( derivePathInfo , derivePathInfo' , standard , mkRoute ) where import Control.Applicative ((<$>)) import Control.Monad (ap, replicateM) import Data.Char (isUpper, toLower, toUpper) import Data.List (intercalate, foldl') import Data.List.Split (split, dropInitBlank, keepDelimsL, whenElt) import Data.Text (pack, unpack) import Data.Typeable (typeOf) import Language.Haskell.TH import Language.Haskell.TH.Syntax (nameBase) import Text.ParserCombinators.Parsec ((<|>),many1) import Web.Routes.PathInfo -- | use Template Haskell to create 'PathInfo' instances for a type. -- -- > $(derivePathInfo ''SiteURL) -- -- Uses the 'standard' formatter by default. derivePathInfo :: Name -> Q [Dec] derivePathInfo = derivePathInfo' standard -- FIXME: handle when called with a type (not data, newtype) -- | use Template Haskell to create 'PathInfo' instances for a type. -- -- This variant allows the user to supply a function that transforms -- the constructor name to a prettier rendering. It is important that -- the transformation function generates a unique output for each -- input. For example, simply converting the string to all lower case -- is not acceptable, because then 'FooBar' and 'Foobar' would be -- indistinguishable. -- -- > $(derivePathInfo' standard ''SiteURL) -- -- see also: 'standard' derivePathInfo' :: (String -> String) -> Name -> Q [Dec] derivePathInfo' formatter name = do c <- parseInfo name case c of Tagged cons cx keys -> do let context = pure $ [ AppT (ConT ''PathInfo) (VarT key) | key <- keys ] ++ cx i <- instanceD context (mkType ''PathInfo [mkType name (map varT keys)]) [ toPathSegmentsFn cons , fromPathSegmentsFn cons ] return [i] where toPathSegmentsFn :: [(Name, Int)] -> DecQ toPathSegmentsFn cons = do inp <- newName "inp" let body = caseE (varE inp) $ [ do args <- replicateM nArgs (newName "arg") let matchCon = conP conName (map varP args) conStr = formatter (nameBase conName) match matchCon (normalB (toURLWork conStr args)) [] | (conName, nArgs) <- cons ] toURLWork :: String -> [Name] -> ExpQ toURLWork conStr args = foldr1 (\a b -> appE (appE [| (++) |] a) b) ([| [pack conStr] |] : [ [| toPathSegments $(varE arg) |] | arg <- args ]) funD 'toPathSegments [clause [varP inp] (normalB body) []] fromPathSegmentsFn :: [(Name,Int)] -> DecQ fromPathSegmentsFn cons = do let body = (foldl1 (\a b -> appE (appE [| (<|>) |] a) b) [ parseCon conName nArgs | (conName, nArgs) <- cons]) parseCon :: Name -> Int -> ExpQ parseCon conName nArgs = foldl1 (\a b -> appE (appE [| ap |] a) b) ([| segment (pack $(stringE (formatter $ nameBase conName))) >> return $(conE conName) |] : (replicate nArgs [| fromPathSegments |])) funD 'fromPathSegments [clause [] (normalB body) []] mkType :: Name -> [TypeQ] -> TypeQ mkType con = foldl appT (conT con) data Class = Tagged [(Name, Int)] Cxt [Name] parseInfo :: Name -> Q Class parseInfo name = do info <- reify name case info of TyConI (DataD cx _ keys _ cs _) -> return $ Tagged (map conInfo cs) cx $ map conv keys TyConI (NewtypeD cx _ keys _ con _)-> return $ Tagged [conInfo con] cx $ map conv keys _ -> error ("Unexpected " <> show (typeOf info) <> ": " <> show info) where conInfo (NormalC n args) = (n, length args) conInfo (RecC n args) = (n, length args) conInfo (InfixC _ n _) = (n, 2) conInfo (ForallC _ _ con) = conInfo con #if MIN_VERSION_template_haskell(2,17,0) conv (PlainTV nm _) = nm conv (KindedTV nm _ _) = nm #else conv (PlainTV nm) = nm conv (KindedTV nm _) = nm #endif -- | the standard formatter -- -- Converts @CamelCase@ to @camel-case@. -- -- see also: 'derivePathInfo' and 'derivePathInfo'' standard :: String -> String standard = intercalate "-" . map (map toLower) . split splitter where splitter = dropInitBlank . keepDelimsL . whenElt $ isUpper mkRoute :: Name -> Q [Dec] mkRoute url = do (Tagged cons _ _) <- parseInfo url fn <- funD (mkName "route") $ map (\(con, numArgs) -> do -- methods <- parseMethods con -- runIO $ print methods args <- replicateM numArgs (newName "arg") clause [conP con $ map varP args] (normalB $ foldl' appE (varE (mkName (headLower (nameBase con)))) (map varE args)) [] ) cons return [fn] where headLower :: String -> String headLower (c:cs) = toLower c : cs -- work in progress parseMethods :: Name -> Q [Name] parseMethods con = do info <- reify con case info of (DataConI _ ty _) -> do runIO $ print ty runIO $ print $ lastTerm ty return $ extractMethods (lastTerm ty) extractMethods :: Type -> [Name] extractMethods ty = case ty of (AppT (ConT con) (ConT method)) -> [method] (AppT (ConT con) methods) -> extractMethods' methods where extractMethods' :: Type -> [Name] extractMethods' t = map (\(ConT n) -> n) (leafs t) -- | return the 'Type' after the right-most @->@. Or the original 'Type' if there are no @->@. lastTerm :: Type -> Type lastTerm t@(AppT l r) | hasArrowT l = lastTerm r | otherwise = t lastTerm t = t -- | tests if a 'Type' contains an 'ArrowT' somewhere hasArrowT :: Type -> Bool hasArrowT ArrowT = True hasArrowT (AppT l r) = hasArrowT l || hasArrowT r hasArrowT _ = False leafs :: Type -> [Type] leafs (AppT l@(AppT _ _) r) = leafs l ++ leafs r leafs (AppT _ r) = leafs r leafs t = [t] web-routes-th-0.22.8.1/test/0000755000000000000000000000000007346545000013617 5ustar0000000000000000web-routes-th-0.22.8.1/test/Test.hs0000644000000000000000000000273207346545000015076 0ustar0000000000000000{-# LANGUAGE ExtendedDefaultRules, FlexibleInstances, GeneralizedNewtypeDeriving, OverloadedStrings, TemplateHaskell, DatatypeContexts #-} module Main (main) where import Test.Hspec import Test.Hspec.QuickCheck import Test.HUnit import Test.QuickCheck import Web.Routes import Web.Routes.TH newtype ArticleId = ArticleId Int deriving (Eq, Show, Num, PathInfo, Arbitrary) -- the use of Show is and 'a' is to exercise the context generation of the 'derivePathInfo' code data (Show a) => Sitemap a = Home a | Article ArticleId deriving (Eq, Show) derivePathInfo ''Sitemap instance Arbitrary (Sitemap Int) where arbitrary = oneof [fmap Home arbitrary, fmap Article arbitrary] prop_PathInfo_isomorphism :: Sitemap Int -> Bool prop_PathInfo_isomorphism = pathInfoInverse_prop case_toPathInfo :: Assertion case_toPathInfo = do toPathInfo ((Home 1) :: Sitemap Int) @?= "/home/1" toPathInfo ((Article 0):: Sitemap Int) @?= "/article/0" case_fromPathInfo :: Assertion case_fromPathInfo = do fromPathInfo "/home/1" @?= ((Right (Home (1 :: Int))) :: Either String (Sitemap Int)) fromPathInfo "/article/0" @?= Right (Article 0) case fromPathInfo "/" :: Either String (Sitemap Int) of Left _ -> return () url -> assertFailure $ "expected a Left, but got: " ++ show url main :: IO () main = hspec $ do prop "toPathInfo" case_toPathInfo prop "fromPathInfo" case_fromPathInfo prop "PathInfo_isomorphism" prop_PathInfo_isomorphism web-routes-th-0.22.8.1/web-routes-th.cabal0000644000000000000000000000325607346545000016337 0ustar0000000000000000Name: web-routes-th Version: 0.22.8.1 License: BSD3 License-File: LICENSE Author: jeremy@seereason.com Maintainer: partners@seereason.com Homepage: https://github.com/happstack/web-routes-th Category: Web, Language Synopsis: Support for deriving PathInfo using Template Haskell Description: web-routes is a portable library for type-safe URLs. This module adds support for creating the URL parsers/printers automatically using Template Haskell. Cabal-Version: >= 1.10 Build-type: Simple tested-with: GHC==8.4.1, GHC==8.6.5, GHC==8.8.4, GHC==8.10.7, GHC==9.0.2, GHC==9.2.5, GHC==9.4.2 source-repository head type: git location: https://github.com/happstack/web-routes-th test-suite Test Default-Language : Haskell2010 type : exitcode-stdio-1.0 main-is : Test.hs hs-source-dirs : test build-depends : base >= 4.9 && < 5, hspec >= 2.2 && < 2.11, HUnit, QuickCheck, web-routes >= 0.27 && < 0.28, web-routes-th Library Default-Language: Haskell2010 Build-Depends: base >= 4.9 && < 5, parsec >= 2 && < 4, template-haskell >= 2.11 && < 2.20, text >= 0.11 && < 2.1, split >= 0.2 && < 0.3, web-routes >= 0.26 && < 0.28 Exposed-Modules: Web.Routes.TH source-repository head type: git location: https://github.com/Happstack/web-routes-th.git