Revision 9188aeef Ganeti/HTools/Utils.hs
b/Ganeti/HTools/Utils.hs | ||
---|---|---|
26 | 26 |
|
27 | 27 |
import Debug.Trace |
28 | 28 |
|
29 |
-- * Debug functions |
|
30 |
|
|
29 | 31 |
-- | To be used only for debugging, breaks referential integrity. |
30 | 32 |
debug :: Show a => a -> a |
31 | 33 |
debug x = trace (show x) x |
32 | 34 |
|
33 |
|
|
34 |
fromJResult :: Monad m => J.Result a -> m a |
|
35 |
fromJResult (J.Error x) = fail x |
|
36 |
fromJResult (J.Ok x) = return x |
|
35 |
-- * Miscelaneous |
|
37 | 36 |
|
38 | 37 |
-- | Comma-join a string list. |
39 | 38 |
commaJoin :: [String] -> String |
... | ... | |
53 | 52 |
commaSplit :: String -> [String] |
54 | 53 |
commaSplit = sepSplit ',' |
55 | 54 |
|
55 |
-- * Mathematical functions |
|
56 |
|
|
56 | 57 |
-- Simple and slow statistical functions, please replace with better versions |
57 | 58 |
|
58 | 59 |
-- | Mean value of a list. |
... | ... | |
72 | 73 |
varianceCoeff :: Floating a => [a] -> a |
73 | 74 |
varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst) |
74 | 75 |
|
75 |
-- | Get an Ok result or print the error and exit |
|
76 |
-- | Get an Ok result or print the error and exit.
|
|
76 | 77 |
readData :: Result a -> IO a |
77 | 78 |
readData nd = |
78 | 79 |
(case nd of |
... | ... | |
81 | 82 |
exitWith $ ExitFailure 1 |
82 | 83 |
Ok x -> return x) |
83 | 84 |
|
85 |
-- * JSON-related functions |
|
86 |
|
|
87 |
-- | Converts a JSON Result into a monadic value. |
|
88 |
fromJResult :: Monad m => J.Result a -> m a |
|
89 |
fromJResult (J.Error x) = fail x |
|
90 |
fromJResult (J.Ok x) = return x |
|
91 |
|
|
92 |
-- | Tries to read a string from a JSON value. |
|
93 |
-- |
|
94 |
-- In case the value was not a string, we fail the read (in the |
|
95 |
-- context of the current monad. |
|
84 | 96 |
readEitherString :: (Monad m) => J.JSValue -> m String |
85 | 97 |
readEitherString v = |
86 | 98 |
case v of |
87 | 99 |
J.JSString s -> return $ J.fromJSString s |
88 | 100 |
_ -> fail "Wrong JSON type" |
89 | 101 |
|
102 |
-- | Converts a JSON message into an array of JSON objects. |
|
90 | 103 |
loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue] |
91 | 104 |
loadJSArray s = fromJResult $ J.decodeStrict s |
92 | 105 |
|
106 |
-- | Reads a the value of a key in a JSON object. |
|
93 | 107 |
fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a |
94 | 108 |
fromObj k o = |
95 | 109 |
case lookup k (J.fromJSObject o) of |
96 | 110 |
Nothing -> fail $ printf "key '%s' not found in %s" k (show o) |
97 | 111 |
Just val -> fromJResult $ J.readJSON val |
98 | 112 |
|
113 |
-- | Converts a JSON value into a JSON object. |
|
99 | 114 |
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue) |
100 | 115 |
asJSObject (J.JSObject a) = return a |
101 | 116 |
asJSObject _ = fail "not an object" |
102 | 117 |
|
118 |
-- | Coneverts a list of JSON values into a list of JSON objects. |
|
103 | 119 |
asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue] |
104 | 120 |
asObjectList = sequence . map asJSObject |
Also available in: Unified diff