Update NEWS file for 2.16.0 beta2
[ganeti-github.git] / src / Ganeti / THH / PyType.hs
1 {-# LANGUAGE TemplateHaskell #-}
2
3 {-| PyType helper for Ganeti Haskell code.
4
5 -}
6
7 {-
8
9 Copyright (C) 2013 Google Inc.
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are
14 met:
15
16 1. Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35 -}
36 module Ganeti.THH.PyType
37 ( PyType(..)
38 , pyType
39 , pyOptionalType
40 ) where
41
42 import Control.Applicative
43 import Control.Monad
44 import Data.List (intercalate)
45 import Language.Haskell.TH
46 import Language.Haskell.TH.Syntax (Lift(..))
47
48 import Ganeti.PyValue
49
50
51 -- | Represents a Python encoding of types.
52 data PyType
53 = PTMaybe PyType
54 | PTApp PyType [PyType]
55 | PTOther String
56 | PTAny
57 | PTDictOf
58 | PTListOf
59 | PTNone
60 | PTObject
61 | PTOr
62 | PTSetOf
63 | PTTupleOf
64 deriving (Show, Eq, Ord)
65
66 -- TODO: We could use th-lift to generate this instance automatically.
67 instance Lift PyType where
68 lift (PTMaybe x) = [| PTMaybe x |]
69 lift (PTApp tf as) = [| PTApp tf as |]
70 lift (PTOther i) = [| PTOther i |]
71 lift PTAny = [| PTAny |]
72 lift PTDictOf = [| PTDictOf |]
73 lift PTListOf = [| PTListOf |]
74 lift PTNone = [| PTNone |]
75 lift PTObject = [| PTObject |]
76 lift PTOr = [| PTOr |]
77 lift PTSetOf = [| PTSetOf |]
78 lift PTTupleOf = [| PTTupleOf |]
79
80 instance PyValue PyType where
81 showValue (PTMaybe x) = ptApp (ht "Maybe") [x]
82 showValue (PTApp tf as) = ptApp (showValue tf) as
83 showValue (PTOther i) = ht i
84 showValue PTAny = ht "Any"
85 showValue PTDictOf = ht "DictOf"
86 showValue PTListOf = ht "ListOf"
87 showValue PTNone = ht "None"
88 showValue PTObject = ht "Object"
89 showValue PTOr = ht "Or"
90 showValue PTSetOf = ht "SetOf"
91 showValue PTTupleOf = ht "TupleOf"
92
93 ht :: String -> String
94 ht = ("ht.T" ++)
95
96 ptApp :: String -> [PyType] -> String
97 ptApp name ts = name ++ "(" ++ intercalate ", " (map showValue ts) ++ ")"
98
99 -- | Converts a Haskell type name into a Python type name.
100 pyTypeName :: Name -> PyType
101 pyTypeName name =
102 case nameBase name of
103 "()" -> PTNone
104 "Map" -> PTDictOf
105 "Set" -> PTSetOf
106 "ListSet" -> PTSetOf
107 "Either" -> PTOr
108 "GenericContainer" -> PTDictOf
109 "JSValue" -> PTAny
110 "JSObject" -> PTObject
111 str -> PTOther str
112
113 -- | Converts a Haskell type into a Python type.
114 pyType :: Type -> Q PyType
115 pyType t | not (null args) = PTApp `liftM` pyType fn `ap` mapM pyType args
116 where (fn, args) = pyAppType t
117 pyType (ConT name) = return $ pyTypeName name
118 pyType ListT = return PTListOf
119 pyType (TupleT 0) = return PTNone
120 pyType (TupleT _) = return PTTupleOf
121 pyType typ = fail $ "unhandled case for type " ++ show typ
122
123 -- | Returns a type and its type arguments.
124 pyAppType :: Type -> (Type, [Type])
125 pyAppType = g []
126 where
127 g as (AppT typ1 typ2) = g (typ2 : as) typ1
128 g as typ = (typ, as)
129
130 -- | @pyType opt typ@ converts Haskell type @typ@ into a Python type,
131 -- where @opt@ determines if the converted type is optional (i.e.,
132 -- Maybe).
133 pyOptionalType :: Bool -> Type -> Q PyType
134 pyOptionalType True typ = PTMaybe <$> pyType typ
135 pyOptionalType False typ = pyType typ