Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validator option once at state creation #128

Merged
merged 1 commit into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 3 additions & 10 deletions cmp/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ type state struct {
}

func newState(opts []Option) *state {
s := new(state)
// Always ensure a validator option exists to validate the inputs.
s := &state{opts: Options{validator{}}}
for _, opt := range opts {
s.processOption(opt)
}
Expand Down Expand Up @@ -293,16 +294,8 @@ func (s *state) compareAny(step PathStep) {
}

func (s *state) tryOptions(t reflect.Type, vx, vy reflect.Value) bool {
// If there were no FilterValues, we will not detect invalid inputs,
// so manually check for them and append a validator if necessary.
// We still evaluate the options since an ignore can override invalid.
opts := s.opts
if !vx.IsValid() || !vx.CanInterface() || !vy.IsValid() || !vy.CanInterface() {
opts = Options{opts, validator{}}
}

// Evaluate all filters and apply the remaining options.
if opt := opts.filter(s, t, vx, vy); opt != nil {
if opt := s.opts.filter(s, t, vx, vy); opt != nil {
opt.apply(s, vx, vy)
return true
}
Expand Down
23 changes: 17 additions & 6 deletions cmp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ type valuesFilter struct {

func (f valuesFilter) filter(s *state, t reflect.Type, vx, vy reflect.Value) applicableOption {
if !vx.IsValid() || !vx.CanInterface() || !vy.IsValid() || !vy.CanInterface() {
return validator{}
return nil
}
if (f.typ == nil || t.AssignableTo(f.typ)) && s.callTTBFunc(f.fnc, vx, vy) {
return f.opt.filter(s, t, vx, vy)
Expand Down Expand Up @@ -207,18 +207,29 @@ func (ignore) String() string
// missing map entries. Both values are validator only for unexported fields.
type validator struct{ core }

func (validator) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableOption {
return validator{}
func (validator) filter(_ *state, _ reflect.Type, vx, vy reflect.Value) applicableOption {
if !vx.IsValid() || !vy.IsValid() {
return validator{}
}
if !vx.CanInterface() || !vy.CanInterface() {
return validator{}
}
return nil
}
func (validator) apply(s *state, vx, vy reflect.Value) {
// Implies missing slice element or map entry.
if !vx.IsValid() || !vy.IsValid() {
s.report(vx.IsValid() == vy.IsValid(), 0)
return
}

// Unable to Interface implies unexported field without visibility access.
if (vx.IsValid() && !vx.CanInterface()) || (vy.IsValid() && !vy.CanInterface()) {
if !vx.CanInterface() || !vy.CanInterface() {
const help = "consider using AllowUnexported or cmpopts.IgnoreUnexported"
panic(fmt.Sprintf("cannot handle unexported field: %#v\n%s", s.curPath, help))
}

// Implies missing slice element or map entry.
s.report(vx.IsValid() == vy.IsValid(), 0)
panic("not reachable")
}

// identRx represents a valid identifier according to the Go specification.
Expand Down