From be75413eaf4aef85274cc16c2283486799d99d73 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 27 Mar 2024 13:20:47 -0500 Subject: T6180: add ability to apply mask to config tree --- src/config_diff.ml | 29 +++++++++++++++++++++++++++++ src/config_diff.mli | 1 + 2 files changed, 30 insertions(+) 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 -- cgit v1.2.3