[Libguestfs] [PATCH 1/5] builder: extract Yajl helper functions to yajl.ml

Cédric Bosdonnat
Tue, 03 Jan 2017 10:18:52 -0000

Extract the yajl object_get_* helpers in the Yajl module since this
could be useful for any Yajl user code.
---
 builder/simplestreams_parser.ml | 52 --------------------------------------
 builder/yajl.ml                 | 55 +++++++++++++++++++++++++++++++++++++++++
 builder/yajl.mli                | 29 ++++++++++++++++++++++
 3 files changed, 84 insertions(+), 52 deletions(-)

diff --git a/builder/simplestreams_parser.ml b/builder/simplestreams_parser.ml
index 81e70189a..8844d476b 100644
--- a/builder/simplestreams_parser.ml
+++ b/builder/simplestreams_parser.ml
@@ -28,58 +28,6 @@ let ensure_trailing_slash str    if String.length str > 0
&& str.[String.length str - 1] <> '/' then str ^
"/"
   else str

-let object_find_optional key = function
-  | Yajl_object o ->
-    (match List.filter (fun (k, _) -> k = key) (Array.to_list o) with
-    | [(k, v)] -> Some v
-    | [] -> None
-    | _ -> error (f_"more than value for the key '%s'")
key)
-  | _ -> error (f_"the value of the key '%s' is not an
object") key
-
-let object_find key yv -  match object_find_optional key yv with
-  | None -> error (f_"missing value for the key '%s'") key
-  | Some v -> v
-
-let object_get_string key yv -  match object_find key yv with
-  | Yajl_string s -> s
-  | _ -> error (f_"the value for the key '%s' is not a
string") key
-
-let object_find_object key yv -  match object_find key yv with
-  | Yajl_object _ as o -> o
-  | _ -> error (f_"the value for the key '%s' is not an
object") key
-
-let object_find_objects fn = function
-  | Yajl_object o -> filter_map fn (Array.to_list o)
-  | _ -> error (f_"the value is not an object")
-
-let object_get_object key yv -  match object_find_object key yv with
-  | Yajl_object o -> o
-  | _ -> assert false (* object_find_object already errors out. *)
-
-let object_get_number key yv -  match object_find key yv with
-  | Yajl_number n -> n
-  | Yajl_double d -> Int64.of_float d
-  | _ -> error (f_"the value for the key '%s' is not an
integer") key
-
-let objects_get_string key yvs -  let rec loop = function
-    | [] -> None
-    | x :: xs ->
-      (match object_find_optional key x with
-      | Some (Yajl_string s) -> Some s
-      | Some _ -> error (f_"the value for key '%s' is not a
string as expected") key
-      | None -> loop xs
-      )
-  in
-  match loop yvs with
-  | Some s -> s
-  | None -> error (f_"the key '%s' was not found in a list of
objects") key
-
 let get_index ~downloader ~sigchecker
   { Sources.uri = uri; proxy = proxy }
diff --git a/builder/yajl.ml b/builder/yajl.ml
index 00e4dac4b..e53706abc 100644
--- a/builder/yajl.ml
+++ b/builder/yajl.ml
@@ -16,6 +16,9 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *)

+open Common_gettext.Gettext
+open Common_utils
+
 type yajl_val  | Yajl_null
 | Yajl_string of string
@@ -26,3 +29,55 @@ type yajl_val  | Yajl_bool of bool

 external yajl_tree_parse : string -> yajl_val =
"virt_builder_yajl_tree_parse"
+
+let object_find_optional key = function
+  | Yajl_object o ->
+    (match List.filter (fun (k, _) -> k = key) (Array.to_list o) with
+    | [(k, v)] -> Some v
+    | [] -> None
+    | _ -> error (f_"more than value for the key '%s'")
key)
+  | _ -> error (f_"the value of the key '%s' is not an
object") key
+
+let object_find key yv +  match object_find_optional key yv with
+  | None -> error (f_"missing value for the key '%s'") key
+  | Some v -> v
+
+let object_get_string key yv +  match object_find key yv with
+  | Yajl_string s -> s
+  | _ -> error (f_"the value for the key '%s' is not a
string") key
+
+let object_find_object key yv +  match object_find key yv with
+  | Yajl_object _ as o -> o
+  | _ -> error (f_"the value for the key '%s' is not an
object") key
+
+let object_find_objects fn = function
+  | Yajl_object o -> filter_map fn (Array.to_list o)
+  | _ -> error (f_"the value is not an object")
+
+let object_get_object key yv +  match object_find_object key yv with
+  | Yajl_object o -> o
+  | _ -> assert false (* object_find_object already errors out. *)
+
+let object_get_number key yv +  match object_find key yv with
+  | Yajl_number n -> n
+  | Yajl_double d -> Int64.of_float d
+  | _ -> error (f_"the value for the key '%s' is not an
integer") key
+
+let objects_get_string key yvs +  let rec loop = function
+    | [] -> None
+    | x :: xs ->
+      (match object_find_optional key x with
+      | Some (Yajl_string s) -> Some s
+      | Some _ -> error (f_"the value for key '%s' is not a
string as expected") key
+      | None -> loop xs
+      )
+  in
+  match loop yvs with
+  | Some s -> s
+  | None -> error (f_"the key '%s' was not found in a list of
objects") key
diff --git a/builder/yajl.mli b/builder/yajl.mli
index 9771e5399..ca0eb92f4 100644
--- a/builder/yajl.mli
+++ b/builder/yajl.mli
@@ -27,3 +27,32 @@ type yajl_val
 val yajl_tree_parse : string -> yajl_val
 (** Parse the JSON string. *)
+
+val object_get_string : string -> yajl_val -> string
+(** [object_get_string key yv] gets the value of the [key] field as a string
+    in the [yv] structure *)
+
+val object_find_object : string -> yajl_val -> yajl_val
+(** [object_get_object key yv] gets the value of the [key] field as a yajl
+    value in the [yv] structure.
+
+    Mind the returned type is different from [object_get_object] *)
+
+val object_get_object : string -> yajl_val -> (string * yajl_val) array
+(** [object_get_object key yv] gets the value of the [key] field as a Yajl
+    object in the [yv] structure *)
+
+val object_get_number : string -> yajl_val -> int64
+(** [object_get_number key yv] gets the value of the [key] field as an
+    integer in the [yv] structure *)
+
+val objects_get_string : string -> yajl_val list -> string
+(** [objects_get_string key yvs] gets the value of the [key] field as a string
+    in an [yvs] list of yajl_val structure.
+
+    The key may not be found at all in the list, in which case an error
+    is raised *)
+
+val object_find_objects : ((string * yajl_val) -> 'a option) ->
yajl_val -> 'a list
+(** [object_find_objects fn obj] returns all the Yajl objects matching the [fn]
+    function in [obj] list. *)
--
2.11.0