~emersion/go-drm

0d4eee9647b4b3c102e53183f7a813e60a52188e — Simon Ser 3 months ago 62e8a36
Remove aliased constants from generated code

This fails the build.
1 files changed, 19 insertions(+), 8 deletions(-)

M fourcc.py
M fourcc.py => fourcc.py +19 -8
@@ 62,7 62,7 @@ def to_camel_case(s):
			parts[i] = part.lower().capitalize()
	return "".join(parts)

consts = []
consts = {}
for (ident, val) in idents.items():
	if "_MOD_" in ident:
		t = "Modifier"


@@ 71,6 71,8 @@ for (ident, val) in idents.items():
			if ident.startswith("VENDOR_"):
				t = "ModifierVendor"
				ident = ident[len("VENDOR_"):]
			if ident.startswith("ARM_TYPE_"):
				continue
		else:
			parts = ident.split("_FORMAT_MOD_")
			assert(len(parts) == 2)


@@ 94,7 96,20 @@ for (ident, val) in idents.items():
	if t == "ModifierVendor" or ident.lower() in words:
		ident = to_camel_case(ident)

	consts.append({"name": name, "go_ident": t + ident, "value": val, "type": t})
	if t not in consts:
		consts[t] = []
	consts[t].append({"name": name, "go_ident": t + ident, "value": val, "type": t})

# Some constants are aliases. Detect and remove these.
for t, l in consts.items():
	seen = set()
	out = []
	for cst in l:
		if cst["value"] in seen:
			continue
		seen.add(cst["value"])
		out.append(cst)
	consts[t] = out

with open("fourcc_generated.go", "w+") as go_file:
	go_file.write('// Code generated by fourcc.py - DO NOT EDIT\n')


@@ 104,17 119,13 @@ with open("fourcc_generated.go", "w+") as go_file:
	for t in types:
		go_file.write('\n')
		go_file.write('const (\n')
		for cst in consts:
			if cst["type"] != t:
				continue
		for cst in consts[t]:
			go_file.write('\t' + cst["go_ident"] + ' ' + cst["type"] + ' = ' + cst["value"] + '\n')
		go_file.write(')\n')
		go_file.write('\n')
		go_file.write('func (v ' + t + ') String() string {\n')
		go_file.write('\tswitch v {\n')
		for cst in consts:
			if cst["type"] != t:
				continue
		for cst in consts[t]:
			go_file.write('\tcase ' + cst["go_ident"] + ':\n')
			go_file.write('\t\treturn "' + cst["name"] + '"\n')
		go_file.write('\tdefault:\n')