web-routes-generics (empty) → 0.1.0.0
raw patch · 4 files changed
+112/−0 lines, 4 filesdep +basedep +parsecdep +textsetup-changed
Dependencies added: base, parsec, text, web-routes
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- Web/Routes/Generics.hs +56/−0
- web-routes-generics.cabal +24/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2019, 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Web/Routes/Generics.hs view
@@ -0,0 +1,56 @@+{-# language PolyKinds, TypeOperators, DeriveGeneric, TypeSynonymInstances, FlexibleContexts, FlexibleInstances, ScopedTypeVariables #-}+module Web.Routes.Generics where++import Data.Text (Text, pack, toLower)+import GHC.Generics+import Text.ParserCombinators.Parsec.Prim+import Text.ParserCombinators.Parsec.Combinator+import Web.Routes.PathInfo (PathInfo(fromPathSegments, toPathSegments), URLParser, segment)++data Path+ = Foo+ | Bar Int Int+ deriving (Show, Generic)++class GToURL f where+ gtoPathSegments :: f a -> [Text]+ gfromPathSegments :: URLParser (f a)++instance GToURL U1 where+ gtoPathSegments U1 = []+ gfromPathSegments = eof >> pure U1++instance forall c f. (Constructor c, GToURL f) => GToURL (C1 c f) where+ gtoPathSegments m@(M1 x) = (toLower $ pack $ conName m) : gtoPathSegments x+ gfromPathSegments =+ let constr = undefined :: C1 c f r+ in do segment (toLower $ pack $ conName constr) <|> segment (pack $ conName constr)+ M1 <$> gfromPathSegments++instance (GToURL f, GToURL g) => GToURL (f :+: g) where+ gtoPathSegments (L1 x) = gtoPathSegments x+ gtoPathSegments (R1 x) = gtoPathSegments x+ gfromPathSegments = try (L1 <$> gfromPathSegments) <|> (R1 <$> gfromPathSegments)++instance (GToURL f, GToURL g) => GToURL (f :*: g) where+ gtoPathSegments (x :*: y) = gtoPathSegments x ++ gtoPathSegments y+ gfromPathSegments =+ do x <- gfromPathSegments+ y <- gfromPathSegments+ pure (x :*: y)++instance (GToURL f) => GToURL (D1 c f) where+ gtoPathSegments m@(M1 x) = gtoPathSegments x+ gfromPathSegments = M1 <$> gfromPathSegments++instance (PathInfo a) => GToURL (K1 i a) where+ gtoPathSegments (K1 a) = toPathSegments a+ gfromPathSegments = K1 <$> fromPathSegments++instance (GToURL f) => GToURL (S1 c f) where+ gtoPathSegments (M1 f) = gtoPathSegments f+ gfromPathSegments = M1 <$> gfromPathSegments++instance PathInfo Path where+ toPathSegments = gtoPathSegments . from+ fromPathSegments = fmap to gfromPathSegments
+ web-routes-generics.cabal view
@@ -0,0 +1,24 @@+Name: web-routes-generics+Version: 0.1.0.0+License: BSD3+License-File: LICENSE+Author: jeremy@seereason.com+Maintainer: partners@seereason.com+Bug-Reports: http://bugzilla.seereason.com/+Category: Web, Language+Synopsis: portable, type-safe URL routing+Description: This library adds support for parsing/printing URLs using the generics library+Cabal-Version: >= 1.6+Build-type: Simple+tested-with: GHC == 8.6.5++Library+ Build-Depends: base >= 4 && < 5,+ parsec >= 2 && <4,+ text,+ web-routes >= 0.26+ Exposed-Modules: Web.Routes.Generics++source-repository head+ type: git+ location: https://github.com/Happstack/web-routes-generics.git