xref: /aosp_15_r20/external/go-cmp/cmp/options.go (revision 88d15eac089d7f20c739ff1001d56b91872b21a1)
1// Copyright 2017, The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package cmp
6
7import (
8	"fmt"
9	"reflect"
10	"regexp"
11	"strings"
12
13	"github.com/google/go-cmp/cmp/internal/function"
14)
15
16// Option configures for specific behavior of Equal and Diff. In particular,
17// the fundamental Option functions (Ignore, Transformer, and Comparer),
18// configure how equality is determined.
19//
20// The fundamental options may be composed with filters (FilterPath and
21// FilterValues) to control the scope over which they are applied.
22//
23// The cmp/cmpopts package provides helper functions for creating options that
24// may be used with Equal and Diff.
25type Option interface {
26	// filter applies all filters and returns the option that remains.
27	// Each option may only read s.curPath and call s.callTTBFunc.
28	//
29	// An Options is returned only if multiple comparers or transformers
30	// can apply simultaneously and will only contain values of those types
31	// or sub-Options containing values of those types.
32	filter(s *state, t reflect.Type, vx, vy reflect.Value) applicableOption
33}
34
35// applicableOption represents the following types:
36//
37//	Fundamental: ignore | validator | *comparer | *transformer
38//	Grouping:    Options
39type applicableOption interface {
40	Option
41
42	// apply executes the option, which may mutate s or panic.
43	apply(s *state, vx, vy reflect.Value)
44}
45
46// coreOption represents the following types:
47//
48//	Fundamental: ignore | validator | *comparer | *transformer
49//	Filters:     *pathFilter | *valuesFilter
50type coreOption interface {
51	Option
52	isCore()
53}
54
55type core struct{}
56
57func (core) isCore() {}
58
59// Options is a list of Option values that also satisfies the Option interface.
60// Helper comparison packages may return an Options value when packing multiple
61// Option values into a single Option. When this package processes an Options,
62// it will be implicitly expanded into a flat list.
63//
64// Applying a filter on an Options is equivalent to applying that same filter
65// on all individual options held within.
66type Options []Option
67
68func (opts Options) filter(s *state, t reflect.Type, vx, vy reflect.Value) (out applicableOption) {
69	for _, opt := range opts {
70		switch opt := opt.filter(s, t, vx, vy); opt.(type) {
71		case ignore:
72			return ignore{} // Only ignore can short-circuit evaluation
73		case validator:
74			out = validator{} // Takes precedence over comparer or transformer
75		case *comparer, *transformer, Options:
76			switch out.(type) {
77			case nil:
78				out = opt
79			case validator:
80				// Keep validator
81			case *comparer, *transformer, Options:
82				out = Options{out, opt} // Conflicting comparers or transformers
83			}
84		}
85	}
86	return out
87}
88
89func (opts Options) apply(s *state, _, _ reflect.Value) {
90	const warning = "ambiguous set of applicable options"
91	const help = "consider using filters to ensure at most one Comparer or Transformer may apply"
92	var ss []string
93	for _, opt := range flattenOptions(nil, opts) {
94		ss = append(ss, fmt.Sprint(opt))
95	}
96	set := strings.Join(ss, "\n\t")
97	panic(fmt.Sprintf("%s at %#v:\n\t%s\n%s", warning, s.curPath, set, help))
98}
99
100func (opts Options) String() string {
101	var ss []string
102	for _, opt := range opts {
103		ss = append(ss, fmt.Sprint(opt))
104	}
105	return fmt.Sprintf("Options{%s}", strings.Join(ss, ", "))
106}
107
108// FilterPath returns a new Option where opt is only evaluated if filter f
109// returns true for the current Path in the value tree.
110//
111// This filter is called even if a slice element or map entry is missing and
112// provides an opportunity to ignore such cases. The filter function must be
113// symmetric such that the filter result is identical regardless of whether the
114// missing value is from x or y.
115//
116// The option passed in may be an Ignore, Transformer, Comparer, Options, or
117// a previously filtered Option.
118func FilterPath(f func(Path) bool, opt Option) Option {
119	if f == nil {
120		panic("invalid path filter function")
121	}
122	if opt := normalizeOption(opt); opt != nil {
123		return &pathFilter{fnc: f, opt: opt}
124	}
125	return nil
126}
127
128type pathFilter struct {
129	core
130	fnc func(Path) bool
131	opt Option
132}
133
134func (f pathFilter) filter(s *state, t reflect.Type, vx, vy reflect.Value) applicableOption {
135	if f.fnc(s.curPath) {
136		return f.opt.filter(s, t, vx, vy)
137	}
138	return nil
139}
140
141func (f pathFilter) String() string {
142	return fmt.Sprintf("FilterPath(%s, %v)", function.NameOf(reflect.ValueOf(f.fnc)), f.opt)
143}
144
145// FilterValues returns a new Option where opt is only evaluated if filter f,
146// which is a function of the form "func(T, T) bool", returns true for the
147// current pair of values being compared. If either value is invalid or
148// the type of the values is not assignable to T, then this filter implicitly
149// returns false.
150//
151// The filter function must be
152// symmetric (i.e., agnostic to the order of the inputs) and
153// deterministic (i.e., produces the same result when given the same inputs).
154// If T is an interface, it is possible that f is called with two values with
155// different concrete types that both implement T.
156//
157// The option passed in may be an Ignore, Transformer, Comparer, Options, or
158// a previously filtered Option.
159func FilterValues(f interface{}, opt Option) Option {
160	v := reflect.ValueOf(f)
161	if !function.IsType(v.Type(), function.ValueFilter) || v.IsNil() {
162		panic(fmt.Sprintf("invalid values filter function: %T", f))
163	}
164	if opt := normalizeOption(opt); opt != nil {
165		vf := &valuesFilter{fnc: v, opt: opt}
166		if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
167			vf.typ = ti
168		}
169		return vf
170	}
171	return nil
172}
173
174type valuesFilter struct {
175	core
176	typ reflect.Type  // T
177	fnc reflect.Value // func(T, T) bool
178	opt Option
179}
180
181func (f valuesFilter) filter(s *state, t reflect.Type, vx, vy reflect.Value) applicableOption {
182	if !vx.IsValid() || !vx.CanInterface() || !vy.IsValid() || !vy.CanInterface() {
183		return nil
184	}
185	if (f.typ == nil || t.AssignableTo(f.typ)) && s.callTTBFunc(f.fnc, vx, vy) {
186		return f.opt.filter(s, t, vx, vy)
187	}
188	return nil
189}
190
191func (f valuesFilter) String() string {
192	return fmt.Sprintf("FilterValues(%s, %v)", function.NameOf(f.fnc), f.opt)
193}
194
195// Ignore is an Option that causes all comparisons to be ignored.
196// This value is intended to be combined with FilterPath or FilterValues.
197// It is an error to pass an unfiltered Ignore option to Equal.
198func Ignore() Option { return ignore{} }
199
200type ignore struct{ core }
201
202func (ignore) isFiltered() bool                                                     { return false }
203func (ignore) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableOption { return ignore{} }
204func (ignore) apply(s *state, _, _ reflect.Value)                                   { s.report(true, reportByIgnore) }
205func (ignore) String() string                                                       { return "Ignore()" }
206
207// validator is a sentinel Option type to indicate that some options could not
208// be evaluated due to unexported fields, missing slice elements, or
209// missing map entries. Both values are validator only for unexported fields.
210type validator struct{ core }
211
212func (validator) filter(_ *state, _ reflect.Type, vx, vy reflect.Value) applicableOption {
213	if !vx.IsValid() || !vy.IsValid() {
214		return validator{}
215	}
216	if !vx.CanInterface() || !vy.CanInterface() {
217		return validator{}
218	}
219	return nil
220}
221func (validator) apply(s *state, vx, vy reflect.Value) {
222	// Implies missing slice element or map entry.
223	if !vx.IsValid() || !vy.IsValid() {
224		s.report(vx.IsValid() == vy.IsValid(), 0)
225		return
226	}
227
228	// Unable to Interface implies unexported field without visibility access.
229	if !vx.CanInterface() || !vy.CanInterface() {
230		help := "consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported"
231		var name string
232		if t := s.curPath.Index(-2).Type(); t.Name() != "" {
233			// Named type with unexported fields.
234			name = fmt.Sprintf("%q.%v", t.PkgPath(), t.Name()) // e.g., "path/to/package".MyType
235			if _, ok := reflect.New(t).Interface().(error); ok {
236				help = "consider using cmpopts.EquateErrors to compare error values"
237			}
238		} else {
239			// Unnamed type with unexported fields. Derive PkgPath from field.
240			var pkgPath string
241			for i := 0; i < t.NumField() && pkgPath == ""; i++ {
242				pkgPath = t.Field(i).PkgPath
243			}
244			name = fmt.Sprintf("%q.(%v)", pkgPath, t.String()) // e.g., "path/to/package".(struct { a int })
245		}
246		panic(fmt.Sprintf("cannot handle unexported field at %#v:\n\t%v\n%s", s.curPath, name, help))
247	}
248
249	panic("not reachable")
250}
251
252// identRx represents a valid identifier according to the Go specification.
253const identRx = `[_\p{L}][_\p{L}\p{N}]*`
254
255var identsRx = regexp.MustCompile(`^` + identRx + `(\.` + identRx + `)*$`)
256
257// Transformer returns an Option that applies a transformation function that
258// converts values of a certain type into that of another.
259//
260// The transformer f must be a function "func(T) R" that converts values of
261// type T to those of type R and is implicitly filtered to input values
262// assignable to T. The transformer must not mutate T in any way.
263//
264// To help prevent some cases of infinite recursive cycles applying the
265// same transform to the output of itself (e.g., in the case where the
266// input and output types are the same), an implicit filter is added such that
267// a transformer is applicable only if that exact transformer is not already
268// in the tail of the Path since the last non-Transform step.
269// For situations where the implicit filter is still insufficient,
270// consider using cmpopts.AcyclicTransformer, which adds a filter
271// to prevent the transformer from being recursively applied upon itself.
272//
273// The name is a user provided label that is used as the Transform.Name in the
274// transformation PathStep (and eventually shown in the Diff output).
275// The name must be a valid identifier or qualified identifier in Go syntax.
276// If empty, an arbitrary name is used.
277func Transformer(name string, f interface{}) Option {
278	v := reflect.ValueOf(f)
279	if !function.IsType(v.Type(), function.Transformer) || v.IsNil() {
280		panic(fmt.Sprintf("invalid transformer function: %T", f))
281	}
282	if name == "" {
283		name = function.NameOf(v)
284		if !identsRx.MatchString(name) {
285			name = "λ" // Lambda-symbol as placeholder name
286		}
287	} else if !identsRx.MatchString(name) {
288		panic(fmt.Sprintf("invalid name: %q", name))
289	}
290	tr := &transformer{name: name, fnc: reflect.ValueOf(f)}
291	if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
292		tr.typ = ti
293	}
294	return tr
295}
296
297type transformer struct {
298	core
299	name string
300	typ  reflect.Type  // T
301	fnc  reflect.Value // func(T) R
302}
303
304func (tr *transformer) isFiltered() bool { return tr.typ != nil }
305
306func (tr *transformer) filter(s *state, t reflect.Type, _, _ reflect.Value) applicableOption {
307	for i := len(s.curPath) - 1; i >= 0; i-- {
308		if t, ok := s.curPath[i].(Transform); !ok {
309			break // Hit most recent non-Transform step
310		} else if tr == t.trans {
311			return nil // Cannot directly use same Transform
312		}
313	}
314	if tr.typ == nil || t.AssignableTo(tr.typ) {
315		return tr
316	}
317	return nil
318}
319
320func (tr *transformer) apply(s *state, vx, vy reflect.Value) {
321	step := Transform{&transform{pathStep{typ: tr.fnc.Type().Out(0)}, tr}}
322	vvx := s.callTRFunc(tr.fnc, vx, step)
323	vvy := s.callTRFunc(tr.fnc, vy, step)
324	step.vx, step.vy = vvx, vvy
325	s.compareAny(step)
326}
327
328func (tr transformer) String() string {
329	return fmt.Sprintf("Transformer(%s, %s)", tr.name, function.NameOf(tr.fnc))
330}
331
332// Comparer returns an Option that determines whether two values are equal
333// to each other.
334//
335// The comparer f must be a function "func(T, T) bool" and is implicitly
336// filtered to input values assignable to T. If T is an interface, it is
337// possible that f is called with two values of different concrete types that
338// both implement T.
339//
340// The equality function must be:
341//   - Symmetric: equal(x, y) == equal(y, x)
342//   - Deterministic: equal(x, y) == equal(x, y)
343//   - Pure: equal(x, y) does not modify x or y
344func Comparer(f interface{}) Option {
345	v := reflect.ValueOf(f)
346	if !function.IsType(v.Type(), function.Equal) || v.IsNil() {
347		panic(fmt.Sprintf("invalid comparer function: %T", f))
348	}
349	cm := &comparer{fnc: v}
350	if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
351		cm.typ = ti
352	}
353	return cm
354}
355
356type comparer struct {
357	core
358	typ reflect.Type  // T
359	fnc reflect.Value // func(T, T) bool
360}
361
362func (cm *comparer) isFiltered() bool { return cm.typ != nil }
363
364func (cm *comparer) filter(_ *state, t reflect.Type, _, _ reflect.Value) applicableOption {
365	if cm.typ == nil || t.AssignableTo(cm.typ) {
366		return cm
367	}
368	return nil
369}
370
371func (cm *comparer) apply(s *state, vx, vy reflect.Value) {
372	eq := s.callTTBFunc(cm.fnc, vx, vy)
373	s.report(eq, reportByFunc)
374}
375
376func (cm comparer) String() string {
377	return fmt.Sprintf("Comparer(%s)", function.NameOf(cm.fnc))
378}
379
380// Exporter returns an Option that specifies whether Equal is allowed to
381// introspect into the unexported fields of certain struct types.
382//
383// Users of this option must understand that comparing on unexported fields
384// from external packages is not safe since changes in the internal
385// implementation of some external package may cause the result of Equal
386// to unexpectedly change. However, it may be valid to use this option on types
387// defined in an internal package where the semantic meaning of an unexported
388// field is in the control of the user.
389//
390// In many cases, a custom Comparer should be used instead that defines
391// equality as a function of the public API of a type rather than the underlying
392// unexported implementation.
393//
394// For example, the reflect.Type documentation defines equality to be determined
395// by the == operator on the interface (essentially performing a shallow pointer
396// comparison) and most attempts to compare *regexp.Regexp types are interested
397// in only checking that the regular expression strings are equal.
398// Both of these are accomplished using Comparers:
399//
400//	Comparer(func(x, y reflect.Type) bool { return x == y })
401//	Comparer(func(x, y *regexp.Regexp) bool { return x.String() == y.String() })
402//
403// In other cases, the cmpopts.IgnoreUnexported option can be used to ignore
404// all unexported fields on specified struct types.
405func Exporter(f func(reflect.Type) bool) Option {
406	if !supportExporters {
407		panic("Exporter is not supported on purego builds")
408	}
409	return exporter(f)
410}
411
412type exporter func(reflect.Type) bool
413
414func (exporter) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableOption {
415	panic("not implemented")
416}
417
418// AllowUnexported returns an Options that allows Equal to forcibly introspect
419// unexported fields of the specified struct types.
420//
421// See Exporter for the proper use of this option.
422func AllowUnexported(types ...interface{}) Option {
423	m := make(map[reflect.Type]bool)
424	for _, typ := range types {
425		t := reflect.TypeOf(typ)
426		if t.Kind() != reflect.Struct {
427			panic(fmt.Sprintf("invalid struct type: %T", typ))
428		}
429		m[t] = true
430	}
431	return exporter(func(t reflect.Type) bool { return m[t] })
432}
433
434// Result represents the comparison result for a single node and
435// is provided by cmp when calling Report (see Reporter).
436type Result struct {
437	_     [0]func() // Make Result incomparable
438	flags resultFlags
439}
440
441// Equal reports whether the node was determined to be equal or not.
442// As a special case, ignored nodes are considered equal.
443func (r Result) Equal() bool {
444	return r.flags&(reportEqual|reportByIgnore) != 0
445}
446
447// ByIgnore reports whether the node is equal because it was ignored.
448// This never reports true if Equal reports false.
449func (r Result) ByIgnore() bool {
450	return r.flags&reportByIgnore != 0
451}
452
453// ByMethod reports whether the Equal method determined equality.
454func (r Result) ByMethod() bool {
455	return r.flags&reportByMethod != 0
456}
457
458// ByFunc reports whether a Comparer function determined equality.
459func (r Result) ByFunc() bool {
460	return r.flags&reportByFunc != 0
461}
462
463// ByCycle reports whether a reference cycle was detected.
464func (r Result) ByCycle() bool {
465	return r.flags&reportByCycle != 0
466}
467
468type resultFlags uint
469
470const (
471	_ resultFlags = (1 << iota) / 2
472
473	reportEqual
474	reportUnequal
475	reportByIgnore
476	reportByMethod
477	reportByFunc
478	reportByCycle
479)
480
481// Reporter is an Option that can be passed to Equal. When Equal traverses
482// the value trees, it calls PushStep as it descends into each node in the
483// tree and PopStep as it ascend out of the node. The leaves of the tree are
484// either compared (determined to be equal or not equal) or ignored and reported
485// as such by calling the Report method.
486func Reporter(r interface {
487	// PushStep is called when a tree-traversal operation is performed.
488	// The PathStep itself is only valid until the step is popped.
489	// The PathStep.Values are valid for the duration of the entire traversal
490	// and must not be mutated.
491	//
492	// Equal always calls PushStep at the start to provide an operation-less
493	// PathStep used to report the root values.
494	//
495	// Within a slice, the exact set of inserted, removed, or modified elements
496	// is unspecified and may change in future implementations.
497	// The entries of a map are iterated through in an unspecified order.
498	PushStep(PathStep)
499
500	// Report is called exactly once on leaf nodes to report whether the
501	// comparison identified the node as equal, unequal, or ignored.
502	// A leaf node is one that is immediately preceded by and followed by
503	// a pair of PushStep and PopStep calls.
504	Report(Result)
505
506	// PopStep ascends back up the value tree.
507	// There is always a matching pop call for every push call.
508	PopStep()
509}) Option {
510	return reporter{r}
511}
512
513type reporter struct{ reporterIface }
514type reporterIface interface {
515	PushStep(PathStep)
516	Report(Result)
517	PopStep()
518}
519
520func (reporter) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableOption {
521	panic("not implemented")
522}
523
524// normalizeOption normalizes the input options such that all Options groups
525// are flattened and groups with a single element are reduced to that element.
526// Only coreOptions and Options containing coreOptions are allowed.
527func normalizeOption(src Option) Option {
528	switch opts := flattenOptions(nil, Options{src}); len(opts) {
529	case 0:
530		return nil
531	case 1:
532		return opts[0]
533	default:
534		return opts
535	}
536}
537
538// flattenOptions copies all options in src to dst as a flat list.
539// Only coreOptions and Options containing coreOptions are allowed.
540func flattenOptions(dst, src Options) Options {
541	for _, opt := range src {
542		switch opt := opt.(type) {
543		case nil:
544			continue
545		case Options:
546			dst = flattenOptions(dst, opt)
547		case coreOption:
548			dst = append(dst, opt)
549		default:
550			panic(fmt.Sprintf("invalid option type: %T", opt))
551		}
552	}
553	return dst
554}
555