summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2015-09-23 01:40:18 +0600
committerDaniil Baturin <daniil@baturin.org>2015-09-23 01:40:18 +0600
commitc007e11142c605aaa8e8f3a0ff56b0fe657f8407 (patch)
tree7e78334ab52e1f9198cd1eafaa0fbdd44f474565 /src
parent0ac5704dd7a374ee4736a5cecb063c2ab49a63ce (diff)
downloadvyconf-c007e11142c605aaa8e8f3a0ff56b0fe657f8407.tar.gz
vyconf-c007e11142c605aaa8e8f3a0ff56b0fe657f8407.zip
Add delete function to Config_tree, add some tests for it.
Diffstat (limited to 'src')
-rw-r--r--src/config_tree.ml19
-rw-r--r--src/config_tree.mli2
2 files changed, 21 insertions, 0 deletions
diff --git a/src/config_tree.ml b/src/config_tree.ml
index b70a8bb..33af881 100644
--- a/src/config_tree.ml
+++ b/src/config_tree.ml
@@ -2,6 +2,7 @@ type value_behaviour = AddValue | ReplaceValue
exception Duplicate_value
exception Node_has_no_value
+exception No_such_value
type config_node_data = {
values: string list;
@@ -31,6 +32,11 @@ let add_value node path value =
let values = values @ [value] in
Vytree.update node path ({data with values=values})
+let delete_value node path value =
+ let data = Vytree.data_of_node @@ Vytree.get node path in
+ let values = Vylist.remove (fun x -> x = value) data.values in
+ Vytree.update node path {data with values=values}
+
let set_value node path value behaviour =
match behaviour with
| AddValue -> add_value node path value
@@ -52,3 +58,16 @@ let get_value node path =
match values with
| [] -> raise Node_has_no_value
| x :: _ -> x
+
+let delete node path value =
+ match value with
+ | Some v ->
+ (let values = get_values node path in
+ if Vylist.in_list values v then
+ (match values with
+ | [_] -> Vytree.delete node path
+ | _ -> delete_value node path v)
+ else raise No_such_value)
+ | None ->
+ Vytree.delete node path
+
diff --git a/src/config_tree.mli b/src/config_tree.mli
index 7ce505c..e4c91fc 100644
--- a/src/config_tree.mli
+++ b/src/config_tree.mli
@@ -16,6 +16,8 @@ val make : string -> t
val set : t -> string list -> string -> value_behaviour -> t
+val delete : t -> string list -> string option -> t
+
val get_values : t -> string list -> string list
val get_value : t -> string list -> string