Revision 4cbe9bda htools/Ganeti/Qlang.hs

b/htools/Ganeti/Qlang.hs
28 28
module Ganeti.Qlang
29 29
    ( Filter(..)
30 30
    , FilterValue(..)
31
    , Fields
31 32
    , Query(..)
32 33
    , QueryResult(..)
33 34
    , QueryFields(..)
34 35
    , QueryFieldsResult(..)
36
    , FieldType(..)
35 37
    , FieldDefinition(..)
36 38
    , ResultEntry(..)
39
    , ResultStatus(..)
37 40
    , ItemType(..)
38 41
    , checkRS
39 42
    ) where
......
46 49

  
47 50
import qualified Ganeti.Constants as C
48 51
import Ganeti.THH
52
import Ganeti.HTools.JSON
49 53

  
50 54
-- * THH declarations, that require ordering.
51 55

  
......
93 97
  ])
94 98
$(makeJSONInstance ''ItemType)
95 99

  
96
-- * Main Qlang queries and responses.
97

  
98
-- | Query2 query.
99
data Query = Query ItemType Fields (Maybe Filter)
100

  
101
-- | Query2 result.
102
data QueryResult = QueryResult [ FieldDefinition ] [ ResultEntry ]
103

  
104
-- | Query2 Fields query.
105
-- (to get supported fields names, descriptions, and types)
106
data QueryFields = QueryFields ItemType Fields
107

  
108
-- | Query2 Fields result.
109
data QueryFieldsResult = QueryFieldsResult [ FieldDefinition ]
110

  
111 100
-- * Sub data types for query2 queries and responses.
112 101

  
113 102
-- | List of requested fields.
......
249 238
-- | Regexp to apply to the filter value, for filteriong purposes.
250 239
type FilterRegexp = String
251 240

  
252
-- | Definition of a field.
253
data FieldDefinition = FieldDefinition FieldName FieldTitle FieldType FieldDoc
254

  
255 241
-- | Name of a field.
256 242
type FieldName = String
257 243
-- | Title of a field, when represented in tabular format.
......
259 245
-- | Human redable description of a field.
260 246
type FieldDoc = String
261 247

  
248
-- | Definition of a field.
249
$(buildObject "FieldDefinition" "fdef"
250
  [ simpleField "name"  [t| FieldName  |] -- FIXME: the name has restrictions
251
  , simpleField "title" [t| FieldTitle |]
252
  , simpleField "kind"  [t| FieldType  |]
253
  , simpleField "doc"   [t| FieldDoc   |]
254
  ])
255

  
262 256
--- | Single field entry result.
263 257
data ResultEntry = ResultEntry ResultStatus (Maybe ResultValue)
258
                   deriving (Show, Read, Eq)
259

  
260
instance JSON ResultEntry where
261
  showJSON (ResultEntry rs rv) =
262
    showJSON (showJSON rs, maybe JSNull showJSON rv)
263
  readJSON v = do
264
    (rs, rv) <- readJSON v
265
    rv' <- case rv of
266
             JSNull -> return Nothing
267
             x -> readJSON x
268
    return $ ResultEntry rs rv'
269

  
270
-- | The type of one result row.
271
type ResultRow = [ ResultEntry ]
264 272

  
265 273
-- | Value of a field, in json encoding.
266 274
-- (its type will be depending on ResultStatus and FieldType)
267 275
type ResultValue = JSValue
276

  
277
-- * Main Qlang queries and responses.
278

  
279
-- | Query2 query.
280
data Query = Query ItemType Fields Filter
281

  
282
-- | Query2 result.
283
$(buildObject "QueryResult" "qres"
284
  [ simpleField "fields" [t| [ FieldDefinition ] |]
285
  , simpleField "data"   [t| [ ResultRow       ] |]
286
  ])
287

  
288
-- | Query2 Fields query.
289
-- (to get supported fields names, descriptions, and types)
290
data QueryFields = QueryFields ItemType Fields
291

  
292
-- | Query2 Fields result.
293
data QueryFieldsResult = QueryFieldsResult [ FieldDefinition ]

Also available in: Unified diff