[Libguestfs] [PATCH v2 4/4] common/mltools: xpath_helpers: Get rid of xpath_*_default functions.

Richard W.M. Jones
Sun, 08 Oct 2017 21:26:56 -0000

Instead of using ‘xpath_(string|int|int64)_default’ we can write the
equivalent code using ‘Option.default’.  This is not quite so concise,
but may be easier to understand.

eg:
  xpath_int_default xctx "xpath_expr" 10
->
  Option.default 10 (xpath_int xctx "xpath_expr")
---
 common/mltools/xpath_helpers.ml  | 12 ------------
 common/mltools/xpath_helpers.mli |  6 ------
 v2v/copy_to_local.ml             |  6 +++---
 v2v/parse_libvirt_xml.ml         |  9 ++++-----
 v2v/parse_ovf_from_ova.ml        | 16 ++++++++--------
 5 files changed, 15 insertions(+), 34 deletions(-)

diff --git a/common/mltools/xpath_helpers.ml b/common/mltools/xpath_helpers.ml
index 05fad89a4..3afee8b21 100644
--- a/common/mltools/xpath_helpers.ml
+++ b/common/mltools/xpath_helpers.ml
@@ -40,15 +40,3 @@ let xpath_eval parsefn xpathctx expr  let xpath_string =
xpath_eval identity
 let xpath_int = xpath_eval int_of_string
 let xpath_int64 = xpath_eval Int64.of_string
