-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastcast_generate.go
164 lines (148 loc) · 3.13 KB
/
astcast_generate.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//go:build ignore
// +build ignore
package main
import (
"bytes"
"go/format"
"io"
"log"
"os"
"text/template"
)
func main() {
typeList := []string{
// Expressions:
"ArrayType",
"BadExpr",
"BasicLit",
"BinaryExpr",
"CallExpr",
"ChanType",
"CompositeLit",
"Ellipsis",
"FuncLit",
"FuncType",
"Ident",
"IndexExpr",
"InterfaceType",
"KeyValueExpr",
"MapType",
"ParenExpr",
"SelectorExpr",
"SliceExpr",
"StarExpr",
"StructType",
"TypeAssertExpr",
"UnaryExpr",
// Statements:
"AssignStmt",
"BadStmt",
"BlockStmt",
"BranchStmt",
"CaseClause",
"CommClause",
"DeclStmt",
"DeferStmt",
"EmptyStmt",
"ExprStmt",
"ForStmt",
"GoStmt",
"IfStmt",
"IncDecStmt",
"LabeledStmt",
"RangeStmt",
"ReturnStmt",
"SelectStmt",
"SendStmt",
"SwitchStmt",
"TypeSwitchStmt",
// Others:
"Comment",
"CommentGroup",
"FieldList",
"File",
"Package",
}
astcastFile, err := os.Create("astcast.go")
if err != nil {
log.Fatal(err)
}
writeCode(astcastFile, typeList)
astcastTestFile, err := os.Create("astcast_test.go")
if err != nil {
log.Fatal(err)
}
writeTests(astcastTestFile, typeList)
}
func generateCode(tmplText string, typeList []string) []byte {
tmpl := template.Must(template.New("code").Parse(tmplText))
var code bytes.Buffer
tmpl.Execute(&code, typeList)
prettyCode, err := format.Source(code.Bytes())
if err != nil {
panic(err)
}
return prettyCode
}
func writeCode(output io.Writer, typeList []string) {
code := generateCode(`// Code generated by astcast_generate.go; DO NOT EDIT
// Package astcast wraps type assertion operations in such way that you don't have
// to worry about nil pointer results anymore.
package astcast
import (
"go/ast"
)
// A set of sentinel nil-like values that are returned
// by all "casting" functions in case of failed type assertion.
var (
{{ range . }}
Nil{{.}} = &ast.{{.}}{}
{{- end }}
)
{{ range . }}
// To{{.}} returns x as a non-nil *ast.{{.}}.
// If ast.Node actually has such dynamic type, the result is
// identical to normal type assertion. In case if it has
// different type, the returned value is Nil{{.}}.
func To{{.}}(x ast.Node) *ast.{{.}} {
if x, ok := x.(*ast.{{.}}); ok {
return x
}
return Nil{{.}}
}
{{ end }}
`, typeList)
output.Write(code)
}
func writeTests(output io.Writer, typeList []string) {
code := generateCode(`// Code generated by astcast_generate.go; DO NOT EDIT
package astcast
import (
"go/ast"
"testing"
)
{{ range . }}
func TestTo{{.}}(t *testing.T) {
// Test successfull cast.
if x := To{{.}}(&ast.{{.}}{}); x == Nil{{.}} || x == nil {
t.Error("expected successfull cast, got nil")
}
// Test nil cast.
if x := To{{.}}(nil); x != Nil{{.}} {
t.Error("nil node didn't resulted in a sentinel value return")
}
// Test unsuccessfull cast.
{{- if (eq . "Ident") }}
if x := To{{.}}(&ast.CallExpr{}); x != Nil{{.}} || x == nil {
t.Errorf("expected unsuccessfull cast to return nil sentinel")
}
{{- else }}
if x := To{{.}}(&ast.Ident{}); x != Nil{{.}} || x == nil {
t.Errorf("expected unsuccessfull cast to return nil sentinel")
}
{{- end }}
}
{{ end }}
`, typeList)
output.Write(code)
}