When displaying query results of type Maybe, one could use the function rsMaybe. Unfortunately, this function maps 'Nothing' values to RSNoData which gets displayed as '?' in the list of query results. These semantics do not fit if the result is of a Maybe type, because it is an optional field, because in this case 'Nothing' is not an error (like for example the consequence of an RPC error), but a legitimate result and as such should be displayed as '-' in the result list. For this purpose, this patch introduces another version of rsMaybe, which is called rsMaybeUnavail to be used for optional fields.
Signed-off-by: Helga Velroyen <[email protected]> --- src/Ganeti/Query/Common.hs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Ganeti/Query/Common.hs b/src/Ganeti/Query/Common.hs index 67d1547..04cc164 100644 --- a/src/Ganeti/Query/Common.hs +++ b/src/Ganeti/Query/Common.hs @@ -28,6 +28,7 @@ module Ganeti.Query.Common , rsUnavail , rsNormal , rsMaybe + , rsMaybeUnavail , rsUnknown , missingRuntime , rpcErrorToStatus @@ -79,10 +80,18 @@ rsNormal a = ResultEntry RSNormal $ Just (showJSON a) -- missing, in which case we return no data). Note that there's some -- ambiguity here: in some cases, we mean 'RSNoData', but in other -- 'RSUnavail'; this is easy to solve in simple cases, but not in --- nested dicts. +-- nested dicts. If you want to return 'RSUnavail' in case of 'Nothing' +-- use the function 'rsMaybeUnavail'. rsMaybe :: (JSON a) => Maybe a -> ResultEntry rsMaybe = maybe rsNoData rsNormal +-- | Helper to declare a result from a 'Maybe'. This version returns +-- a 'RSUnavail' in case of 'Nothing'. It should be used for optional +-- fields that are not set. For cases where 'Nothing' means that there +-- was an error, consider using 'rsMaybe' instead. +rsMaybeUnavail :: (JSON a) => Maybe a -> ResultEntry +rsMaybeUnavail = maybe rsUnavail rsNormal + -- | Helper for unknown field result. rsUnknown :: ResultEntry rsUnknown = ResultEntry RSUnknown Nothing -- 1.8.1
