-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
109 lines (100 loc) · 2.56 KB
/
helpers.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
package gotype
import (
"strings"
)
// IsErrorAggregator reports whether or not the given
// type implements the "ErrorAggregator" interface.
func IsErrorAggregator(t *Type) bool {
var hasAddError, hasError bool
for _, m := range t.Methods {
switch m.Name {
// Error(key string, val any, rule string, args ...any)
case "Error":
sig := m.Type // signature
if !sig.IsVariadic {
return false
}
in, out := sig.In, sig.Out
if len(in) != 4 || len(out) != 0 {
return false
}
if !in[0].Type.IsGoString() || !in[1].Type.IsGoAny() ||
!in[2].Type.IsGoString() || !in[3].Type.IsGoAnySlice() {
return false
}
hasAddError = true
// Out() error
case "Out":
sig := m.Type // signature
in, out := sig.In, sig.Out
if len(in) != 0 || len(out) != 1 {
return false
}
if !out[0].Type.IsGoError() {
return false
}
hasError = true
}
}
return hasAddError && hasError
}
// IsErrorConstructor reports whether or not the given
// type implements the "ErrorConstructor" interface.
func IsErrorConstructor(t *Type) bool {
var hasError bool
for _, m := range t.Methods {
switch m.Name {
// Error(key string, val any, rule string, args ...any) error
case "Error":
if IsErrorConstructorFunc(m.Type) {
hasError = true
}
}
}
return hasError
}
// IsErrorConstructorFunc reports whether or not the given
// type matches the "ErrorConstructor" function signature.
//
// func(key string, val any, rule string, args ...any) error
//
func IsErrorConstructorFunc(t *Type) bool {
if t.Kind != K_FUNC {
return false
}
if !t.IsVariadic {
return false
}
in, out := t.In, t.Out
if len(in) != 4 || len(out) != 1 {
return false
}
if !in[0].Type.IsGoString() || !in[1].Type.IsGoAny() ||
!in[2].Type.IsGoString() || !in[3].Type.IsGoAnySlice() ||
!out[0].Type.IsGoError() {
return false
}
return true
}
// nameOfValidateMethod scans the method set of the given type for
// a method with the signature "func() error" and with its name equal
// to the given prefix+"validate" (case insensitive). If it finds a
// match it will return that method's full name (with case preserved),
// and if there's no match it will it will return an empty string.
func nameOfValidateMethod(t *Type, prefix string) string {
name := strings.ToLower(prefix) + "validate"
for _, m := range t.Methods {
if strings.ToLower(m.Name) == name {
sig := m.Type // signature
in, out := sig.In, sig.Out
if len(in) != 0 || len(out) != 1 {
return ""
}
if !out[0].Type.IsGoError() {
return ""
}
return m.Name
}
}
return ""
}