Skip to content

Commit 5d6a8fa

Browse files
committed
CP-18289-v2 add functions for interpreting platform strings
This code is identical to code used by Xapi for the same purpose: decide, what string values in the platform flags are considered true or false. Signed-off-by: Christian Lindig <[email protected]>
1 parent 6317b6f commit 5d6a8fa

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

lib/platform.ml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(*
2+
* Copyright (C) Citrix Systems Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*)
14+
15+
16+
type platformdata = (string * string) list
17+
18+
let is_valid ~key ~platformdata =
19+
(not (List.mem_assoc key platformdata)) ||
20+
(match List.assoc key platformdata |> String.lowercase with
21+
| "true" | "1" | "false" | "0" -> true
22+
| v -> false
23+
)
24+
25+
let is_true ~key ~platformdata ~default =
26+
try
27+
match List.assoc key platformdata |> String.lowercase with
28+
| "true" | "1" -> true
29+
| "false" | "0" -> false
30+
| _ -> default (* Check for validity using is_valid if required *)
31+
with Not_found ->
32+
default
33+
34+

lib/platform.mli

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(*
2+
* Copyright (C) Citrix Systems Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*)
14+
15+
(* This module defines interpretation of boolean platform values. It
16+
* matches the interpretation implemented in XenAPI for these values.
17+
*)
18+
19+
type platformdata = (string * string) list
20+
21+
(** [is_valid key platformdata] returns true if:
22+
* 1. The key is _not_ in platformdata (absence of key is valid) or
23+
* 2. The key is in platformdata, associated with a booleanish value *)
24+
val is_valid: key:string -> platformdata:platformdata -> bool
25+
26+
(** [is_true key platformdata default] returns true, if the platformdata
27+
* contains a value for key that is "true" or "1". It returns false, if
28+
* a value "0" or "false" exists. If the key doesn't exist or contains
29+
* none of the values above, [default] is returned.
30+
*)
31+
val is_true:
32+
key:string ->
33+
platformdata:platformdata ->
34+
default:bool ->
35+
bool
36+

0 commit comments

Comments
 (0)