-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsl.go
202 lines (184 loc) · 4.74 KB
/
dsl.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"reflect"
"strings"
)
type (
// DSL - dsl-struct description
DSL struct {
Imports
Items []*DSLItem
PkgName string
}
// DSLItem corresponds to the field of dsl-struct
DSLItem struct {
InstName string
GenericTypes []PkgTypePair
TypeArgs map[string]string
}
// PkgTypePair tuple of type and its package
PkgTypePair struct {
PkgName string
Type string
}
)
const defaultStructName = "_typeinst"
// ParseDSL parses and rertrieves dsl-struct
func ParseDSL(filename, structName string) (dsl *DSL, err error) {
defer bpan.RecoverTo(&err)
if structName == "" {
structName = defaultStructName
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return nil, err
}
dsl = &DSL{
PkgName: f.Name.Name,
}
imports := Imports{}
for _, spec := range f.Imports {
if err := imports.AddSpec(spec); err != nil {
return nil, fmt.Errorf("bad imports: %v", err)
}
}
stringer := astStringer{}
parseFunc := func(it *DSLItem, t *ast.FuncType) {
typeVarsPkgs := NewStrSet()
walker := pkgNameWalker(typeVarsPkgs)
estr := func(s string) string {
return s + " [in dsl-struct field: " + it.InstName + "]"
}
if t.Params == nil || len(t.Params.List) == 0 {
bpan.Panicf(estr("dsl-func has no arguments i.e. typevar substitutions"))
}
if t.Results == nil || len(t.Results.List) == 0 {
bpan.Panicf(estr("dsl-func has no result i.e. generic type"))
}
for _, field := range t.Params.List {
typeVar := fieldName(field)
if typeVar == "" {
bpan.Panicf(estr("typevar param in func requires name"))
}
ast.Walk(walker, field.Type)
it.TypeArgs[typeVar] = stringer.ToString(field.Type)
}
for pkgname := range typeVarsPkgs {
bpan.Check(dsl.Imports.Add(pkgname, imports.requireNamed(pkgname)))
}
qtset := NewStrSet()
for _, field := range t.Results.List {
if len(field.Names) > 0 {
bpan.Panicf(estr("dsl-func result cannot have field names"))
}
pair := parseGenericTypeExpr(field.Type)
if pair.PkgName == "" {
bpan.Panicf(estr("generic type cannot be local, it must be imported from another package"))
}
qt := pair.qualifiedType()
if qtset.Contains(qt) {
bpan.Panicf(estr("merging repeated generic type: %v"), qt)
}
qtset.Add(qt)
pair.PkgName = imports.requireNamed(pair.PkgName)
it.GenericTypes = append(it.GenericTypes, pair)
}
}
parseStruct := func(ts *ast.TypeSpec) {
expr, ok := ts.Type.(*ast.StructType)
if !ok {
bpan.Panicf("struct type expected")
}
if expr.Fields == nil || len(expr.Fields.List) == 0 {
bpan.Panicf("empty struct")
}
for _, field := range expr.Fields.List {
it := &DSLItem{
InstName: fieldName(field),
TypeArgs: make(map[string]string),
}
ft, ok := field.Type.(*ast.FuncType)
if !ok {
bpan.Panicf("struct fields must have func types, e.g: `func(K int, V string) MyMap`, found: field: %s type: %v ",
it.InstName, reflect.TypeOf(ts.Type))
}
parseFunc(it, ft)
dsl.Items = append(dsl.Items, it)
}
}
for _, decl := range f.Decls {
switch decl := decl.(type) {
case *ast.GenDecl:
if decl.Tok == token.TYPE {
for _, spec := range decl.Specs {
ts := spec.(*ast.TypeSpec)
name := ts.Name.Name
if strings.HasPrefix(name, structName) {
parseStruct(ts)
return
}
}
}
}
}
return nil, fmt.Errorf("delaration of dsl struct not found: %s", structName)
}
func fieldName(field *ast.Field) string {
if len(field.Names) != 1 {
bpan.Panicf("field must have one name in struct fields and func params/returns: %v", field.Names)
}
return field.Names[0].Name
}
type astStringer struct {
printer.Config
buf *bytes.Buffer
fset *token.FileSet
}
func (s *astStringer) ToString(node interface{}) string {
if s.buf == nil {
s.buf = bytes.NewBuffer(make([]byte, 64))
s.fset = token.NewFileSet()
s.Config.Mode = printer.RawFormat
}
s.buf.Reset()
_ = s.Fprint(s.buf, s.fset, node)
return s.buf.String()
}
type pkgNameWalker map[string]struct{}
func (w pkgNameWalker) Visit(n ast.Node) ast.Visitor {
switch n := n.(type) {
case *ast.SelectorExpr:
switch n := n.X.(type) {
case *ast.Ident:
StrSet(w).Add(n.Name)
}
}
return w
}
func parseGenericTypeExpr(t ast.Expr) PkgTypePair {
switch t := t.(type) {
case *ast.Ident:
return PkgTypePair{"", t.Name}
case *ast.SelectorExpr:
typ := t.Sel.Name
switch t := t.X.(type) {
case *ast.Ident:
return PkgTypePair{t.Name, typ}
}
}
bpan.Panicf("unexpected type expr for generic type: %v in expr: %v", reflect.TypeOf(t), t)
return PkgTypePair{}
}
func (p PkgTypePair) qualifiedType() string {
if p.PkgName == "" {
return p.Type
}
return p.PkgName + "." + p.Type
}