-
-(* Parse an xpath expression and return a string/int; if the expression
- * doesn't match, return the default.
- *)
-let xpath_eval_default parsefn xpath expr default -  match xpath_eval parsefn
xpath expr with
-  | None -> default
-  | Some s -> s
-
-let xpath_string_default = xpath_eval_default identity
-let xpath_int_default = xpath_eval_default int_of_string
-let xpath_int64_default = xpath_eval_default Int64.of_string
diff --git a/common/mltools/xpath_helpers.mli b/common/mltools/xpath_helpers.mli
index 7434ba645..3a8190b05 100644
--- a/common/mltools/xpath_helpers.mli
+++ b/common/mltools/xpath_helpers.mli
@@ -25,9 +25,3 @@ val xpath_int : Xml.xpathctx -> string -> int option
 val xpath_int64 : Xml.xpathctx -> string -> int64 option
 (** Parse an xpath expression and return a string/int.  Returns
     [Some v], or [None] if the expression doesn't match. *)
-
-val xpath_string_default : Xml.xpathctx -> string -> string -> string
-val xpath_int_default : Xml.xpathctx -> string -> int -> int
-val xpath_int64_default : Xml.xpathctx -> string -> int64 -> int64
-(** Parse an xpath expression and return a string/int; if the expression
-    doesn't match, return the default. *)
diff --git a/v2v/copy_to_local.ml b/v2v/copy_to_local.ml
index d2471a546..8a64f3a58 100644
--- a/v2v/copy_to_local.ml
+++ b/v2v/copy_to_local.ml
@@ -243,14 +243,14 @@ and parse_libvirt_xml guest_name xml    let xpathctx =
Xml.xpath_new_context doc in
   Xml.xpath_register_ns xpathctx
                         "vmware"
"http://libvirt.org/schemas/domain/vmware/1.0";
-  let xpath_string = xpath_string xpathctx
-  and xpath_string_default = xpath_string_default xpathctx in
+  let xpath_string = xpath_string xpathctx in

   (* Get the dcpath, only present for libvirt >= 1.2.20 so use a
    * sensible default for older versions.
    *)
   let dcpath -    xpath_string_default
"/domain/vmware:datacenterpath" "ha-datacenter" in
+    Option.default "ha-datacenter"
+                   (xpath_string "/domain/vmware:datacenterpath") in

   (* Parse the disks. *)
   let get_disks, add_disk diff --git a/v2v/parse_libvirt_xml.ml
b/v2v/parse_libvirt_xml.ml
index 2f90bee0c..421175373 100644
--- a/v2v/parse_libvirt_xml.ml
+++ b/v2v/parse_libvirt_xml.ml
@@ -77,10 +77,8 @@ let parse_libvirt_xml ?conn xml    let doc = Xml.parse_memory
xml in
   let xpathctx = Xml.xpath_new_context doc in
   let xpath_string = xpath_string xpathctx
-  and xpath_string_default = xpath_string_default xpathctx
   and xpath_int = xpath_int xpathctx
-  (*and xpath_int_default = xpath_int_default xpathctx*)
-  and xpath_int64_default = xpath_int64_default xpathctx in
+  and xpath_int64 = xpath_int64 xpathctx in

   let hypervisor      match xpath_string "/domain/@type" with
@@ -92,7 +90,8 @@ let parse_libvirt_xml ?conn xml      | None | Some
"" ->
        error (f_"in the libvirt XML metadata, <name> is missing or
empty")
     | Some s -> s in
-  let memory = xpath_int64_default "/domain/memory/text()" (1024L *^
1024L) in
+  let memory +    Option.default (1024L *^ 1024L) (xpath_int64
"/domain/memory/text()") in
   let memory = memory *^ 1024L in

   let cpu_vendor = xpath_string "/domain/cpu/vendor/text()" in
@@ -317,7 +316,7 @@ let parse_libvirt_xml ?conn xml             (* This is for
testing curl, eg for testing VMware conversions
             * without needing VMware around.
             *)
-           let path = xpath_string_default "source/@name"
"" in
+           let path = Option.default "" (xpath_string
"source/@name") in
            let qemu_uri = create_curl_qemu_uri driver host port path in
            add_disk qemu_uri format controller P_dont_rewrite
         | Some protocol, _, _ ->
diff --git a/v2v/parse_ovf_from_ova.ml b/v2v/parse_ovf_from_ova.ml
index 1c113eca2..1d7c632bc 100644
--- a/v2v/parse_ovf_from_ova.ml
+++ b/v2v/parse_ovf_from_ova.ml
@@ -52,9 +52,7 @@ let parse_ovf_from_ova ovf_filename
   let xpath_string = xpath_string xpathctx
   and xpath_int = xpath_int xpathctx
-  and xpath_string_default = xpath_string_default xpathctx
-  and xpath_int_default = xpath_int_default xpathctx
-  and xpath_int64_default = xpath_int64_default xpathctx in
+  and xpath_int64 = xpath_int64 xpathctx in

   let rec parse_top ()      (* Search for vm name. *)
@@ -64,11 +62,11 @@ let parse_ovf_from_ova ovf_filename        | Some _ as name
-> name in

     (* Search for memory. *)
-    let memory = xpath_int64_default
"/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/ovf:Item[rasd:ResourceType/text()=4]/rasd:VirtualQuantity/text()"
(1024L *^ 1024L) in
+    let memory = Option.default (1024L *^ 1024L) (xpath_int64
"/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/ovf:Item[rasd:ResourceType/text()=4]/rasd:VirtualQuantity/text()")
in
     let memory = memory *^ 1024L *^ 1024L in

     (* Search for number of vCPUs. *)
-    let vcpu = xpath_int_default
"/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/ovf:Item[rasd:ResourceType/text()=3]/rasd:VirtualQuantity/text()"
1 in
+    let vcpu = Option.default 1 (xpath_int
"/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/ovf:Item[rasd:ResourceType/text()=3]/rasd:VirtualQuantity/text()")
in

     (* CPU topology.  coresPerSocket is a VMware proprietary extension.
      * I couldn't find out how hyperthreads is specified in the OVF.
@@ -91,7 +89,7 @@ let parse_ovf_from_ova ovf_filename             Some sockets,
Some cores_per_socket in

     (* BIOS or EFI firmware? *)
-    let firmware = xpath_string_default
"/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/vmw:Config[@vmw:key=\"firmware\"]/@vmw:value"
"bios" in
+    let firmware = Option.default "bios" (xpath_string
"/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/vmw:Config[@vmw:key=\"firmware\"]/@vmw:value")
in
     let firmware        match firmware with
       | "bios" -> BIOS
@@ -141,7 +139,8 @@ let parse_ovf_from_ova ovf_filename          | Some id ->
parent_controller id in

       Xml.xpathctx_set_current_context xpathctx n;
-      let file_id = xpath_string_default "rasd:HostResource/text()"
"" in
+      let file_id +        Option.default "" (xpath_string
"rasd:HostResource/text()") in
       let rex = PCRE.compile "^(?:ovf:)?/disk/(.*)" in
       if PCRE.matches rex file_id then (
         (* Chase the references through to the actual file name. *)
@@ -231,7 +230,8 @@ let parse_ovf_from_ova ovf_filename        let n =
Xml.xpathobj_node obj i in
       Xml.xpathctx_set_current_context xpathctx n;
       let vnet -        xpath_string_default
"rasd:ElementName/text()" (sprintf"eth%d" i) in
+        Option.default (sprintf"eth%d" i)
+                       (xpath_string "rasd:ElementName/text()") in
       let nic = {
         s_mac = None;
         s_nic_model = None;
--
2.13.2