forked from bflad/tfproviderlint
/
schemavalidatefuncinfo.go
59 lines (47 loc) · 1.48 KB
/
schemavalidatefuncinfo.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
package schemavalidatefuncinfo
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: "schemavalidatefuncinfo",
Doc: "find github.com/hashicorp/terraform-plugin-sdk/helper/schema SchemaValidateFunc declarations for later passes",
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
Run: run,
ResultType: reflect.TypeOf([]*schema.SchemaValidateFuncInfo{}),
}
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.SchemaValidateFuncInfo
inspect.Preorder(nodeFilter, func(n ast.Node) {
funcType := astutils.FuncTypeFromNode(n)
if funcType == nil {
return
}
if !astutils.IsFieldListType(funcType.Params, 0, astutils.IsExprTypeInterface) {
return
}
if !astutils.IsFieldListType(funcType.Params, 1, astutils.IsExprTypeString) {
return
}
if !astutils.IsFieldListType(funcType.Results, 0, astutils.IsExprTypeArrayString) {
return
}
if !astutils.IsFieldListType(funcType.Results, 1, astutils.IsExprTypeArrayError) {
return
}
result = append(result, schema.NewSchemaValidateFuncInfo(n, pass.TypesInfo))
})
return result, nil
}