-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdopts.go
More file actions
76 lines (66 loc) · 1.88 KB
/
cmdopts.go
File metadata and controls
76 lines (66 loc) · 1.88 KB
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
package assert
import (
"go/token"
"reflect"
"github.com/google/go-cmp/cmp"
)
// compareExported returns an [cmp.Option] that compares all exported fields of a struct,
func compareExported() cmp.Option {
return cmp.Exporter(func(reflect.Type) bool { return true })
}
// ignoreUnexported returns an [cmp.Option] that only ignores the immediate unexported
// fields of a struct, including anonymous fields of unexported types.
func ignoreUnexported() cmp.Option {
return cmp.FilterPath(
func(p cmp.Path) bool {
sf, ok := p.Index(-1).(cmp.StructField)
if !ok {
return false
}
return !token.IsExported(sf.Name())
},
cmp.Ignore(),
)
}
// ignoreEmptyFields returns an [cmp.Option]
// ignores fields that are empty in the expected value.
func ignoreEmptyFields() cmp.Option {
return cmp.FilterPath(
func(p cmp.Path) bool {
sf, ok := p.Index(-1).(cmp.StructField)
if !ok {
return false
}
_, wantv := sf.Values()
return isEmptyValue(wantv)
},
cmp.Ignore(),
)
}
// ignoreZeroFields returns an [cmp.Option] that
// ignores fields that have a zero value.
func ignoreZeroFields() cmp.Option {
return cmp.FilterPath(
func(p cmp.Path) bool {
sf, ok := p.Index(-1).(cmp.StructField)
if !ok {
return false
}
_, wantv := sf.Values()
return isZeroValue(wantv)
},
cmp.Ignore(),
)
}
// ignoreFieldNames returns an [cmp.Option] that ignores fields of the
// given names on a single struct type.
//
// It respects the names of exported fields that are forwarded due to struct embedding.
// The struct type is specified by passing in a value of that type.
//
// The name may be a dot-delimited string (e.g., "Foo.Bar") to ignore a
// specific sub-field that is embedded or nested within the parent struct.
func ignoreFieldNames(typ any, names ...string) cmp.Option {
sf := newStructFilter(typ, names...)
return cmp.FilterPath(sf.filter, cmp.Ignore())
}