Revision f047f90f htools/Ganeti/HTools/Utils.hs
b/htools/Ganeti/HTools/Utils.hs | ||
---|---|---|
49 | 49 |
, parseUnit |
50 | 50 |
) where |
51 | 51 |
|
52 |
import Control.Monad (liftM) |
|
53 | 52 |
import Data.Char (toUpper) |
54 | 53 |
import Data.List |
55 |
import Data.Maybe (fromMaybe) |
|
56 | 54 |
import qualified Text.JSON as J |
57 |
import Text.Printf (printf) |
|
58 | 55 |
|
59 | 56 |
import Debug.Trace |
60 | 57 |
|
61 | 58 |
import Ganeti.HTools.Types |
59 |
-- we will re-export these for our existing users |
|
60 |
import Ganeti.HTools.JSON |
|
62 | 61 |
|
63 | 62 |
-- * Debug functions |
64 | 63 |
|
... | ... | |
131 | 130 |
-> a -- ^ first result which has a True condition, or default |
132 | 131 |
select def = maybe def snd . find fst |
133 | 132 |
|
134 |
-- * JSON-related functions |
|
135 |
|
|
136 |
-- | A type alias for the list-based representation of J.JSObject. |
|
137 |
type JSRecord = [(String, J.JSValue)] |
|
138 |
|
|
139 |
-- | Converts a JSON Result into a monadic value. |
|
140 |
fromJResult :: Monad m => String -> J.Result a -> m a |
|
141 |
fromJResult s (J.Error x) = fail (s ++ ": " ++ x) |
|
142 |
fromJResult _ (J.Ok x) = return x |
|
143 |
|
|
144 |
-- | Tries to read a string from a JSON value. |
|
145 |
-- |
|
146 |
-- In case the value was not a string, we fail the read (in the |
|
147 |
-- context of the current monad. |
|
148 |
readEitherString :: (Monad m) => J.JSValue -> m String |
|
149 |
readEitherString v = |
|
150 |
case v of |
|
151 |
J.JSString s -> return $ J.fromJSString s |
|
152 |
_ -> fail "Wrong JSON type" |
|
153 |
|
|
154 |
-- | Converts a JSON message into an array of JSON objects. |
|
155 |
loadJSArray :: (Monad m) |
|
156 |
=> String -- ^ Operation description (for error reporting) |
|
157 |
-> String -- ^ Input message |
|
158 |
-> m [J.JSObject J.JSValue] |
|
159 |
loadJSArray s = fromJResult s . J.decodeStrict |
|
160 |
|
|
161 |
-- | Reads the value of a key in a JSON object. |
|
162 |
fromObj :: (J.JSON a, Monad m) => JSRecord -> String -> m a |
|
163 |
fromObj o k = |
|
164 |
case lookup k o of |
|
165 |
Nothing -> fail $ printf "key '%s' not found, object contains only %s" |
|
166 |
k (show (map fst o)) |
|
167 |
Just val -> fromKeyValue k val |
|
168 |
|
|
169 |
-- | Reads the value of an optional key in a JSON object. |
|
170 |
maybeFromObj :: (J.JSON a, Monad m) => |
|
171 |
JSRecord -> String -> m (Maybe a) |
|
172 |
maybeFromObj o k = |
|
173 |
case lookup k o of |
|
174 |
Nothing -> return Nothing |
|
175 |
Just val -> liftM Just (fromKeyValue k val) |
|
176 |
|
|
177 |
-- | Reads the value of a key in a JSON object with a default if missing. |
|
178 |
fromObjWithDefault :: (J.JSON a, Monad m) => |
|
179 |
JSRecord -> String -> a -> m a |
|
180 |
fromObjWithDefault o k d = liftM (fromMaybe d) $ maybeFromObj o k |
|
181 |
|
|
182 |
-- | Reads a JValue, that originated from an object key. |
|
183 |
fromKeyValue :: (J.JSON a, Monad m) |
|
184 |
=> String -- ^ The key name |
|
185 |
-> J.JSValue -- ^ The value to read |
|
186 |
-> m a |
|
187 |
fromKeyValue k val = |
|
188 |
fromJResult (printf "key '%s', value '%s'" k (show val)) (J.readJSON val) |
|
189 | 133 |
|
190 | 134 |
-- | Annotate a Result with an ownership information. |
191 | 135 |
annotateResult :: String -> Result a -> Result a |
... | ... | |
201 | 145 |
-> Result a |
202 | 146 |
tryFromObj t o = annotateResult t . fromObj o |
203 | 147 |
|
204 |
-- | Small wrapper over readJSON. |
|
205 |
fromJVal :: (Monad m, J.JSON a) => J.JSValue -> m a |
|
206 |
fromJVal v = |
|
207 |
case J.readJSON v of |
|
208 |
J.Error s -> fail ("Cannot convert value '" ++ show v ++ |
|
209 |
"', error: " ++ s) |
|
210 |
J.Ok x -> return x |
|
211 |
|
|
212 |
-- | Converts a JSON value into a JSON object. |
|
213 |
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue) |
|
214 |
asJSObject (J.JSObject a) = return a |
|
215 |
asJSObject _ = fail "not an object" |
|
216 |
|
|
217 |
-- | Coneverts a list of JSON values into a list of JSON objects. |
|
218 |
asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue] |
|
219 |
asObjectList = mapM asJSObject |
|
220 | 148 |
|
221 | 149 |
-- * Parsing utility functions |
222 | 150 |
|
Also available in: Unified diff