-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerate_value.py
executable file
·42 lines (37 loc) · 1.36 KB
/
generate_value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
from gotypes import types, imports, imports_stringer
res = "// Code generated by generate_value.py; DO NOT EDIT.\n"
res += "\n"
res += "package cli\n"
res += "\n"
res += "import (\n"
for pkg in sorted(imports + imports_stringer):
res += "\t\"%s\"\n" % pkg
res += ")\n"
for (typ, name, stringer, emptier) in types:
safe_typ = typ.replace(".", "")
res += "\n"
res += "// %s\n" % typ
res += "\n"
res += "var (\n"
res += "\t_ Value = (*%sValue)(nil)\n" % safe_typ
res += "\t_ Getter = (*%sValue)(nil)\n" % safe_typ
res += "\t_ Emptier = (*%sValue)(nil)\n" % safe_typ
res += "\t_ Typer = (*%sValue)(nil)\n" % safe_typ
res += ")\n"
res += "\n"
res += "type %sValue %s\n" % (safe_typ, typ)
res += "\n"
res += "func new%sValue(p *%s) *%sValue {\n" % (name, typ, safe_typ)
res += "\treturn (*%sValue)(p)\n" % safe_typ
res += "}\n"
res += "\n"
res += "func (v *%sValue) Get() interface{} { return %s(*v) }\n" % (safe_typ, typ)
res += "\n"
res += "func (v *%sValue) Empty() bool { return %s }\n" % (safe_typ, (emptier % "v"))
res += "\n"
res += "func (v *%sValue) String() string { return %s }\n" % (safe_typ, (stringer % "v"))
res += "\n"
res += "func (*%sValue) Type() string { return \"%s\" }\n" % (safe_typ, typ)
with open("./value_gen.go", "w") as f:
f.write(res)