diff options
author | John Estabrook <jestabro@vyos.io> | 2023-03-18 19:21:34 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2023-03-23 13:42:52 -0500 |
commit | 0d45764873c0a2ec345b14c3bf750ff1fccb74a9 (patch) | |
tree | 9871fc146fde743797b3daa6bbc387f43fc967df /src/vytree.ml | |
parent | cd80232b1e9af085e2d3c3102bb096451916bfce (diff) | |
download | vyos1x-config-0d45764873c0a2ec345b14c3bf750ff1fccb74a9.tar.gz vyos1x-config-0d45764873c0a2ec345b14c3bf750ff1fccb74a9.zip |
T5089: order nodes on configtree parsing from string
This is a conservative application of lexical_numeric ordering for
configtree: order nodes on instantiation in 'from_string'
Diffstat (limited to 'src/vytree.ml')
-rw-r--r-- | src/vytree.ml | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/vytree.ml b/src/vytree.ml index 905f12b..bd73776 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -88,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. @@ -99,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 @@ -110,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 @@ -179,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 |