Revision 5aa48dbe Ganeti/HTools/Utils.hs
b/Ganeti/HTools/Utils.hs | ||
---|---|---|
57 | 57 |
return = Ok |
58 | 58 |
fail = Bad |
59 | 59 |
|
60 |
fromJResult :: J.Result a -> Result a
|
|
61 |
fromJResult (J.Error x) = Bad x
|
|
62 |
fromJResult (J.Ok x) = Ok x
|
|
60 |
fromJResult :: Monad m => J.Result a -> m a
|
|
61 |
fromJResult (J.Error x) = fail x
|
|
62 |
fromJResult (J.Ok x) = return x
|
|
63 | 63 |
|
64 | 64 |
-- | Comma-join a string list. |
65 | 65 |
commaJoin :: [String] -> String |
... | ... | |
111 | 111 |
exitWith $ ExitFailure 1 |
112 | 112 |
Ok x -> return x) |
113 | 113 |
|
114 |
readEitherString :: J.JSValue -> Result String
|
|
114 |
readEitherString :: (Monad m) => J.JSValue -> m String
|
|
115 | 115 |
readEitherString v = |
116 | 116 |
case v of |
117 |
J.JSString s -> Ok $ J.fromJSString s
|
|
118 |
_ -> Bad "Wrong JSON type"
|
|
117 |
J.JSString s -> return $ J.fromJSString s
|
|
118 |
_ -> fail "Wrong JSON type"
|
|
119 | 119 |
|
120 |
loadJSArray :: String -> Result [J.JSObject J.JSValue]
|
|
120 |
loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
|
|
121 | 121 |
loadJSArray s = fromJResult $ J.decodeStrict s |
122 | 122 |
|
123 |
fromObj :: J.JSON a => String -> J.JSObject J.JSValue -> Result a
|
|
123 |
fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
|
|
124 | 124 |
fromObj k o = |
125 | 125 |
case lookup k (J.fromJSObject o) of |
126 |
Nothing -> Bad $ printf "key '%s' not found" k
|
|
126 |
Nothing -> fail $ printf "key '%s' not found" k
|
|
127 | 127 |
Just val -> fromJResult $ J.readJSON val |
128 | 128 |
|
129 |
getStringElement :: String -> J.JSObject J.JSValue -> Result String
|
|
129 |
getStringElement :: (Monad m) => String -> J.JSObject J.JSValue -> m String
|
|
130 | 130 |
getStringElement = fromObj |
131 | 131 |
|
132 |
getIntElement :: String -> J.JSObject J.JSValue -> Result Int
|
|
132 |
getIntElement :: (Monad m) => String -> J.JSObject J.JSValue -> m Int
|
|
133 | 133 |
getIntElement = fromObj |
134 | 134 |
|
135 |
getBoolElement :: String -> J.JSObject J.JSValue -> Result Bool
|
|
135 |
getBoolElement :: (Monad m) => String -> J.JSObject J.JSValue -> m Bool
|
|
136 | 136 |
getBoolElement = fromObj |
137 | 137 |
|
138 |
getListElement :: String -> J.JSObject J.JSValue -> Result [J.JSValue]
|
|
138 |
getListElement :: (Monad m) => String -> J.JSObject J.JSValue -> m [J.JSValue]
|
|
139 | 139 |
getListElement = fromObj |
140 | 140 |
|
141 |
getObjectElement :: String -> J.JSObject J.JSValue |
|
142 |
-> Result (J.JSObject J.JSValue)
|
|
141 |
getObjectElement :: (Monad m) => String -> J.JSObject J.JSValue
|
|
142 |
-> m (J.JSObject J.JSValue)
|
|
143 | 143 |
getObjectElement = fromObj |
144 | 144 |
|
145 |
asJSObject :: J.JSValue -> Result (J.JSObject J.JSValue)
|
|
146 |
asJSObject (J.JSObject a) = Ok a
|
|
147 |
asJSObject _ = Bad "not an object"
|
|
145 |
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
|
|
146 |
asJSObject (J.JSObject a) = return a
|
|
147 |
asJSObject _ = fail "not an object"
|
|
148 | 148 |
|
149 |
asObjectList :: [J.JSValue] -> Result [J.JSObject J.JSValue]
|
|
149 |
asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
|
|
150 | 150 |
asObjectList = sequence . map asJSObject |
151 | 151 |
|
152 | 152 |
-- | Function to concat two strings with a separator under a monad |
Also available in: Unified diff