forked from gojuno/genval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinspector.go
191 lines (175 loc) · 5.31 KB
/
inspector.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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"strings"
"github.com/gojuno/genval/types"
)
type inspector struct {
structs []StructDef
publicValidators map[string]bool
enums map[string][]string
}
func NewInspector() *inspector {
return &inspector{
publicValidators: make(map[string]bool),
enums: make(map[string][]string),
}
}
func (insp *inspector) Inspect(dir, outputFile string) error {
files, err := getFilesForInspect(dir, outputFile)
if err != nil {
return err
}
for _, f := range files {
fs := token.NewFileSet()
parsedFile, err := parser.ParseFile(fs, f, nil, 0)
if err != nil {
log.Fatalf("Error parsing file: %s: %s", f, err)
}
ast.Walk(insp, parsedFile)
}
return nil
}
func (insp *inspector) Result() []StructDef {
res := insp.structs
for i, s := range res {
if v, ok := insp.publicValidators[s.Name]; ok {
res[i].PublicValidatorExist = v
}
if v, ok := insp.enums[s.Name]; ok {
res[i].EnumValues = v
}
}
return res
}
func getFilesForInspect(dir, outputFile string) ([]string, error) {
if strings.HasPrefix(dir, "/") {
dir = "." + dir
}
var result []string
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("failed to read dir %s: %s", dir, err)
}
for _, f := range files {
if f.IsDir() || f.Name() == outputFile || strings.HasSuffix(f.Name(), "_test.go") || !strings.HasSuffix(f.Name(), ".go") {
continue
}
result = append(result, dir+"/"+f.Name())
}
return result, nil
}
func (insp *inspector) Visit(node ast.Node) ast.Visitor {
switch spec := node.(type) {
case *ast.TypeSpec:
insp.visitStruct(spec)
return nil
case *ast.FuncDecl: //To check if Validate() method already exist
methodName := spec.Name.Name
if methodName == "Validate" && spec.Recv != nil {
st := spec.Recv.List[0].Type
if x, ok := st.(*ast.Ident); ok {
insp.publicValidators[x.Name] = true
} else {
log.Fatalf("method Validate should be: 'func (s Struct)Validate() error{...}' not on pointer, %+v,%T", st, st)
}
}
return nil
case *ast.ValueSpec: //To find all consts and then generate validation with consts
if spec.Names[0].Obj.Kind == ast.Con {
valueName := spec.Names[0].Name
if x, ok := spec.Type.(*ast.Ident); ok {
valueType := x.Name
if !isSimple(valueType) {
insp.enums[valueType] = append(insp.enums[valueType], valueName)
}
}
return nil
}
}
return insp
}
func (insp *inspector) addStruct(s StructDef) {
insp.structs = append(insp.structs, s)
}
func (insp *inspector) visitStruct(astTypeSpec *ast.TypeSpec) {
structName := astTypeSpec.Name.Name
switch v := astTypeSpec.Type.(type) {
case *ast.StructType:
astFields := v.Fields.List
s := NewFieldsStruct(structName)
for _, astField := range astFields {
fieldType := parseFieldType(astField.Type, fmt.Sprintf("struct %s", structName))
fieldName := parseFieldName(astField.Names, fieldType)
tags := types.ParseTags(astField.Tag, fmt.Sprintf("struct %s, field %s", structName, fieldName))
field, err := NewField(fieldName, fieldType, tags)
if err != nil {
log.Fatalf("field creation failed for struct %s, %s", structName, err)
}
s.AddField(*field)
}
insp.addStruct(s)
case *ast.Ident, *ast.SelectorExpr, *ast.MapType, *ast.ArrayType, *ast.FuncType, *ast.ChanType: //aliases
aliasType := parseFieldType(v, fmt.Sprintf("struct %s", structName))
insp.addStruct(NewAliasStruct(structName, aliasType))
case *ast.StarExpr: //aliases
log.Fatalf("can not use alias on pointer with genval (because can not use pointer type as a receiver): %s, %+v: %T", structName, astTypeSpec, v)
case *ast.InterfaceType: //aliases
log.Fatalf("can not use alias on interface with genval (because can not use interface type as a receiver): %s, %+v: %T", structName, astTypeSpec, v)
default:
log.Fatalf("not expected Type for typeSpec: %s, %+v: %T", structName, astTypeSpec, astTypeSpec.Type)
}
}
func parseFieldType(t ast.Expr, logCtx string) types.TypeDef {
switch v := t.(type) {
case *ast.Ident:
simple := getSimpleType(v.Name)
if simple != nil {
return simple
}
return types.NewStruct(v.Name)
case *ast.SelectorExpr:
return types.NewExternalStruct(v.Sel.Name)
case *ast.ArrayType:
return types.NewArray(parseFieldType(v.Elt, logCtx))
case *ast.StarExpr:
return types.NewPointer(parseFieldType(v.X, logCtx))
case *ast.InterfaceType:
return types.NewInterface()
case *ast.MapType:
return types.NewMap(parseFieldType(v.Key, logCtx), parseFieldType(v.Value, logCtx))
case *ast.FuncType:
return types.NewFunc()
case *ast.ChanType:
return types.NewChan()
}
log.Fatalf("undefined typeField for %s: %+v, %T", logCtx, t, t)
return nil
}
func parseFieldName(fieldNames []*ast.Ident, fieldType types.TypeDef) string {
if len(fieldNames) != 0 {
return fieldNames[0].Name
}
return fieldType.Type() //wrapped struct, fieldName the same as type
}
func isSimple(fieldType string) bool {
return getSimpleType(fieldType) != nil
}
func getSimpleType(fieldType string) types.TypeDef {
switch fieldType {
case "string":
return types.NewString()
case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
return types.NewNumber(fieldType)
case "float32", "float64":
return types.NewNumber(fieldType)
case "bool":
return types.NewBool()
}
return nil
}