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

#!/usr/bin/env lua5.1

local yaml = require "yaml";

function merge_in(t1, t2, depth)
	depth = depth or 0;
	if type(t1) == "table" and type(t2) == "table" then
		local array = {};
		if depth < 2 then -- numeric
			for i,v in ipairs(t1) do
				array[v] = true;
				t1[i] = nil;
			end
			for i,v in ipairs(t2) do
				array[v] = true;
			end
		end
		for k,v in pairs(t2) do -- non-numeric
			if not(type(k) == "number" and array[v]) then -- fixme, merge non-numeric
				t1[k] = merge_in(t1[k], v, depth + 1);
			end
		end
		for v in pairs(array) do
			table.insert(t1, v);
		end
	elseif t2 ~= nil then
		t1 = t2;
	end
	return t1;
end

local out = {};
for i, fn in ipairs(arg) do
	local f = assert(io.open(fn, "r"));
	merge_in(out, yaml.load(f:read("*a")));
	f:close();
end

print(yaml.dump(out));

mercurial