Revision 6696f1f9 src/Ganeti/THH/Field.hs

b/src/Ganeti/THH/Field.hs
36 36
  , serialFields
37 37
  , TagSet
38 38
  , tagsFields
39
  , fileModeAsIntField
39 40
  ) where
40 41

  
41 42
import Control.Monad
42 43
import qualified Data.Set as Set
43 44
import Language.Haskell.TH
44 45
import qualified Text.JSON as JSON
46
import System.Posix.Types (FileMode)
45 47
import System.Time (ClockTime(..))
46 48

  
47 49
import Ganeti.JSON
......
74 76
    , fieldShow = Just $ [| \c -> (JSON.showJSON $ TimeAsDoubleJSON c, []) |]
75 77
    }
76 78

  
79
-- | A helper function for creating fields whose Haskell representation is
80
-- 'Integral' and which are serialized as numbers.
81
integralField :: Q Type -> String -> Field
82
integralField typq fname =
83
  let (~->) = appT . appT arrowT  -- constructs an arrow type
84
      (~::) = sigE . varE         -- (f ~:: t) constructs (f :: t)
85
  in (simpleField fname typq)
86
      { fieldRead = Just $
87
        [| \_ -> liftM $('fromInteger ~:: (conT ''Integer ~-> typq))
88
                   . JSON.readJSON |]
89
      , fieldShow = Just $
90
          [| \c -> (JSON.showJSON
91
                    . $('toInteger ~:: (typq ~-> conT ''Integer))
92
                    $ c, []) |]
93
      }
94

  
77 95
-- * External functions and data types
78 96

  
79 97
-- | Timestamp fields description.
......
98 116
tagsFields :: [Field]
99 117
tagsFields = [ defaultField [| Set.empty |] $
100 118
               simpleField "tags" [t| TagSet |] ]
119

  
120
-- ** Fields related to POSIX data types
121

  
122
-- | Creates a new mandatory field that reads a file mode in the standard
123
-- POSIX file mode representation. The Haskell type of the field is 'FileMode'.
124
fileModeAsIntField :: String -> Field
125
fileModeAsIntField = integralField [t| FileMode |]

Also available in: Unified diff