Skip to content
Snippets Groups Projects
Commit d7cf83bf authored by Iustin Pop's avatar Iustin Pop
Browse files

Simplify some JSON transforms

... hopefully this is more clear.
parent aab26f2d
No related branches found
No related tags found
No related merge requests found
...@@ -54,18 +54,14 @@ fromObj k o = ...@@ -54,18 +54,14 @@ fromObj k o =
Just val -> resultToEither $ readJSON val Just val -> resultToEither $ readJSON val
getStringElement :: String -> JSObject JSValue -> Either String String getStringElement :: String -> JSObject JSValue -> Either String String
getStringElement key o = fromObj key o getStringElement = fromObj
getIntElement :: String -> JSObject JSValue -> Either String String getIntElement :: String -> JSObject JSValue -> Either String Int
getIntElement key o = getIntElement = fromObj
let tmp = (fromObj key o)::Either String Int
in case tmp of
Left x -> Left x
Right x -> Right $ show x
getListElement :: String -> JSObject JSValue getListElement :: String -> JSObject JSValue
-> Either String [JSValue] -> Either String [JSValue]
getListElement key o = fromObj key o getListElement = fromObj
readString :: JSValue -> Either String String readString :: JSValue -> Either String String
readString v = readString v =
...@@ -78,6 +74,12 @@ concatElems :: Either String String ...@@ -78,6 +74,12 @@ concatElems :: Either String String
-> Either String String -> Either String String
concatElems = apply2 (\x y -> x ++ "|" ++ y) concatElems = apply2 (\x y -> x ++ "|" ++ y)
apply1 :: (a -> b) -> Either String a -> Either String b
apply1 fn a =
case a of
Left x -> Left x
Right y -> Right $ fn y
apply2 :: (a -> b -> c) apply2 :: (a -> b -> c)
-> Either String a -> Either String a
-> Either String b -> Either String b
...@@ -137,7 +139,7 @@ parseInstance :: JSObject JSValue -> Either String String ...@@ -137,7 +139,7 @@ parseInstance :: JSObject JSValue -> Either String String
parseInstance a = parseInstance a =
let name = getStringElement "name" a let name = getStringElement "name" a
disk = case getIntElement "disk_usage" a of disk = case getIntElement "disk_usage" a of
Left _ -> apply2 (\x y -> show $ ((read x)::Int) + ((read y)::Int)) Left _ -> apply2 (+)
(getIntElement "sda_size" a) (getIntElement "sda_size" a)
(getIntElement "sdb_size" a) (getIntElement "sdb_size" a)
Right x -> Right x Right x -> Right x
...@@ -146,10 +148,12 @@ parseInstance a = ...@@ -146,10 +148,12 @@ parseInstance a =
snode = (listHead $ getListElement "snodes" a) `combine` readString snode = (listHead $ getListElement "snodes" a) `combine` readString
mem = case bep of mem = case bep of
Left _ -> getIntElement "admin_ram" a Left _ -> getIntElement "admin_ram" a
Right _ -> bep Right o -> getIntElement "memory" o
in in
concatElems name $ concatElems mem $ concatElems name $
concatElems disk $ concatElems pnode snode concatElems (show `apply1` mem) $
concatElems (show `apply1` disk) $
concatElems pnode snode
parseNode :: JSObject JSValue -> Either String String parseNode :: JSObject JSValue -> Either String String
parseNode a = parseNode a =
...@@ -158,5 +162,7 @@ parseNode a = ...@@ -158,5 +162,7 @@ parseNode a =
mfree = getIntElement "mfree" a mfree = getIntElement "mfree" a
dtotal = getIntElement "dtotal" a dtotal = getIntElement "dtotal" a
dfree = getIntElement "dfree" a dfree = getIntElement "dfree" a
in concatElems name $ concatElems mtotal $ concatElems mfree $ in concatElems name $
concatElems dtotal dfree concatElems (show `apply1` mtotal) $
concatElems (show `apply1` mfree) $
concatElems (show `apply1` dtotal) (show `apply1` dfree)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment