diff options
Diffstat (limited to 'vendor/github.com/google/go-cmp/cmp/internal/function/func.go')
-rw-r--r-- | vendor/github.com/google/go-cmp/cmp/internal/function/func.go | 64 |
1 files changed, 57 insertions, 7 deletions
diff --git a/vendor/github.com/google/go-cmp/cmp/internal/function/func.go b/vendor/github.com/google/go-cmp/cmp/internal/function/func.go index 4c35ff1..ace1dbe 100644 --- a/vendor/github.com/google/go-cmp/cmp/internal/function/func.go +++ b/vendor/github.com/google/go-cmp/cmp/internal/function/func.go | |||
@@ -2,25 +2,34 @@ | |||
2 | // Use of this source code is governed by a BSD-style | 2 | // Use of this source code is governed by a BSD-style |
3 | // license that can be found in the LICENSE.md file. | 3 | // license that can be found in the LICENSE.md file. |
4 | 4 | ||
5 | // Package function identifies function types. | 5 | // Package function provides functionality for identifying function types. |
6 | package function | 6 | package function |
7 | 7 | ||
8 | import "reflect" | 8 | import ( |
9 | "reflect" | ||
10 | "regexp" | ||
11 | "runtime" | ||
12 | "strings" | ||
13 | ) | ||
9 | 14 | ||
10 | type funcType int | 15 | type funcType int |
11 | 16 | ||
12 | const ( | 17 | const ( |
13 | _ funcType = iota | 18 | _ funcType = iota |
14 | 19 | ||
20 | tbFunc // func(T) bool | ||
15 | ttbFunc // func(T, T) bool | 21 | ttbFunc // func(T, T) bool |
22 | trbFunc // func(T, R) bool | ||
16 | tibFunc // func(T, I) bool | 23 | tibFunc // func(T, I) bool |
17 | trFunc // func(T) R | 24 | trFunc // func(T) R |
18 | 25 | ||
19 | Equal = ttbFunc // func(T, T) bool | 26 | Equal = ttbFunc // func(T, T) bool |
20 | EqualAssignable = tibFunc // func(T, I) bool; encapsulates func(T, T) bool | 27 | EqualAssignable = tibFunc // func(T, I) bool; encapsulates func(T, T) bool |
21 | Transformer = trFunc // func(T) R | 28 | Transformer = trFunc // func(T) R |
22 | ValueFilter = ttbFunc // func(T, T) bool | 29 | ValueFilter = ttbFunc // func(T, T) bool |
23 | Less = ttbFunc // func(T, T) bool | 30 | Less = ttbFunc // func(T, T) bool |
31 | ValuePredicate = tbFunc // func(T) bool | ||
32 | KeyValuePredicate = trbFunc // func(T, R) bool | ||
24 | ) | 33 | ) |
25 | 34 | ||
26 | var boolType = reflect.TypeOf(true) | 35 | var boolType = reflect.TypeOf(true) |
@@ -32,10 +41,18 @@ func IsType(t reflect.Type, ft funcType) bool { | |||
32 | } | 41 | } |
33 | ni, no := t.NumIn(), t.NumOut() | 42 | ni, no := t.NumIn(), t.NumOut() |
34 | switch ft { | 43 | switch ft { |
44 | case tbFunc: // func(T) bool | ||
45 | if ni == 1 && no == 1 && t.Out(0) == boolType { | ||
46 | return true | ||
47 | } | ||
35 | case ttbFunc: // func(T, T) bool | 48 | case ttbFunc: // func(T, T) bool |
36 | if ni == 2 && no == 1 && t.In(0) == t.In(1) && t.Out(0) == boolType { | 49 | if ni == 2 && no == 1 && t.In(0) == t.In(1) && t.Out(0) == boolType { |
37 | return true | 50 | return true |
38 | } | 51 | } |
52 | case trbFunc: // func(T, R) bool | ||
53 | if ni == 2 && no == 1 && t.Out(0) == boolType { | ||
54 | return true | ||
55 | } | ||
39 | case tibFunc: // func(T, I) bool | 56 | case tibFunc: // func(T, I) bool |
40 | if ni == 2 && no == 1 && t.In(0).AssignableTo(t.In(1)) && t.Out(0) == boolType { | 57 | if ni == 2 && no == 1 && t.In(0).AssignableTo(t.In(1)) && t.Out(0) == boolType { |
41 | return true | 58 | return true |
@@ -47,3 +64,36 @@ func IsType(t reflect.Type, ft funcType) bool { | |||
47 | } | 64 | } |
48 | return false | 65 | return false |
49 | } | 66 | } |
67 | |||
68 | var lastIdentRx = regexp.MustCompile(`[_\p{L}][_\p{L}\p{N}]*$`) | ||
69 | |||
70 | // NameOf returns the name of the function value. | ||
71 | func NameOf(v reflect.Value) string { | ||
72 | fnc := runtime.FuncForPC(v.Pointer()) | ||
73 | if fnc == nil { | ||
74 | return "<unknown>" | ||
75 | } | ||
76 | fullName := fnc.Name() // e.g., "long/path/name/mypkg.(*MyType).(long/path/name/mypkg.myMethod)-fm" | ||
77 | |||
78 | // Method closures have a "-fm" suffix. | ||
79 | fullName = strings.TrimSuffix(fullName, "-fm") | ||
80 | |||
81 | var name string | ||
82 | for len(fullName) > 0 { | ||
83 | inParen := strings.HasSuffix(fullName, ")") | ||
84 | fullName = strings.TrimSuffix(fullName, ")") | ||
85 | |||
86 | s := lastIdentRx.FindString(fullName) | ||
87 | if s == "" { | ||
88 | break | ||
89 | } | ||
90 | name = s + "." + name | ||
91 | fullName = strings.TrimSuffix(fullName, s) | ||
92 | |||
93 | if i := strings.LastIndexByte(fullName, '('); inParen && i >= 0 { | ||
94 | fullName = fullName[:i] | ||
95 | } | ||
96 | fullName = strings.TrimSuffix(fullName, ".") | ||
97 | } | ||
98 | return strings.TrimSuffix(name, ".") | ||
99 | } | ||