-
Notifications
You must be signed in to change notification settings - Fork 25
/
type_valuevalidationfunc.go
87 lines (72 loc) · 2.06 KB
/
type_valuevalidationfunc.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
package customdiff
import (
"go/ast"
"go/token"
"go/types"
"github.com/bflad/tfproviderlint/helper/astutils"
)
const (
TypeNameValueValidationFunc = `ValueValidationFunc`
)
// IsFuncTypeValueValidationFunc returns true if the FuncType matches expected parameters and results types
func IsFuncTypeValueValidationFunc(node ast.Node, info *types.Info) bool {
funcType := astutils.FuncTypeFromNode(node)
if funcType == nil {
return false
}
if !astutils.HasFieldListLength(funcType.Params, 2) {
return false
}
if !astutils.IsFieldListType(funcType.Params, 0, astutils.IsExprTypeInterface) {
return false
}
if !astutils.IsFieldListType(funcType.Params, 1, astutils.IsExprTypeInterface) {
return false
}
if !astutils.HasFieldListLength(funcType.Results, 1) {
return false
}
return astutils.IsFieldListType(funcType.Results, 0, astutils.IsExprTypeError)
}
// IsTypeValueValidationFunc returns if the type is ValueValidationFunc from the customdiff package
func IsTypeValueValidationFunc(t types.Type) bool {
switch t := t.(type) {
case *types.Named:
return IsNamedType(t, TypeNameValueValidationFunc)
case *types.Pointer:
return IsTypeValueValidationFunc(t.Elem())
default:
return false
}
}
// ValueValidationFuncInfo represents all gathered ValueValidationFunc data for easier access
type ValueValidationFuncInfo struct {
AstFuncDecl *ast.FuncDecl
AstFuncLit *ast.FuncLit
Body *ast.BlockStmt
Node ast.Node
Pos token.Pos
Type *ast.FuncType
TypesInfo *types.Info
}
// NewValueValidationFuncInfo instantiates a ValueValidationFuncInfo
func NewValueValidationFuncInfo(node ast.Node, info *types.Info) *ValueValidationFuncInfo {
result := &ValueValidationFuncInfo{
TypesInfo: info,
}
switch node := node.(type) {
case *ast.FuncDecl:
result.AstFuncDecl = node
result.Body = node.Body
result.Node = node
result.Pos = node.Pos()
result.Type = node.Type
case *ast.FuncLit:
result.AstFuncLit = node
result.Body = node.Body
result.Node = node
result.Pos = node.Pos()
result.Type = node.Type
}
return result
}