Commit 0538c375 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

objects: Add custom de-/serializing code for query responses



… and use them in cmdlib.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarRené Nussbaumer <rn@google.com>
parent 28b71a76
......@@ -550,9 +550,7 @@ class _QueryBase:
else:
fdefs = query.Query(cls.FIELDS, fields).GetFields()
return {
"fields": [fdef.ToDict() for fdef in fdefs],
}
return objects.QueryFieldsResponse(fields=fdefs).ToDict()
def ExpandNames(self, lu):
"""Expand names for this query.
......@@ -584,11 +582,8 @@ class _QueryBase:
"""
data = self._GetQueryData(lu)
return {
"data": self.query.Query(data),
"fields": [fdef.ToDict()
for fdef in self.query.GetFields()],
}
return objects.QueryResponse(data=self.query.Query(data),
fields=self.query.GetFields()).ToDict()
def OldStyleQuery(self, lu):
"""Collect data and execute query.
......
......@@ -1392,6 +1392,29 @@ class QueryFieldDefinition(ConfigObject):
]
class _QueryResponseBase(ConfigObject):
__slots__ = [
"fields",
]
def ToDict(self):
"""Custom function for serializing.
"""
mydict = super(_QueryResponseBase, self).ToDict()
mydict["fields"] = self._ContainerToDicts(mydict["fields"])
return mydict
@classmethod
def FromDict(cls, val):
"""Custom function for de-serializing.
"""
obj = super(_QueryResponseBase, cls).FromDict(val)
obj.fields = cls._ContainerFromDicts(obj.fields, list, QueryFieldDefinition)
return obj
class QueryRequest(ConfigObject):
"""Object holding a query request.
......@@ -1403,7 +1426,7 @@ class QueryRequest(ConfigObject):
]
class QueryResponse(ConfigObject):
class QueryResponse(_QueryResponseBase):
"""Object holding the response to a query.
@ivar fields: List of L{QueryFieldDefinition} objects
......@@ -1411,7 +1434,6 @@ class QueryResponse(ConfigObject):
"""
__slots__ = [
"fields",
"data",
]
......@@ -1426,14 +1448,13 @@ class QueryFieldsRequest(ConfigObject):
]
class QueryFieldsResponse(ConfigObject):
class QueryFieldsResponse(_QueryResponseBase):
"""Object holding the response to a query for fields.
@ivar fields: List of L{QueryFieldDefinition} objects
"""
__slots__ = [
"fields",
]
......
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