yaml-merge

Mon, 16 Jan 2012 21:42:31 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 16 Jan 2012 21:42:31 +0000
changeset 2
0653a782618c
parent 0
0757d85e604b
permissions
-rwxr-xr-x

yaml-merge: Preserve ordering when merging arrays

0
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env lua5.1
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local yaml = require "yaml";
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 function merge_in(t1, t2, depth)
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 depth = depth or 0;
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 if type(t1) == "table" and type(t2) == "table" then
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local array = {};
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if depth < 2 then -- numeric
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 for i,v in ipairs(t2) do
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 array[v] = true;
2
0653a782618c yaml-merge: Preserve ordering when merging arrays
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
12 table.insert(t1, v);
0
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 for k,v in pairs(t2) do -- non-numeric
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if not(type(k) == "number" and array[v]) then -- fixme, merge non-numeric
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 t1[k] = merge_in(t1[k], v, depth + 1);
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 elseif t2 ~= nil then
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 t1 = t2;
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return t1;
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local out = {};
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 for i, fn in ipairs(arg) do
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local f = assert(io.open(fn, "r"));
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 merge_in(out, yaml.load(f:read("*a")));
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 f:close();
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 print(yaml.dump(out));

mercurial