yaml-merge

changeset 2
0653a782618c
parent 0
0757d85e604b
equal deleted inserted replaced
1:149a63bc9906 2:0653a782618c
5 function merge_in(t1, t2, depth) 5 function merge_in(t1, t2, depth)
6 depth = depth or 0; 6 depth = depth or 0;
7 if type(t1) == "table" and type(t2) == "table" then 7 if type(t1) == "table" and type(t2) == "table" then
8 local array = {}; 8 local array = {};
9 if depth < 2 then -- numeric 9 if depth < 2 then -- numeric
10 for i,v in ipairs(t1) do
11 array[v] = true;
12 t1[i] = nil;
13 end
14 for i,v in ipairs(t2) do 10 for i,v in ipairs(t2) do
15 array[v] = true; 11 array[v] = true;
12 table.insert(t1, v);
16 end 13 end
17 end 14 end
18 for k,v in pairs(t2) do -- non-numeric 15 for k,v in pairs(t2) do -- non-numeric
19 if not(type(k) == "number" and array[v]) then -- fixme, merge non-numeric 16 if not(type(k) == "number" and array[v]) then -- fixme, merge non-numeric
20 t1[k] = merge_in(t1[k], v, depth + 1); 17 t1[k] = merge_in(t1[k], v, depth + 1);
21 end 18 end
22 end
23 for v in pairs(array) do
24 table.insert(t1, v);
25 end 19 end
26 elseif t2 ~= nil then 20 elseif t2 ~= nil then
27 t1 = t2; 21 t1 = t2;
28 end 22 end
29 return t1; 23 return t1;

mercurial