/
main.go
81 lines (68 loc) · 1.58 KB
/
main.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package templates
import (
"bytes"
"go/format"
"html/template"
"log"
)
type CodeTemplate struct {
Name string
Raw string
Parsed *template.Template
}
func (r *CodeTemplate) parse() {
r.Parsed = template.Must(template.New(r.Name).Parse(r.Raw))
}
type Analysis struct {
Command string
PackageName string
Types map[string]TypeSpec
}
type TypeSpec struct {
TypeName string
Values []TypeValue
ExcludeList map[string]bool
}
type TypeValue struct {
Name string
Str string
}
func (analysis *Analysis) GenerateByTemplate(merge bool) map[string][]byte {
var results = make(map[string][]byte)
var buf bytes.Buffer
FileBase.Parsed.Execute(&buf, analysis)
for typeName, spec := range analysis.Types {
for _, t := range EnumBase {
_, excludeList := spec.ExcludeList[t.Name]
//_, haveSpare := Spare[t.Name]
if excludeList {
//if !haveSpare {
continue
//}
}
if err := t.Parsed.Execute(&buf, &spec); err != nil {
log.Fatalf("generating code: %v", err)
}
}
if !merge {
results[typeName] = buf.Bytes()
buf = bytes.Buffer{}
FileBase.Parsed.Execute(&buf, analysis)
}
}
if merge {
results["all"] = buf.Bytes()
}
var err error
for key, val := range results {
results[key], err = format.Source(val)
if err != nil {
// Should never happen, but can arise when developing this code.
// The user can compile the output to see the error.
log.Printf("[WARN] internal error: invalid Go generated: %s", err)
log.Printf("[WARN] compile the package to analyze the error")
results[key] = val
}
}
return results
}