diff options
author | John Estabrook <jestabro@vyos.io> | 2023-03-28 16:09:57 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-28 16:09:57 -0500 |
commit | 8955c0854f42ea2d8c9b71649b03d97f27c3f1d1 (patch) | |
tree | b4706794e349b78e353d34bed3cdc86e438ede44 /src/vytree.ml | |
parent | 334819524a78c920b0184f6f6a99daabf57c520e (diff) | |
parent | ef0fdf987d6a32311f664bd9bb925ae03675fad4 (diff) | |
download | vyos1x-config-8955c0854f42ea2d8c9b71649b03d97f27c3f1d1.tar.gz vyos1x-config-8955c0854f42ea2d8c9b71649b03d97f27c3f1d1.zip |
Merge pull request #15 from jestabro/identity
T5089: support for unit test of config_diff
Diffstat (limited to 'src/vytree.ml')
-rw-r--r-- | src/vytree.ml | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/vytree.ml b/src/vytree.ml index cda12f0..bd73776 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -4,7 +4,7 @@ type 'a t = { children: 'a t list } [@@deriving yojson] -type position = Before of string | After of string | End | Default +type position = Before of string | After of string | Lexical | End | Default exception Empty_path exception Duplicate_child @@ -27,6 +27,8 @@ let insert_immediate ?(position=Default) node name data children = | End -> node.children @ [new_node] | Before s -> Vylist.insert_before (fun x -> x.name = s) new_node node.children | After s -> Vylist.insert_after (fun x -> x.name = s) new_node node.children + | Lexical -> + Vylist.insert_compare (fun x y -> Util.lexical_numeric_compare x.name y.name) new_node node.children in { node with children = children' } let delete_immediate node name = @@ -86,6 +88,14 @@ let rec insert ?(position=Default) ?(children=[]) node path data = let s = Printf.sprintf "Non-existent intermediary node: \'%s\'" name in raise (Insert_error s) +let sorted_children_of_node cmp node = + let names = list_children node in + let names = List.sort cmp names in + List.map (find_or_fail node) names + +let sort_children cmp node = + {node with children = (sorted_children_of_node cmp node)} + (** Given a node N check if it has children with duplicate names, and merge subsequent children's children into the first child by that name. @@ -97,9 +107,11 @@ let rec insert ?(position=Default) ?(children=[]) node path data = may be normal and even expected, such as "ethernet eth0" and "ethernet eth1" in the "curly" format. *) -let merge_children merge_data node = +let merge_children merge_data cmp node = (* Given a node N and a list of nodes NS, find all nodes in NS that - have the same name as N and merge their children into N *) + have the same name as N and merge their children into N, sorting + children by a comparison function cmp (string -> string -> int) on + node names *) let rec merge_into n ns = match ns with | [] -> n @@ -108,6 +120,7 @@ let merge_children merge_data node = let children = List.append n.children n'.children in let data = merge_data n.data n'.data in let n = {n with children=children; data=data} in + let n = sort_children cmp n in merge_into n ns' else merge_into n ns' in @@ -177,11 +190,6 @@ let children_of_path node path = let node' = get node path in list_children node' -let sorted_children_of_node cmp node = - let names = list_children node in - let names = List.sort cmp names in - List.map (find_or_fail node) names - let copy node old_path new_path = if exists node new_path then raise Duplicate_child else let child = get node old_path in |