-
Notifications
You must be signed in to change notification settings - Fork 25
/
crudfuncinfo.go
55 lines (44 loc) · 1.43 KB
/
crudfuncinfo.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
package crudfuncinfo
import (
"go/ast"
"reflect"
"github.com/bflad/tfproviderlint/helper/astutils"
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/schema"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
var Analyzer = &analysis.Analyzer{
Name: "crudfuncinfo",
Doc: "find github.com/hashicorp/terraform-plugin-sdk/helper/schema CreateFunc, ReadFunc, UpdateFunc, and DeleteFunc declarations for later passes",
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
Run: run,
ResultType: reflect.TypeOf([]*schema.CRUDFuncInfo{}),
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.FuncLit)(nil),
}
var result []*schema.CRUDFuncInfo
inspect.Preorder(nodeFilter, func(n ast.Node) {
funcType := astutils.FuncTypeFromNode(n)
if funcType == nil {
return
}
if !astutils.IsFieldListTypePackageType(funcType.Params, 0, pass.TypesInfo, schema.PackagePath, schema.TypeNameResourceData) {
return
}
if !astutils.IsFieldListType(funcType.Params, 1, astutils.IsFunctionParameterTypeInterface) {
return
}
if !astutils.IsFieldListType(funcType.Results, 0, astutils.IsFunctionParameterTypeError) {
return
}
result = append(result, schema.NewCRUDFuncInfo(n, pass.TypesInfo))
})
return result, nil
}