-
Notifications
You must be signed in to change notification settings - Fork 25
/
schemainfo.go
60 lines (49 loc) · 1.54 KB
/
schemainfo.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
package schemainfo
import (
"go/ast"
"reflect"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/schema"
"github.com/bflad/tfproviderlint/passes/helper/schema/schemamapcompositelit"
)
var Analyzer = &analysis.Analyzer{
Name: "schemainfo",
Doc: "find github.com/hashicorp/terraform-plugin-sdk/helper/schema.Schema literals for later passes",
Requires: []*analysis.Analyzer{
inspect.Analyzer,
schemamapcompositelit.Analyzer,
},
Run: run,
ResultType: reflect.TypeOf([]*schema.SchemaInfo{}),
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
schemamapcompositelits := pass.ResultOf[schemamapcompositelit.Analyzer].([]*ast.CompositeLit)
nodeFilter := []ast.Node{
(*ast.CompositeLit)(nil),
}
var result []*schema.SchemaInfo
for _, smap := range schemamapcompositelits {
for _, mapSchema := range schema.GetSchemaMapSchemas(smap) {
result = append(result, schema.NewSchemaInfo(mapSchema, pass.TypesInfo))
}
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
x := n.(*ast.CompositeLit)
if !isSchemaSchema(pass, x) {
return
}
result = append(result, schema.NewSchemaInfo(x, pass.TypesInfo))
})
return result, nil
}
func isSchemaSchema(pass *analysis.Pass, cl *ast.CompositeLit) bool {
switch v := cl.Type.(type) {
default:
return false
case *ast.SelectorExpr:
return schema.IsTypeSchema(pass.TypesInfo.TypeOf(v))
}
}