summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2024-03-27 13:20:47 -0500
committerJohn Estabrook <jestabro@vyos.io>2024-03-29 01:03:32 -0500
commitbe75413eaf4aef85274cc16c2283486799d99d73 (patch)
treef90528ad2df47376dbf1d72bb182e8a683788eb6
parent406158f8557fd6ea88168357be1353fb58012b27 (diff)
downloadvyos1x-config-be75413eaf4aef85274cc16c2283486799d99d73.tar.gz
vyos1x-config-be75413eaf4aef85274cc16c2283486799d99d73.zip
T6180: add ability to apply mask to config tree
-rw-r--r--src/config_diff.ml29
-rw-r--r--src/config_diff.mli1
2 files changed, 30 insertions, 0 deletions
diff --git a/src/config_diff.ml b/src/config_diff.ml
index 35a5b55..3529a5d 100644
--- a/src/config_diff.ml
+++ b/src/config_diff.ml
@@ -390,6 +390,35 @@ let show_diff ?(cmds=false) path left right =
in
strs
+let is_terminal_path node path =
+ try
+ let n = Vytree.get node path in
+ match (Vytree.children_of_node n) with
+ | [] -> true
+ | _ -> false
+ with Vytree.Nonexistent_path -> false
+
+(* mask function; mask applied on right *)
+let mask_func ?recurse:_ (path : string list) (Diff_tree res) (m : change) =
+ match m with
+ | Added -> Diff_tree (res)
+ | Subtracted ->
+ (match path with
+ | [_] -> Diff_tree {res with left = Vytree.delete res.left path}
+ | _ -> if not (is_terminal_path res.right (list_but_last path)) then
+ Diff_tree {res with left = Vytree.delete res.left path}
+ else Diff_tree (res))
+ | Unchanged -> Diff_tree (res)
+ | Updated _ -> Diff_tree (res)
+
+(* call recursive diff with mask_func; mask applied on right *)
+let mask_tree left right =
+ let trees = make_diff_trees left right in
+ let d = diff [] mask_func trees (Option.some left, Option.some right)
+ in
+ let res = eval_result d in
+ res.left
+
let union_of_values (n : Config_tree.t) (m : Config_tree.t) =
let set_n = ValueS.of_list (data_of n).values in
let set_m = ValueS.of_list (data_of m).values in
diff --git a/src/config_diff.mli b/src/config_diff.mli
index c995ba3..6adf5a7 100644
--- a/src/config_diff.mli
+++ b/src/config_diff.mli
@@ -37,3 +37,4 @@ exception Nonexistent_child
val diff_tree : string list -> Config_tree.t -> Config_tree.t -> Config_tree.t
val show_diff : ?cmds:bool -> string list -> Config_tree.t -> Config_tree.t -> string
val tree_union : Config_tree.t -> Config_tree.t -> Config_tree.t
+val mask_tree : Config_tree.t -> Config_tree.t -> Config_tree.t