[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[augeas-devel] Difference between seq and label


Someone pointed out that it was harder to manipulate the tree with "seq" and counter lens than with labels [1] in the case of fstab file. Why does seq is used instead of a label lens? It's related to the alignment problem in the put direction (tree -> string transformation). When using label, the keys may be duplicated, and then associating strings that were removed in the tree is done in node order. So, if a node is inserted in the middle, all other nodes below will be associated with the abstracted string from the next node, and the last one will get the default string. The right behavior would be that the new node should get default values. For this to happen, keys in the tree must be unique. Then, even if the nodes are reordered or new nodes are added, then every strings will be replaced exactly where at their previous place and new nodes will get default string. This is important for example in files that are line based. A seq lens to identify uniquely every like is a good pattern to prevent a big diff for a file in which we would insert something at the beginning of the file.

An example of this behavior is provided in the attached align.aug lens.



[1] http://mrpointy.wordpress.com/2010/05/01/augeas-and-appending-lines-to-etcfstab/
module Align =

let dels (s:string) = del s s

let align_sequence = [ label "entry" . store /[a-zA-Z]+/ . dels " " . del /[0-9]+/ "0" . dels "\n" ]*
let align_key = counter "entry" . [ seq "entry" . store /[a-zA-Z]+/ . dels " " . del /[0-9]+/ "0" . dels "\n" ]*

let input = 
"Foo 1
Bar 2
(* keys are duplicated *)
test align_sequence get input =
  { "entry" = "Foo" }
  { "entry" = "Bar" }

(* keys are unique *)
test align_key get input =
  { "1" = "Foo" }
  { "2" = "Bar" }

(* Baz stoled the number from Bar, which is embarassing *)
test align_sequence put input after insa "entry" "/entry[1]"; set "/entry[2]" "Baz" =
"Foo 1
Baz 2
Bar 0

(* Baz gets the default value for the del lens and it makes life wonderful *)
test align_key put input after insa "3" "/1"; set "/3" "Baz" =
"Foo 1
Baz 0
Bar 2

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]