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

htools: simplify some JSON-related code


Instead of manually encoding the JSValue(s), we can use showJSON as
long as we use data types that can be auto-converted.

Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent 7c14b50a
No related branches found
No related tags found
No related merge requests found
...@@ -32,9 +32,8 @@ import Data.Either () ...@@ -32,9 +32,8 @@ import Data.Either ()
import Data.Maybe (fromMaybe, isJust) import Data.Maybe (fromMaybe, isJust)
import Data.List import Data.List
import Control.Monad import Control.Monad
import Text.JSON (JSObject, JSValue(JSBool, JSString, JSArray), import Text.JSON (JSObject, JSValue(JSArray),
makeObj, encodeStrict, decodeStrict, makeObj, encodeStrict, decodeStrict, fromJSObject, showJSON)
fromJSObject, toJSString)
import System (exitWith, ExitCode(..)) import System (exitWith, ExitCode(..))
import System.IO import System.IO
...@@ -226,8 +225,8 @@ formatResponse :: Bool -- ^ Whether the request was successful ...@@ -226,8 +225,8 @@ formatResponse :: Bool -- ^ Whether the request was successful
-> String -- ^ The full JSON-formatted message -> String -- ^ The full JSON-formatted message
formatResponse success info result = formatResponse success info result =
let let
e_success = ("success", JSBool success) e_success = ("success", showJSON success)
e_info = ("info", JSString . toJSString $ info) e_info = ("info", showJSON info)
e_result = ("result", result) e_result = ("result", result)
in encodeStrict $ makeObj [e_success, e_info, e_result] in encodeStrict $ makeObj [e_success, e_info, e_result]
...@@ -243,8 +242,7 @@ formatEvacuate as = do ...@@ -243,8 +242,7 @@ formatEvacuate as = do
when (null elems) $ fail info when (null elems) $ fail info
let sols = map (\(_, inst, nl, _) -> Instance.name inst : map Node.name nl) let sols = map (\(_, inst, nl, _) -> Instance.name inst : map Node.name nl)
elems elems
jsols = map (JSArray . map (JSString . toJSString)) sols return (info, showJSON sols)
return (info, JSArray jsols)
-- | Convert allocation/relocation results into the result format. -- | Convert allocation/relocation results into the result format.
formatAllocate :: Cluster.AllocSolution -> Result IAllocResult formatAllocate :: Cluster.AllocSolution -> Result IAllocResult
...@@ -252,9 +250,7 @@ formatAllocate as = do ...@@ -252,9 +250,7 @@ formatAllocate as = do
let info = describeSolution as let info = describeSolution as
case Cluster.asSolutions as of case Cluster.asSolutions as of
[] -> fail info [] -> fail info
(_, _, nodes, _):[] -> do (_, _, nodes, _):[] -> return (info, showJSON $ map (Node.name) nodes)
let nodes' = map Node.name nodes
return (info, JSArray $ map (JSString . toJSString) nodes')
_ -> fail "Internal error: multiple allocation solutions" _ -> fail "Internal error: multiple allocation solutions"
-- | Process a request and return new node lists -- | Process a request and return new node lists
......
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