yaml-merge

Sat, 17 Dec 2011 17:08:33 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 17 Dec 2011 17:08:33 +0000
changeset 0
0757d85e604b
child 2
0653a782618c
permissions
-rwxr-xr-x

Initial commit

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(t1) do
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 array[v] = true;
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 t1[i] = nil;
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 for i,v in ipairs(t2) do
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 array[v] = true;
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 for k,v in pairs(t2) do -- non-numeric
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 if not(type(k) == "number" and array[v]) then -- fixme, merge non-numeric
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 t1[k] = merge_in(t1[k], v, depth + 1);
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
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 for v in pairs(array) do
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 table.insert(t1, v);
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 elseif t2 ~= nil then
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 t1 = t2;
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 return t1;
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local out = {};
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 for i, fn in ipairs(arg) do
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local f = assert(io.open(fn, "r"));
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 merge_in(out, yaml.load(f:read("*a")));
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 f:close();
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 print(yaml.dump(out));

mercurial