web-routes-th-0.22.4/0000755000000000000000000000000012635040252012471 5ustar0000000000000000web-routes-th-0.22.4/LICENSE0000644000000000000000000000275712635040252013511 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.4/Setup.hs0000644000000000000000000000005612635040252014126 0ustar0000000000000000import Distribution.Simple main = defaultMain web-routes-th-0.22.4/web-routes-th.cabal0000644000000000000000000000252412635040252016165 0ustar0000000000000000Name: web-routes-th Version: 0.22.4 License: BSD3 License-File: LICENSE Author: jeremy@seereason.com Maintainer: partners@seereason.com Bug-Reports: http://bugzilla.seereason.com/ 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.8 Build-type: Simple tested-with: GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.3 test-suite Test type : exitcode-stdio-1.0 main-is : Test.hs hs-source-dirs : test build-depends : base == 4.*, hspec == 2.2.*, HUnit, QuickCheck, web-routes, web-routes-th Library Build-Depends: base >= 4 && < 5, parsec >= 2 && < 4, template-haskell, text, split, web-routes >= 0.26 Exposed-Modules: Web.Routes.TH source-repository head type: git location: https://github.com/Happstack/web-routes-th.git web-routes-th-0.22.4/test/0000755000000000000000000000000012635040252013450 5ustar0000000000000000web-routes-th-0.22.4/test/Test.hs0000644000000000000000000000227012635040252014724 0ustar0000000000000000{-# LANGUAGE GeneralizedNewtypeDeriving, OverloadedStrings, TemplateHaskell #-} 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) data Sitemap = Home | Article ArticleId deriving (Eq, Show) derivePathInfo ''Sitemap instance Arbitrary Sitemap where arbitrary = oneof [return Home, fmap Article arbitrary] prop_PathInfo_isomorphism :: Sitemap -> Bool prop_PathInfo_isomorphism = pathInfoInverse_prop case_toPathInfo :: Assertion case_toPathInfo = do toPathInfo Home @?= "/home" toPathInfo (Article 0) @?= "/article/0" case_fromPathInfo :: Assertion case_fromPathInfo = do fromPathInfo "/home" @?= Right Home fromPathInfo "/article/0" @?= Right (Article 0) case fromPathInfo "/" :: Either String Sitemap 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.4/Web/0000755000000000000000000000000012635040252013206 5ustar0000000000000000web-routes-th-0.22.4/Web/Routes/0000755000000000000000000000000012635040252014467 5ustar0000000000000000web-routes-th-0.22.4/Web/Routes/TH.hs0000644000000000000000000001465012635040252015344 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 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 = [ mkCtx ''PathInfo [varT key] | key <- keys ] ++ map return cx i <- instanceD (sequence context) (mkType ''PathInfo [mkType name (map varT keys)]) [ toPathSegmentsFn cons , fromPathSegmentsFn cons ] return [i] where #if MIN_VERSION_template_haskell(2,4,0) mkCtx = classP #else mkCtx = mkType #endif 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 $ "derivePathInfo - invalid input: " ++ pprint 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,4,0) conv (PlainTV nm) = nm conv (KindedTV nm _) = nm #else conv = id #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]