[Libguestfs] [PATCH 3/4] mltools: JSON: combine JSON.Int and JSON.Int64 into a single variant.

Richard W.M. Jones rjones at redhat.com
Mon Aug 20 16:02:05 UTC 2018


It was convenient to have these as separate variants when we were only
using this type to generate JSON.  However if we also use it to parse
JSON documents then integers in the document should only map to a
single variant.
---
 builder/list_entries.ml      |  4 ++--
 common/mltools/JSON.ml       |  8 +++-----
 common/mltools/JSON.mli      |  3 +--
 common/mltools/JSON_tests.ml | 40 +++++++++++++++++++-----------------
 v2v/input_libvirt_xen_ssh.ml |  2 +-
 v2v/input_ova.ml             |  4 ++--
 v2v/input_vmx.ml             |  2 +-
 v2v/output_rhv_upload.ml     |  2 +-
 v2v/parse_libvirt_xml.ml     |  4 ++--
 v2v/utils.ml                 |  4 ++--
 v2v/vCenter.ml               |  4 ++--
 11 files changed, 38 insertions(+), 39 deletions(-)

diff --git a/builder/list_entries.ml b/builder/list_entries.ml
index c64d554a2..f1f67290c 100644
--- a/builder/list_entries.ml
+++ b/builder/list_entries.ml
@@ -117,7 +117,7 @@ and list_entries_json ~sources index =
           | None -> item
           | Some str -> ("full-name", JSON.String str) :: item in
         let item = ("arch", JSON.String (Index.string_of_arch arch)) :: item in
-        let item = ("size", JSON.Int64 size) :: item in
+        let item = ("size", JSON.Int size) :: item in
         let item =
           match compressed_size with
           | None -> item
@@ -148,7 +148,7 @@ and list_entries_json ~sources index =
         JSON.Dict (List.rev item)
     ) index in
   let doc = [
-    "version", JSON.Int 1;
+    "version", JSON.Int 1L;
     "sources", JSON.List json_sources;
     "templates", JSON.List json_templates;
   ] in
diff --git a/common/mltools/JSON.ml b/common/mltools/JSON.ml
index a51037ab4..8c2e695e2 100644
--- a/common/mltools/JSON.ml
+++ b/common/mltools/JSON.ml
@@ -21,8 +21,7 @@
 type field = string * json_t
 and json_t =
   | String of string
-  | Int of int
-  | Int64 of int64
+  | Int of int64
   | Float of float
   | Bool of bool
   | List of json_t list
@@ -108,14 +107,13 @@ and output_list fields ~fmt ~indent =
 
 and output_field ~indent ~fmt = function
   | String s -> json_quote_string s
-  | Int i -> string_of_int i
-  | Bool b -> if b then "true" else "false"
-  | Int64 i -> Int64.to_string i
+  | Int i -> Int64.to_string i
   (* The JSON standard permits either "1" or "1.0" but not "1.".
    * OCaml string_of_float will generate "1.", but the %g formatter
    * will only generate the valid JSON values.
    *)
   | Float f -> Printf.sprintf "%g" f
+  | Bool b -> if b then "true" else "false"
   | List l -> output_list ~indent:(indent + 1) ~fmt l
   | Dict d -> output_dict ~indent:(indent + 1) ~fmt d
 
diff --git a/common/mltools/JSON.mli b/common/mltools/JSON.mli
index 06a777c20..c85b786ff 100644
--- a/common/mltools/JSON.mli
+++ b/common/mltools/JSON.mli
@@ -21,8 +21,7 @@
 type field = string * json_t    (** ["field": "value"] *)
 and json_t =                    (** JSON value. *)
   | String of string            (** string value, eg. ["string"] *)
-  | Int of int                  (** int value, eg. [99] *)
-  | Int64 of int64              (** int64 value, eg. [99] *)
+  | Int of int64                (** int value, eg. [99] *)
   | Float of float              (** floating point value, eg. [9.9] *)
   | Bool of bool                (** boolean value, [true] or [false] *)
   | List of json_t list         (** array value, eg. [[1,2,3]] *)
diff --git a/common/mltools/JSON_tests.ml b/common/mltools/JSON_tests.ml
index 2f3998e6e..6bd98af26 100644
--- a/common/mltools/JSON_tests.ml
+++ b/common/mltools/JSON_tests.ml
@@ -18,6 +18,8 @@
 
 (* This file tests the JSON module. *)
 
+open Std_utils
+
 open OUnit2
 
 (* Utils. *)
@@ -53,21 +55,21 @@ let test_bool ctx =
     (JSON.string_of_doc ~fmt:JSON.Indented doc)
 
 let test_int ctx =
