Divide camelcase name functions into another package.

This commit is contained in:
Kei Hibino 2013-05-03 02:45:07 +09:00
parent e254f11903
commit 0eae444f96
14 changed files with 247 additions and 61 deletions

View File

@ -31,6 +31,7 @@ library
build-depends: base <5
, template-haskell
, th-names
, containers
, time
, convertible

View File

@ -15,14 +15,6 @@
-- This module contains templates to generate Haskell record types
-- and instances correspond to RDB table schema.
module Database.HDBC.TH (
ConName(conName), toConName,
VarName(varName), toVarName,
conCamelcaseName,
varCamelcaseName,
pprQ,
fieldInfo,
derivingEq, derivingShow, derivingRead, derivingData, derivingTypable,
@ -57,8 +49,13 @@ import Data.List (elemIndex)
import Database.HDBC (IConnection, SqlValue, fromSql, toSql)
import Language.Haskell.TH.CamelCaseNames
(ConName (conName), VarName (varName),
conCamelcaseName, varCamelcaseName,
varNameWithPrefix,
toTypeCon)
import Language.Haskell.TH
(Q, Name, mkName, runQ, runIO, Ppr, ppr,
(Q, Name, mkName, runIO,
TypeQ, ExpQ, DecQ, Dec,
appsE, conE, varE, listE, litE, stringE, integerL,
listP, varP, wildP,
@ -66,8 +63,6 @@ import Language.Haskell.TH
dataD, sigD, funD, valD,
clause, normalB,
recC, cxt, varStrictType, strictType, isStrict)
import qualified Language.Haskell.TH.PprLib as TH
import qualified Language.Haskell.TH.Syntax as TH
import Database.HDBC.Session (withConnectionIO)
import Database.Record.Persistable
@ -87,64 +82,16 @@ import qualified Language.SQL.Keyword as SQL
import Database.HDBC.Schema.Driver (Driver, getFields, getPrimaryKey)
capitalize :: String -> String
capitalize (c:cs) = toUpper c : cs
capitalize "" = ""
unCapitalize :: String -> String
unCapitalize (c:cs) = toLower c : cs
unCapitalize "" = ""
newtype ConName = ConName { conName :: Name }
newtype VarName = VarName { varName :: Name }
toConName :: String -> ConName
toConName = ConName . mkName . capitalize
toVarName :: String -> VarName
toVarName = VarName . mkName . unCapitalize
nameChars :: String
nameChars = '\'' : ['0' .. '9'] ++ ['A' .. 'Z'] ++ ['a' .. 'z']
splitForName :: String -> [String]
splitForName str
| rest /= [] = tk : splitForName (tail rest)
| otherwise = [tk]
where
(tk, rest) = span (`elem` nameChars) str
camelcaseUpper :: String -> String
camelcaseUpper = concat . map capitalize . splitForName
-- camelcaseLower :: String -> String
-- camelcaseLower = unCapitalize . camelcaseUpper
conCamelcaseName :: String -> ConName
conCamelcaseName = toConName . camelcaseUpper
varCamelcaseName :: String -> VarName
varCamelcaseName = toVarName . camelcaseUpper
varNameWithPrefix :: String -> String -> VarName
varNameWithPrefix n p = toVarName $ p ++ camelcaseUpper n
nameOfTableSQL :: String -> String -> String
nameOfTableSQL schema table = map toUpper schema ++ '.' : map toLower table
typeOfName :: ConName -> TypeQ
typeOfName = conT . conName
recordTypeNameDefault :: String -> ConName
recordTypeNameDefault = conCamelcaseName
recordTypeDefault :: String -> TypeQ
recordTypeDefault = typeOfName . recordTypeNameDefault
recordTypeDefault = toTypeCon . recordTypeNameDefault
pprQ :: (Functor m, TH.Quasi m, Ppr a) => Q a -> m TH.Doc
pprQ = fmap ppr . runQ
fieldInfo :: String
-> TypeQ
-> ((VarName, TypeQ), String) -- ^ (fieldVarName, (fieldInSQL, fieldTypeInTable))
@ -283,7 +230,7 @@ defineRecord
let schemas = map fst schemas'
typ <- defineRecordType tyC schemas drvs
let width = length schemas'
typeCon = typeOfName tyC
typeCon = toTypeCon tyC
fromSQL <- defineRecordConstructFunction cF tyC width
toSQL <- defineRecordDecomposeFunction dF typeCon (map fst schemas)
tableI <- defineTableInfo

1
th-names/GNUmakefile Symbolic link
View File

@ -0,0 +1 @@
../devel/GNUmakefile

30
th-names/LICENSE Normal file
View File

@ -0,0 +1,30 @@
Copyright (c) 2013, Kei Hibino
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 Kei Hibino 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.

2
th-names/Setup.hs Normal file
View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

View File

@ -0,0 +1,6 @@
haskell-th-names (0.0.1.0-1~hackage1) unstable; urgency=low
* Debianization generated by cabal-debian
-- Kei Hibino <ex8k.hibino@gmail.com> Fri, 03 May 2013 02:02:02 +0900

1
th-names/debian/compat Normal file
View File

@ -0,0 +1 @@
7

64
th-names/debian/control Normal file
View File

@ -0,0 +1,64 @@
Source: haskell-th-names
Priority: extra
Section: haskell
Maintainer: Kei Hibino <ex8k.hibino@gmail.com>
Build-Depends: debhelper (>= 7.0)
, haskell-devscripts (>= 0.8)
, cdbs
, ghc
, ghc-prof
, libghc-base-dev (<< 5) | ghc
, libghc-base-prof (<< 5) | ghc-prof
, libghc-template-haskell-dev | ghc
, libghc-template-haskell-prof | ghc-prof
Build-Depends-Indep: ghc-doc
, libghc-base-doc (<< 5) | ghc-doc
, libghc-template-haskell-doc | ghc-doc
Standards-Version: 3.9.3
Homepage: http://twitter.com/khibino
Package: libghc-th-names-dev
Architecture: any
Depends: ${shlibs:Depends}
, ${haskell:Depends}
, ${misc:Depends}
Recommends: ${haskell:Recommends}
Suggests: ${haskell:Suggests}
Provides: ${haskell:Provides}
Description: Manipulate name strings for TH
This package includes functions to manipulate name string for Template Haskell.
.
Author: Kei Hibino
Upstream-Maintainer: ex8k.hibino@gmail.com
.
This package contains the normal library files.
Package: libghc-th-names-prof
Architecture: any
Depends: ${haskell:Depends}
, ${misc:Depends}
Recommends: ${haskell:Recommends}
Suggests: ${haskell:Suggests}
Provides: ${haskell:Provides}
Description: Manipulate name strings for TH
This package includes functions to manipulate name string for Template Haskell.
.
Author: Kei Hibino
Upstream-Maintainer: ex8k.hibino@gmail.com
.
This package contains the libraries compiled with profiling enabled.
Package: libghc-th-names-doc
Architecture: all
Section: doc
Depends: ${haskell:Depends}
, ${misc:Depends}
Recommends: ${haskell:Recommends}
Suggests: ${haskell:Suggests}
Description: Manipulate name strings for TH
This package includes functions to manipulate name string for Template Haskell.
.
Author: Kei Hibino
Upstream-Maintainer: ex8k.hibino@gmail.com
.
This package contains the documentation files.

30
th-names/debian/copyright Normal file
View File

@ -0,0 +1,30 @@
Copyright (c) 2013, Kei Hibino
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 Kei Hibino 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.

11
th-names/debian/rules Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/make -f
DEB_CABAL_PACKAGE = th-names
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/hlibrary.mk
# How to install an extra file into the documentation package
#binary-fixup/libghc-th-names-doc::
# echo "Some informative text" > debian/libghc-th-names-doc/usr/share/doc/libghc-th-names-doc/AnExtraDocFile

View File

@ -0,0 +1 @@
3.0 (quilt)

5
th-names/debian/watch Normal file
View File

@ -0,0 +1,5 @@
version=3
opts="downloadurlmangle=s|archive/([\w\d_-]+)/([\d\.]+)/|archive/$1/$2/$1-$2.tar.gz|,\
filenamemangle=s|(.*)/$|th-names-$1.tar.gz|" \
http://hackage.haskell.org/packages/archive/th-names \
([\d\.]*\d)/

View File

@ -0,0 +1,68 @@
module Language.Haskell.TH.CamelCaseNames (
ConName (ConName, conName), toConName,
VarName (VarName, varName), toVarName,
conCamelcaseName, varCamelcaseName,
varNameWithPrefix,
toTypeCon, toDataCon,
pprQ
) where
import Data.Char (toUpper, toLower)
import Language.Haskell.TH
(Name, mkName, TypeQ, conT, ExpQ, conE,
Ppr, ppr, Q, runQ)
import Language.Haskell.TH.PprLib (Doc)
import Language.Haskell.TH.Syntax (Quasi)
capitalize :: String -> String
capitalize (c:cs) = toUpper c : cs
capitalize "" = ""
unCapitalize :: String -> String
unCapitalize (c:cs) = toLower c : cs
unCapitalize "" = ""
newtype ConName = ConName { conName :: Name }
newtype VarName = VarName { varName :: Name }
toConName :: String -> ConName
toConName = ConName . mkName . capitalize
toVarName :: String -> VarName
toVarName = VarName . mkName . unCapitalize
nameChars :: String
nameChars = '\'' : ['0' .. '9'] ++ ['A' .. 'Z'] ++ ['a' .. 'z']
splitForName :: String -> [String]
splitForName str
| rest /= [] = tk : splitForName (tail rest)
| otherwise = [tk]
where
(tk, rest) = span (`elem` nameChars) str
camelcaseUpper :: String -> String
camelcaseUpper = concat . map capitalize . splitForName
conCamelcaseName :: String -> ConName
conCamelcaseName = toConName . camelcaseUpper
varCamelcaseName :: String -> VarName
varCamelcaseName = toVarName . camelcaseUpper
varNameWithPrefix :: String -> String -> VarName
varNameWithPrefix n p = toVarName $ p ++ camelcaseUpper n
toTypeCon :: ConName -> TypeQ
toTypeCon = conT . conName
toDataCon :: ConName -> ExpQ
toDataCon = conE . conName
pprQ :: (Functor m, Quasi m, Ppr a) => Q a -> m Doc
pprQ = fmap ppr . runQ

19
th-names/th-names.cabal Normal file
View File

@ -0,0 +1,19 @@
name: th-names
version: 0.0.1.0
synopsis: Manipulate name strings for TH
description: This package includes functions to manipulate name string for Template Haskell.
homepage: http://twitter.com/khibino
license: BSD3
license-file: LICENSE
author: Kei Hibino
maintainer: ex8k.hibino@gmail.com
copyright: Copyright (c) 2013 Kei Hibino
category: Development
build-type: Simple
cabal-version: >=1.8
library
exposed-modules:
Language.Haskell.TH.CamelCaseNames
build-depends: base <5, template-haskell
hs-source-dirs: src