options.go 19 KB

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