-  let doc = [ "test_zero", JSON.Int 0;
-              "test_pos", JSON.Int 5;
-              "test_neg", JSON.Int (-5);
-              "test_pos64", JSON.Int64 (Int64.of_int 10);
-              "test_neg64", JSON.Int64 (Int64.of_int (-10)); ] in
+  let doc = [ "test_zero", JSON.Int 0L;
+              "test_pos", JSON.Int 5L;
+              "test_neg", JSON.Int (-5L);
+              "test_pos64", JSON.Int 1_000_000_000_000L;
+              "test_neg64", JSON.Int (-1_000_000_000_000L); ] in
   assert_equal_string
-    "{ \"test_zero\": 0, \"test_pos\": 5, \"test_neg\": -5, \"test_pos64\": 10, \"test_neg64\": -10 }"
+    "{ \"test_zero\": 0, \"test_pos\": 5, \"test_neg\": -5, \"test_pos64\": 1000000000000, \"test_neg64\": -1000000000000 }"
     (JSON.string_of_doc doc);
   assert_equal_string
     "{
   \"test_zero\": 0,
   \"test_pos\": 5,
   \"test_neg\": -5,
-  \"test_pos64\": 10,
-  \"test_neg64\": -10
+  \"test_pos64\": 1000000000000,
+  \"test_neg64\": -1000000000000
 }"
     (JSON.string_of_doc ~fmt:JSON.Indented doc)
 
@@ -91,7 +93,7 @@ let test_float ctx =
     (JSON.string_of_doc ~fmt:JSON.Indented doc)
 
 let test_list ctx =
-  let doc = [ "item", JSON.List [ JSON.String "foo"; JSON.Int 10; JSON.Bool true ] ] in
+  let doc = [ "item", JSON.List [ JSON.String "foo"; JSON.Int 10L; JSON.Bool true ] ] in
   assert_equal_string
     "{ \"item\": [ \"foo\", 10, true ] }"
     (JSON.string_of_doc doc);
@@ -107,8 +109,8 @@ let test_list ctx =
 
 let test_nested_dict ctx =
   let doc = [
-      "item", JSON.Dict [ "int", JSON.Int 5; "string", JSON.String "foo"; ];
-      "last", JSON.Int 10;
+      "item", JSON.Dict [ "int", JSON.Int 5L; "string", JSON.String "foo"; ];
+      "last", JSON.Int 10L;
     ] in
   assert_equal_string
     "{ \"item\": { \"int\": 5, \"string\": \"foo\" }, \"last\": 10 }"
