22module Data.API.Tools.Datatypes
33 ( datatypesTool
44 , datatypesTool'
5- , defaultDerivedClasses
65 , type_nm
76 , rep_type_nm
87 , nodeT
98 , nodeRepT
109 , nodeConE
1110 , nodeConP
1211 , nodeNewtypeConE
12+ , nodeNewtypeConP
1313 , nodeFieldE
1414 , nodeFieldP
1515 , nodeAltConE
1616 , nodeAltConP
1717 , newtypeProjectionE
18+ , pref_field_nm
1819 ) where
1920
2021import Data.API.TH
@@ -30,15 +31,14 @@ import Data.Maybe
3031import Data.String
3132import qualified Data.Text as T
3233import Data.Time
33- import Data.Typeable
3434import Language.Haskell.TH
3535import Text.Regex
3636import Prelude
3737
3838
3939-- | Tool to generate datatypes and type synonyms corresponding to an API
40- datatypesTool :: APITool
41- datatypesTool = datatypesTool' defaultDerivedClasses
40+ datatypesTool :: ToolSettings -> APITool
41+ datatypesTool = datatypesTool' . defaultDerivedClasses
4242
4343-- | Tool to generate datatypes and type synonyms corresponding to an
4444-- API, where the function specifies the derived classes for each datatype.
@@ -163,28 +163,6 @@ basic_type bt =
163163 BTutc -> ConT ''UTCTime
164164
165165
166- -- | Default names of classes for which to derive instances, depending
167- -- on the type of API node.
168- defaultDerivedClasses :: APINode -> [Name ]
169- defaultDerivedClasses an = case anSpec an of
170- SpNewtype sn -> case snType sn of
171- BTstring -> ''IsString : derive_leaf_nms
172- BTbinary -> derive_leaf_nms
173- BTbool -> derive_leaf_nms
174- BTint -> derive_leaf_nms
175- BTutc -> derive_leaf_nms
176- SpRecord _ -> derive_node_nms
177- SpUnion _ -> derive_node_nms
178- SpEnum _ -> derive_leaf_nms ++ [''Bounded, ''Enum]
179- SpSynonym _ -> []
180-
181- derive_leaf_nms :: [Name ]
182- derive_leaf_nms = [''Show,''Eq,''Ord,''Typeable]
183-
184- derive_node_nms :: [Name ]
185- derive_node_nms = [''Show,''Eq,''Typeable]
186-
187-
188166-- | Name of the type corresponding to the API node, e.g. @JobId@
189167type_nm :: APINode -> Name
190168type_nm an = mkName $ T. unpack $ _TypeName $ anName an
@@ -252,6 +230,9 @@ nodeConP an = conP (rep_type_nm an)
252230nodeNewtypeConE :: ToolSettings -> APINode -> SpecNewtype -> ExpQ
253231nodeNewtypeConE ts an sn = conE $ newtype_con_nm (newtypeSmartConstructors ts && isJust (snFilter sn)) an
254232
233+ nodeNewtypeConP :: ToolSettings -> APINode -> SpecNewtype -> [Q Pat ] -> PatQ
234+ nodeNewtypeConP ts an sn ps = conP (newtype_con_nm (newtypeSmartConstructors ts && isJust (snFilter sn)) an) ps
235+
255236-- | A record field in an API node, as an expression
256237nodeFieldE :: APINode -> FieldName -> ExpQ
257238nodeFieldE an fnm = varE $ pref_field_nm an fnm
0 commit comments