-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
schema.go
201 lines (176 loc) · 4.62 KB
/
schema.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
package colfer
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"path"
)
// FormatFile normalizes the structure.
// The content is expected to be syntactically correct.
func FormatFile(path string) (changed bool, err error) {
orig, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
clean, err := format.Source(orig)
if err != nil {
return false, fmt.Errorf("colfer: format %q: %s", path, err)
}
if bytes.Equal(orig, clean) {
return false, nil
}
if err := ioutil.WriteFile(path, clean, 0644); err != nil {
return false, err
}
return true, nil
}
// ParseFiles returns the schema definitions.
func ParseFiles(paths ...string) (Packages, error) {
var packages Packages
fileSet := token.NewFileSet()
for _, schemaPath := range paths {
fileAST, err := parser.ParseFile(fileSet, schemaPath, nil, parser.ParseComments|parser.AllErrors)
if err != nil {
return nil, err
}
var pkg *Package
for _, p := range packages {
if p.Name == fileAST.Name.Name {
pkg = p
break
}
}
if pkg == nil {
pkg = &Package{Name: fileAST.Name.Name}
packages = append(packages, pkg)
}
pkg.SchemaFiles = append(pkg.SchemaFiles, path.Base(schemaPath))
pkg.Docs = append(pkg.Docs, docs(fileAST.Doc)...)
// switch through the AST types
for _, decl := range fileAST.Decls {
switch decl := decl.(type) {
default:
return nil, fmt.Errorf("colfer: unsupported declaration type %T", decl)
case *ast.GenDecl:
for _, spec := range decl.Specs {
if err := addSpec(pkg, decl, spec, schemaPath); err != nil {
return nil, err
}
}
}
}
}
names := make(map[string]*Struct)
for _, pkg := range packages {
for _, t := range pkg.Structs {
qname := t.String()
if dupe, ok := names[qname]; ok {
return nil, fmt.Errorf("colfer: duplicate struct definition %q in file %s and %s", qname, dupe.SchemaFile, t.SchemaFile)
}
names[qname] = t
}
}
for _, pkg := range packages {
for _, t := range pkg.Structs {
for _, f := range t.Fields {
_, ok := datatypes[f.Type]
if ok {
if f.TypeList {
switch f.Type {
case "int32", "int64":
fmt.Println("colfer: WARNING: integer lists are Go only at the moment")
case "float32", "float64", "text", "binary":
default:
return nil, fmt.Errorf("colfer: unsupported lists type %q for field %s", t, f)
}
}
continue
}
if f.TypeRef, ok = names[f.Type]; ok {
continue
}
if f.TypeRef, ok = names[pkg.Name+"."+f.Type]; ok {
continue
}
return nil, fmt.Errorf("colfer: unknown datatype %q for field %s", f.Type, f)
}
}
}
return packages, nil
}
func addSpec(pkg *Package, decl *ast.GenDecl, spec ast.Spec, schemaPath string) error {
switch spec := spec.(type) {
default:
return fmt.Errorf("colfer: unsupported specification type %T", spec)
case *ast.TypeSpec:
switch specType := spec.Type.(type) {
default:
return fmt.Errorf("colfer: unsupported data type %T", specType)
case *ast.StructType:
t := &Struct{Pkg: pkg, Name: spec.Name.Name, SchemaFile: path.Base(schemaPath)}
for _, pt := range pkg.Structs {
if pt.Name == t.Name {
return fmt.Errorf("colfer: duplicate %s declaration", pt)
}
}
pkg.Structs = append(pkg.Structs, t)
t.Docs = append(docs(decl.Doc), docs(spec.Doc)...)
if err := mapStruct(t, specType); err != nil {
return err
}
}
}
return nil
}
func mapStruct(dst *Struct, src *ast.StructType) error {
if len(src.Fields.List) == 0 {
return fmt.Errorf("colfer: %s has no fields", dst)
}
for i, f := range src.Fields.List {
field := &Field{Struct: dst, Index: i}
dst.Fields = append(dst.Fields, field)
if len(f.Names) == 0 {
return fmt.Errorf("colfer: field %d from %s has no name", i, dst)
}
field.Name = f.Names[0].Name
if f.Tag != nil {
return fmt.Errorf("colfer: illegal tag %s on %s", f.Tag.Value, field)
}
field.Docs = docs(f.Doc)
expr := f.Type
for {
switch t := expr.(type) {
case *ast.ArrayType:
expr = t.Elt
field.TypeList = true
continue
case *ast.Ident:
field.Type = t.Name
case *ast.SelectorExpr:
switch pkgIdent := t.X.(type) {
case *ast.Ident:
field.Type = pkgIdent.Name + "." + t.Sel.Name
default:
return fmt.Errorf("colfer: unknown datatype selector expression %T for field %s", pkgIdent, field)
}
default:
return fmt.Errorf("colfer: unknown datatype declaration %T for field %s", t, field)
}
break
}
}
return nil
}
func docs(g *ast.CommentGroup) []string {
var a []string
if g != nil {
for _, c := range g.List {
a = append(a, c.Text)
}
}
return a
}