@@ -125,10 +127,10 @@ let test_nested_dict ctx =
 
 let test_nested_nested_dict ctx =
   let doc = [
-      "item", JSON.Dict [ "int", JSON.Int 5;
-        "item2", JSON.Dict [ "int", JSON.Int 0; ];
+      "item", JSON.Dict [ "int", JSON.Int 5L;
+        "item2", JSON.Dict [ "int", JSON.Int 0L; ];
       ];
-      "last", JSON.Int 10;
+      "last", JSON.Int 10L;
     ] in
   assert_equal_string
     "{ \"item\": { \"int\": 5, \"item2\": { \"int\": 0 } }, \"last\": 10 }"
@@ -159,8 +161,8 @@ let test_qemu ctx =
   let doc = [
     "file.driver", JSON.String "https";
     "file.url", JSON.String "https://libguestfs.org";
-    "file.timeout", JSON.Int 60;
-    "file.readahead", JSON.Int (64 * 1024 * 1024);
+    "file.timeout", JSON.Int 60L;
+    "file.readahead", JSON.Int (64L *^ 1024L *^ 1024L);
   ] in
   assert_equal_string
     "{ \"file.driver\": \"https\", \"file.url\": \"https://libguestfs.org\", \"file.timeout\": 60, \"file.readahead\": 67108864 }"
@@ -176,7 +178,7 @@ let test_qemu ctx =
 
 let test_builder ctx =
   let doc = [
-    "version", JSON.Int 1;
+    "version", JSON.Int 1L;
     "sources", JSON.List [
       JSON.Dict [
         "uri", JSON.String "http://libguestfs.org/index";
@@ -187,7 +189,7 @@ let test_builder ctx =
         "os-version", JSON.String "phony-debian";
         "full-name", JSON.String "Phony Debian";
         "arch", JSON.String "x86_64";
-        "size", JSON.Int64 536870912_L;
+        "size", JSON.Int 536870912_L;
         "notes", JSON.Dict [
           "C", JSON.String "Phony Debian look-alike used for testing.";
         ];
@@ -197,7 +199,7 @@ let test_builder ctx =
         "os-version", JSON.String "phony-fedora";
         "full-name", JSON.String "Phony Fedora";
         "arch", JSON.String "x86_64";
-        "size", JSON.Int64 1073741824_L;
+        "size", JSON.Int 1073741824_L;
         "notes", JSON.Dict [
           "C", JSON.String "Phony Fedora look-alike used for testing.";
         ];
diff --git a/v2v/input_libvirt_xen_ssh.ml b/v2v/input_libvirt_xen_ssh.ml
index 597957f92..c4b671490 100644
--- a/v2v/input_libvirt_xen_ssh.ml
+++ b/v2v/input_libvirt_xen_ssh.ml
@@ -83,7 +83,7 @@ object
           (* qemu will actually assert-fail if you send the port
            * number as a string ...
            *)
-          | i -> ("file.port", JSON.Int i) :: json_params in
+          | i -> ("file.port", JSON.Int (Int64.of_int i)) :: json_params in
 
         let json_params =
           match parsed_uri.uri_user with
diff --git a/v2v/input_ova.ml b/v2v/input_ova.ml
index bfd72dee4..d5c9f1203 100644
--- a/v2v/input_ova.ml
+++ b/v2v/input_ova.ml
@@ -161,8 +161,8 @@ class input_ova ova = object
            let doc = [
                "file", JSON.Dict [
                            "driver", JSON.String "raw";
-                           "offset", JSON.Int64 offset;
-                           "size", JSON.Int64 size;
+                           "offset", JSON.Int offset;
+                           "size", JSON.Int size;
                            "file", JSON.Dict [
                                        "driver", JSON.String "file";
                                        "filename", JSON.String tar_path]
diff --git a/v2v/input_vmx.ml b/v2v/input_vmx.ml
index 12fb975d6..30649b33b 100644
--- a/v2v/input_vmx.ml
+++ b/v2v/input_vmx.ml
@@ -245,7 +245,7 @@ and qemu_uri_of_filename vmx_source filename =
        match port_of_uri uri with
        | None -> json_params
        | Some port ->
-          ("file.port", JSON.Int port) :: json_params in
+          ("file.port", JSON.Int (Int64.of_int port)) :: json_params in
 
      "json:" ^ JSON.string_of_doc json_params, format
 
diff --git a/v2v/output_rhv_upload.ml b/v2v/output_rhv_upload.ml
index 966323cae..f03e1ede3 100644
--- a/v2v/output_rhv_upload.ml
+++ b/v2v/output_rhv_upload.ml
@@ -276,7 +276,7 @@ object
 
         let disk_size = ov.ov_virtual_size in
         let json_params =
-          ("disk_size", JSON.Int64 disk_size) :: json_params in
+          ("disk_size", JSON.Int disk_size) :: json_params in
 
         (* Ask the plugin to write the disk ID to a special file. *)
         let diskid_file = diskid_file_of_id id in
diff --git a/v2v/parse_libvirt_xml.ml b/v2v/parse_libvirt_xml.ml
index dac99511c..255c935a6 100644
--- a/v2v/parse_libvirt_xml.ml
+++ b/v2v/parse_libvirt_xml.ml
@@ -63,8 +63,8 @@ let create_curl_qemu_uri driver host port path =
   let json_params = [
     "file.driver", JSON.String driver;  (* "http" or "https" *)
     "file.url", JSON.String url;
-    "file.timeout", JSON.Int 2000;
-    "file.readahead", JSON.Int (1024 * 1024);
+    "file.timeout", JSON.Int 2000_L;
+    "file.readahead", JSON.Int (1024_L *^ 1024_L);
     (* "file.sslverify", JSON.String "off"; XXX *)
   ] in
 
diff --git a/v2v/utils.ml b/v2v/utils.ml
index 67e2028f3..63ef91c51 100644
--- a/v2v/utils.ml
+++ b/v2v/utils.ml
@@ -114,8 +114,8 @@ let qemu_img_supports_offset_and_size () =
   let json = [
       "file", JSON.Dict [
         "driver", JSON.String "raw";
-        "offset", JSON.Int 512;
-        "size", JSON.Int 512;
+        "offset", JSON.Int 512_L;
+        "size", JSON.Int 512_L;
         "file", JSON.Dict [
           "filename", JSON.String tmp
         ]
diff --git a/v2v/vCenter.ml b/v2v/vCenter.ml
index e97d25ce1..cf124f067 100644
--- a/v2v/vCenter.ml
+++ b/v2v/vCenter.ml
@@ -78,14 +78,14 @@ let rec map_source ?readahead ?password_file dcPath uri server path =
       "file.driver", JSON.String "https";
       "file.url", JSON.String https_url;
       (* https://bugzilla.redhat.com/show_bug.cgi?id=1146007#c10 *)
-      "file.timeout", JSON.Int 2000;
+      "file.timeout", JSON.Int 2000_L;
     ] in
 
     let json_params =
       match readahead with
       | None -> json_params
       | Some readahead ->
-         ("file.readahead", JSON.Int readahead) :: json_params in
+         ("file.readahead", JSON.Int (Int64.of_int readahead)) :: json_params in
 
     let json_params =
       if sslverify then json_params
-- 
2.18.0




More information about the Libguestfs mailing list