Revision 117dc2d8 Ganeti/HTools/Utils.hs
b/Ganeti/HTools/Utils.hs | ||
---|---|---|
31 | 31 |
, readEitherString |
32 | 32 |
, loadJSArray |
33 | 33 |
, fromObj |
34 |
, tryFromObj |
|
35 |
, fromJVal |
|
34 | 36 |
, asJSObject |
35 | 37 |
, asObjectList |
36 | 38 |
, fromJResult |
37 | 39 |
, tryRead |
38 | 40 |
, formatTable |
41 |
, annotateResult |
|
39 | 42 |
) where |
40 | 43 |
|
41 | 44 |
import Data.List |
... | ... | |
45 | 48 |
|
46 | 49 |
import Debug.Trace |
47 | 50 |
|
51 |
import Ganeti.HTools.Types |
|
52 |
|
|
48 | 53 |
-- * Debug functions |
49 | 54 |
|
50 | 55 |
-- | To be used only for debugging, breaks referential integrity. |
... | ... | |
119 | 124 |
Nothing -> fail $ printf "key '%s' not found in %s" k (show o) |
120 | 125 |
Just val -> fromJResult $ J.readJSON val |
121 | 126 |
|
127 |
-- | Annotate a Result with an ownership information |
|
128 |
annotateResult :: String -> Result a -> Result a |
|
129 |
annotateResult owner (Bad s) = Bad $ owner ++ ": " ++ s |
|
130 |
annotateResult _ v = v |
|
131 |
|
|
132 |
-- | Try to extract a key from a object with better error reporting |
|
133 |
-- than fromObj |
|
134 |
tryFromObj :: (J.JSON a) => |
|
135 |
String -> [(String, J.JSValue)] -> String -> Result a |
|
136 |
tryFromObj t o k = annotateResult (t ++ " key '" ++ k ++ "'") (fromObj k o) |
|
137 |
|
|
138 |
-- | Small wrapper over readJSON. |
|
139 |
fromJVal :: (Monad m, J.JSON a) => J.JSValue -> m a |
|
140 |
fromJVal v = |
|
141 |
case J.readJSON v of |
|
142 |
J.Error s -> fail ("Cannot convert value " ++ show v ++ ", error: " ++ s) |
|
143 |
J.Ok x -> return x |
|
144 |
|
|
122 | 145 |
-- | Converts a JSON value into a JSON object. |
123 | 146 |
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue) |
124 | 147 |
asJSObject (J.JSObject a) = return a |
Also available in: Unified diff