Skip to content

Commit 655aad0

Browse files
committed
Move defaultDerivedClasses as a ToolSetting
1 parent fafc9cc commit 655aad0

5 files changed

Lines changed: 32 additions & 65 deletions

File tree

api-tools.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ Library
5353
Data.API.Tools.CBOR
5454
Data.API.Tools.Combinators
5555
Data.API.Tools.Datatypes
56-
Data.API.Tools.DataTypeable
5756
Data.API.Tools.DeepSeq
5857
Data.API.Tools.Enum
5958
Data.API.Tools.Example

src/Data/API/Tools.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module Data.API.Tools
2424
-- * Individual tools
2525
, enumTool
2626
, exampleTool
27-
, dataTypeableTool
2827
, deepSeqTool
2928
, jsonTool
3029
, jsonTool'
@@ -42,7 +41,6 @@ module Data.API.Tools
4241
) where
4342

4443
import Data.API.Tools.Combinators
45-
import Data.API.Tools.DataTypeable
4644
import Data.API.Tools.Datatypes
4745
import Data.API.Tools.DeepSeq
4846
import Data.API.Tools.Enum
@@ -65,7 +63,7 @@ generate = generateWith defaultToolSettings
6563
-- | Generate the datatypes corresponding to an API, allowing the
6664
-- 'ToolSettings' to be overriden.
6765
generateWith :: ToolSettings -> API -> Q [Dec]
68-
generateWith ts api = generateAPIToolsWith ts api [datatypesTool]
66+
generateWith ts api = generateAPIToolsWith ts api [datatypesTool ts]
6967

7068
-- | Apply a list of tools to an 'API', generating TH declarations.
7169
-- See the individual tool descriptions for details. Note that

src/Data/API/Tools/Combinators.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ module Data.API.Tools.Combinators
2222
, warnOnOmittedInstance
2323
, newtypeSmartConstructors
2424
, defaultToolSettings
25+
, defaultDerivedClasses
2526
) where
2627

2728
import Data.API.Types
2829

2930
import Control.Applicative
3031
import Data.Monoid
3132
import Data.Semigroup as Sem
33+
import Data.String
34+
import Data.Typeable
3235
import Language.Haskell.TH
3336
import Prelude
3437

@@ -43,15 +46,41 @@ data ToolSettings = ToolSettings
4346
, newtypeSmartConstructors :: Bool
4447
-- ^ Rename the constructors of filtered newtypes and generate
4548
-- smart constructors that enforce the invariants
49+
, defaultDerivedClasses :: APINode -> [Name]
50+
-- ^ The classes which are derived automatically by api-tools.
4651
}
4752

4853
-- | Default settings designed to be overridden.
4954
defaultToolSettings :: ToolSettings
5055
defaultToolSettings = ToolSettings
5156
{ warnOnOmittedInstance = False
5257
, newtypeSmartConstructors = False
58+
, defaultDerivedClasses = default_derived_classes
5359
}
5460

61+
-- | Default names of classes for which to derive instances, depending
62+
-- on the type of API node.
63+
default_derived_classes :: APINode -> [Name]
64+
default_derived_classes an = case anSpec an of
65+
SpNewtype sn -> case snType sn of
66+
BTstring -> ''IsString : derive_leaf_nms
67+
BTbinary -> derive_leaf_nms
68+
BTbool -> derive_leaf_nms
69+
BTint -> derive_leaf_nms
70+
BTutc -> derive_leaf_nms
71+
SpRecord _ -> derive_node_nms
72+
SpUnion _ -> derive_node_nms
73+
SpEnum _ -> derive_leaf_nms ++ [''Bounded, ''Enum]
74+
SpSynonym _ -> []
75+
76+
derive_leaf_nms :: [Name]
77+
derive_leaf_nms = [''Show,''Eq,''Ord,''Typeable]
78+
79+
derive_node_nms :: [Name]
80+
derive_node_nms = [''Show,''Eq,''Typeable]
81+
82+
83+
5584
-- | A @'Tool' a@ is something that can generate TH declarations from
5685
-- a value of type @a@. Tools can be combined using the 'Monoid'
5786
-- instance.

src/Data/API/Tools/DataTypeable.hs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Data/API/Tools/Datatypes.hs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
module Data.API.Tools.Datatypes
33
( datatypesTool
44
, datatypesTool'
5-
, defaultDerivedClasses
65
, type_nm
76
, rep_type_nm
87
, nodeT
@@ -32,15 +31,14 @@ import Data.Maybe
3231
import Data.String
3332
import qualified Data.Text as T
3433
import Data.Time
35-
import Data.Typeable
3634
import Language.Haskell.TH
3735
import Text.Regex
3836
import Prelude
3937

4038

4139
-- | Tool to generate datatypes and type synonyms corresponding to an API
42-
datatypesTool :: APITool
43-
datatypesTool = datatypesTool' defaultDerivedClasses
40+
datatypesTool :: ToolSettings -> APITool
41+
datatypesTool = datatypesTool' . defaultDerivedClasses
4442

4543
-- | Tool to generate datatypes and type synonyms corresponding to an
4644
-- API, where the function specifies the derived classes for each datatype.
@@ -165,28 +163,6 @@ basic_type bt =
165163
BTutc -> ConT ''UTCTime
166164

167165

168-
-- | Default names of classes for which to derive instances, depending
169-
-- on the type of API node.
170-
defaultDerivedClasses :: APINode -> [Name]
171-
defaultDerivedClasses an = case anSpec an of
172-
SpNewtype sn -> case snType sn of
173-
BTstring -> ''IsString : derive_leaf_nms
174-
BTbinary -> derive_leaf_nms
175-
BTbool -> derive_leaf_nms
176-
BTint -> derive_leaf_nms
177-
BTutc -> derive_leaf_nms
178-
SpRecord _ -> derive_node_nms
179-
SpUnion _ -> derive_node_nms
180-
SpEnum _ -> derive_leaf_nms ++ [''Bounded, ''Enum]
181-
SpSynonym _ -> []
182-
183-
derive_leaf_nms :: [Name]
184-
derive_leaf_nms = [''Show,''Eq,''Ord,''Typeable]
185-
186-
derive_node_nms :: [Name]
187-
derive_node_nms = [''Show,''Eq,''Typeable]
188-
189-
190166
-- | Name of the type corresponding to the API node, e.g. @JobId@
191167
type_nm :: APINode -> Name
192168
type_nm an = mkName $ T.unpack $ _TypeName $ anName an

0 commit comments

Comments
 (0)