From 77cab6795301f33c2fb4bdb18c5aaafc31b2f8c4 Mon Sep 17 00:00:00 2001
From: Agata Murawska <agatamurawska@google.com>
Date: Mon, 24 Sep 2012 15:45:57 +0200
Subject: [PATCH] Json helper for node query

This is a helper function that gets the JSON encoding of the first
element of the list (if possible).

Signed-off-by: Agata Murawska <agatamurawska@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 htools/Ganeti/JSON.hs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/htools/Ganeti/JSON.hs b/htools/Ganeti/JSON.hs
index 132c0b223..7c7dd3423 100644
--- a/htools/Ganeti/JSON.hs
+++ b/htools/Ganeti/JSON.hs
@@ -31,6 +31,7 @@ module Ganeti.JSON
   , fromObjWithDefault
   , fromKeyValue
   , fromJVal
+  , jsonHead
   , asJSObject
   , asObjectList
   , tryFromObj
@@ -125,6 +126,11 @@ fromJVal v =
                        "', error: " ++ s)
     J.Ok x -> return x
 
+-- | Helper function that returns Null or first element of the list.
+jsonHead :: (J.JSON b) => [a] -> (a -> b) -> J.JSValue
+jsonHead [] _ = J.JSNull
+jsonHead (x:_) f = J.showJSON $ f x
+
 -- | Converts a JSON value into a JSON object.
 asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
 asJSObject (J.JSObject a) = return a
-- 
GitLab