-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gen.hs
56 lines (43 loc) · 1.56 KB
/
Gen.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TupleSections #-}
module Gen where
import Debug.Trace
import Control.Monad
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import Data.Typeable
import Data.Data
import Data.List
data TypeEnum = IntType | FloatType | BoolType | DateType | StringType deriving (Enum, Show, Eq)
data Typed a = Typed TypeEnum a deriving Show
type KeyPath = [String]
data Schema = Object [(String, Schema)] |
Field TypeEnum |
Array Schema deriving Show
data Permissions = Permissions Bool Bool Bool deriving (Data, Typeable, Show)
data FileType = FileType {
pathname :: String,
subdirs :: [FileType],
permissions ::Permissions
} deriving (Data, Typeable, Show)
data FileString = FileString {address :: String, age :: [Int], fi :: FileInt} deriving (Data, Typeable)
data FileInt = FileInt {something::String} deriving (Data, Typeable)
fieldType :: Type -> Q Exp
fieldType (ConT name)
| name == ''String = [| Field StringType |]
| name == ''Bool = [| Field BoolType |]
| name == ''Int = [| Field IntType |]
| otherwise = listFields name
fieldType (AppT ListT typ) = [| Array $(fieldType typ) |]
listFields :: Name -> Q Exp
listFields name = do
rf <- reify name
let fields = case rf of
TyConI (DataD _ _ _ [RecC _ fields] _) -> fields
a -> error $ show a
let names = map (\(name, _, typ) -> typ) fields
let specs = listE $ map (\(name, _, typ) -> let sn = nameBase name in [| (sn, $(fieldType typ)) |]) fields
[| Object $specs |]
--stringE $ show names