Commit fa10983e authored by Iustin Pop's avatar Iustin Pop

Rework custom fields handling

This patch changes a bit the handling of custom fields. Since in
general we use custom fields to aggregate multiple entries in the JSON
object into a safer data-type, we should also have a way to declare
which extra entries this field covers (so that in the future we can
say what are all the JSON keys for an object).
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent af42ffb6
......@@ -72,6 +72,7 @@ import Ganeti.Constants
import Ganeti.Errors
import Ganeti.JSON
import Ganeti.Jobs (JobStatus)
import Ganeti.OpParams (pTagsObject)
import Ganeti.OpCodes
import Ganeti.Utils
import qualified Ganeti.Query.Language as Qlang
......@@ -137,9 +138,7 @@ $(genLuxiOp "LuxiOp"
)
, (luxiReqQueryClusterInfo, [])
, (luxiReqQueryTags,
[ customField 'decodeTagObject 'encodeTagObject $
simpleField "kind" [t| TagObject |]
])
[ pTagsObject ])
, (luxiReqSubmitJob,
[ simpleField "job" [t| [OpCode] |] ]
)
......
......@@ -305,7 +305,7 @@ data Disk = Disk
} deriving (Show, Eq)
$(buildObjectSerialisation "Disk"
[ customField 'decodeDLId 'encodeFullDLId $
[ customField 'decodeDLId 'encodeFullDLId ["dev_type"] $
simpleField "logical_id" [t| DiskLogicalId |]
-- , simpleField "physical_id" [t| String |]
, defaultField [| [] |] $ simpleField "children" [t| [Disk] |]
......
......@@ -514,8 +514,9 @@ pTagsList = simpleField "tags" [t| [String] |]
-- | Tags object.
pTagsObject :: Field
pTagsObject = customField 'decodeTagObject 'encodeTagObject $
simpleField "kind" [t| TagObject |]
pTagsObject =
customField 'decodeTagObject 'encodeTagObject [tagNameField] $
simpleField "kind" [t| TagObject |]
-- | Selected output fields.
pOutputFields :: Field
......
......@@ -90,6 +90,7 @@ data Field = Field { fieldName :: String
, fieldType :: Q Type
, fieldRead :: Maybe (Q Exp)
, fieldShow :: Maybe (Q Exp)
, fieldExtraKeys :: [String]
, fieldDefault :: Maybe (Q Exp)
, fieldConstr :: Maybe String
, fieldIsOptional :: OptionalType
......@@ -102,6 +103,7 @@ simpleField fname ftype =
, fieldType = ftype
, fieldRead = Nothing
, fieldShow = Nothing
, fieldExtraKeys = []
, fieldDefault = Nothing
, fieldConstr = Nothing
, fieldIsOptional = NotOptional
......@@ -126,12 +128,14 @@ optionalNullSerField :: Field -> Field
optionalNullSerField field = field { fieldIsOptional = OptionalSerializeNull }
-- | Sets custom functions on a field.
customField :: Name -- ^ The name of the read function
-> Name -- ^ The name of the show function
-> Field -- ^ The original field
-> Field -- ^ Updated field
customField readfn showfn field =
field { fieldRead = Just (varE readfn), fieldShow = Just (varE showfn) }
customField :: Name -- ^ The name of the read function
-> Name -- ^ The name of the show function
-> [String] -- ^ The name of extra field keys
-> Field -- ^ The original field
-> Field -- ^ Updated field
customField readfn showfn extra field =
field { fieldRead = Just (varE readfn), fieldShow = Just (varE showfn)
, fieldExtraKeys = extra }
-- | Computes the record name for a given field, based on either the
-- string value in the JSON serialisation or the custom named if any
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment