options.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 cmp/cmpopts package provides helper functions for creating options that
  20. // 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. if _, ok := reflect.New(t).Interface().(error); ok {
  209. help = "consider using cmpopts.EquateErrors to compare error values"
  210. }
  211. } else {
  212. // Unnamed type with unexported fields. Derive PkgPath from field.
  213. var pkgPath string
  214. for i := 0; i < t.NumField() && pkgPath == ""; i++ {
  215. pkgPath = t.Field(i).PkgPath
  216. }
  217. name = fmt.Sprintf("%q.(%v)", pkgPath, t.String()) // e.g., "path/to/package".(struct { a int })
  218. }
  219. panic(fmt.Sprintf("cannot handle unexported field at %#v:\n\t%v\n%s", s.curPath, name, help))
  220. }
  221. panic("not reachable")
  222. }
  223. // identRx represents a valid identifier according to the Go specification.
  224. const identRx = `[_\p{L}][_\p{L}\p{N}]*`
  225. var identsRx = regexp.MustCompile(`^` + identRx + `(\.` + identRx + `)*$`)
  226. // Transformer returns an Option that applies a transformation function that
  227. // converts values of a certain type into that of another.
  228. //
  229. // The transformer f must be a function "func(T) R" that converts values of
  230. // type T to those of type R and is implicitly filtered to input values
  231. // assignable to T. The transformer must not mutate T in any way.
  232. //
  233. // To help prevent some cases of infinite recursive cycles applying the
  234. // same transform to the output of itself (e.g., in the case where the
  235. // input and output types are the same), an implicit filter is added such that
  236. // a transformer is applicable only if that exact transformer is not already
  237. // in the tail of the Path since the last non-Transform step.
  238. // For situations where the implicit filter is still insufficient,
  239. // consider using cmpopts.AcyclicTransformer, which adds a filter
  240. // to prevent the transformer from being recursively applied upon itself.
  241. //
  242. // The name is a user provided label that is used as the Transform.Name in the
  243. // transformation PathStep (and eventually shown in the Diff output).
  244. // The name must be a valid identifier or qualified identifier in Go syntax.
  245. // If empty, an arbitrary name is used.
  246. func Transformer(name string, f interface{}) Option {
  247. v := reflect.ValueOf(f)
  248. if !function.IsType(v.Type(), function.Transformer) || v.IsNil() {
  249. panic(fmt.Sprintf("invalid transformer function: %T", f))
  250. }
  251. if name == "" {
  252. name = function.NameOf(v)
  253. if !identsRx.MatchString(name) {
  254. name = "λ" // Lambda-symbol as placeholder name
  255. }
  256. } else if !identsRx.MatchString(name) {
  257. panic(fmt.Sprintf("invalid name: %q", name))
  258. }
  259. tr := &transformer{name: name, fnc: reflect.ValueOf(f)}
  260. if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
  261. tr.typ = ti
  262. }
  263. return tr
  264. }
  265. type transformer struct {
  266. core
  267. name string
  268. typ reflect.Type // T
  269. fnc reflect.Value // func(T) R
  270. }
  271. func (tr *transformer) isFiltered() bool { return tr.typ != nil }
  272. func (tr *transformer) filter(s *state, t reflect.Type, _, _ reflect.Value) applicableOption {
  273. for i := len(s.curPath) - 1; i >= 0; i-- {
  274. if t, ok := s.curPath[i].(Transform); !ok {
  275. break // Hit most recent non-Transform step
  276. } else if tr == t.trans {
  277. return nil // Cannot directly use same Transform
  278. }
  279. }
  280. if tr.typ == nil || t.AssignableTo(tr.typ) {
  281. return tr
  282. }
  283. return nil
  284. }
  285. func (tr *transformer) apply(s *state, vx, vy reflect.Value) {
  286. step := Transform{&transform{pathStep{typ: tr.fnc.Type().Out(0)}, tr}}
  287. vvx := s.callTRFunc(tr.fnc, vx, step)
  288. vvy := s.callTRFunc(tr.fnc, vy, step)
  289. step.vx, step.vy = vvx, vvy
  290. s.compareAny(step)
  291. }
  292. func (tr transformer) String() string {
  293. return fmt.Sprintf("Transformer(%s, %s)", tr.name, function.NameOf(tr.fnc))
  294. }
  295. // Comparer returns an Option that determines whether two values are equal
  296. // to each other.
  297. //
  298. // The comparer f must be a function "func(T, T) bool" and is implicitly
  299. // filtered to input values assignable to T. If T is an interface, it is
  300. // possible that f is called with two values of different concrete types that
  301. // both implement T.
  302. //
  303. // The equality function must be:
  304. // - Symmetric: equal(x, y) == equal(y, x)
  305. // - Deterministic: equal(x, y) == equal(x, y)
  306. // - Pure: equal(x, y) does not modify x or y
  307. func Comparer(f interface{}) Option {
  308. v := reflect.ValueOf(f)
  309. if !function.IsType(v.Type(), function.Equal) || v.IsNil() {
  310. panic(fmt.Sprintf("invalid comparer function: %T", f))
  311. }
  312. cm := &comparer{fnc: v}
  313. if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
  314. cm.typ = ti
  315. }
  316. return cm
  317. }
  318. type comparer struct {
  319. core
  320. typ reflect.Type // T
  321. fnc reflect.Value // func(T, T) bool
  322. }
  323. func (cm *comparer) isFiltered() bool { return cm.typ != nil }
  324. func (cm *comparer) filter(_ *state, t reflect.Type, _, _ reflect.Value) applicableOption {
  325. if cm.typ == nil || t.AssignableTo(cm.typ) {
  326. return cm
  327. }
  328. return nil
  329. }
  330. func (cm *comparer) apply(s *state, vx, vy reflect.Value) {
  331. eq := s.callTTBFunc(cm.fnc, vx, vy)
  332. s.report(eq, reportByFunc)
  333. }
  334. func (cm comparer) String() string {
  335. return fmt.Sprintf("Comparer(%s)", function.NameOf(cm.fnc))
  336. }
  337. // Exporter returns an Option that specifies whether Equal is allowed to
  338. // introspect into the unexported fields of certain struct types.
  339. //
  340. // Users of this option must understand that comparing on unexported fields
  341. // from external packages is not safe since changes in the internal
  342. // implementation of some external package may cause the result of Equal
  343. // to unexpectedly change. However, it may be valid to use this option on types
  344. // defined in an internal package where the semantic meaning of an unexported
  345. // field is in the control of the user.
  346. //
  347. // In many cases, a custom Comparer should be used instead that defines
  348. // equality as a function of the public API of a type rather than the underlying
  349. // unexported implementation.
  350. //
  351. // For example, the reflect.Type documentation defines equality to be determined
  352. // by the == operator on the interface (essentially performing a shallow pointer
  353. // comparison) and most attempts to compare *regexp.Regexp types are interested
  354. // in only checking that the regular expression strings are equal.
  355. // Both of these are accomplished using Comparers:
  356. //
  357. // Comparer(func(x, y reflect.Type) bool { return x == y })
  358. // Comparer(func(x, y *regexp.Regexp) bool { return x.String() == y.String() })
  359. //
  360. // In other cases, the cmpopts.IgnoreUnexported option can be used to ignore
  361. // all unexported fields on specified struct types.
  362. func Exporter(f func(reflect.Type) bool) Option {
  363. if !supportExporters {
  364. panic("Exporter is not supported on purego builds")
  365. }
  366. return exporter(f)
  367. }
  368. type exporter func(reflect.Type) bool
  369. func (exporter) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableOption {
  370. panic("not implemented")
  371. }
  372. // AllowUnexported returns an Options that allows Equal to forcibly introspect
  373. // unexported fields of the specified struct types.
  374. //
  375. // See Exporter for the proper use of this option.
  376. func AllowUnexported(types ...interface{}) Option {
  377. m := make(map[reflect.Type]bool)
  378. for _, typ := range types {
  379. t := reflect.TypeOf(typ)
  380. if t.Kind() != reflect.Struct {
  381. panic(fmt.Sprintf("invalid struct type: %T", typ))
  382. }
  383. m[t] = true
  384. }
  385. return exporter(func(t reflect.Type) bool { return m[t] })
  386. }
  387. // Result represents the comparison result for a single node and
  388. // is provided by cmp when calling Report (see Reporter).
  389. type Result struct {
  390. _ [0]func() // Make Result incomparable
  391. flags resultFlags
  392. }
  393. // Equal reports whether the node was determined to be equal or not.
  394. // As a special case, ignored nodes are considered equal.
  395. func (r Result) Equal() bool {
  396. return r.flags&(reportEqual|reportByIgnore) != 0
  397. }
  398. // ByIgnore reports whether the node is equal because it was ignored.
  399. // This never reports true if Equal reports false.
  400. func (r Result) ByIgnore() bool {
  401. return r.flags&reportByIgnore != 0
  402. }
  403. // ByMethod reports whether the Equal method determined equality.
  404. func (r Result) ByMethod() bool {
  405. return r.flags&reportByMethod != 0
  406. }
  407. // ByFunc reports whether a Comparer function determined equality.
  408. func (r Result) ByFunc() bool {
  409. return r.flags&reportByFunc != 0
  410. }
  411. // ByCycle reports whether a reference cycle was detected.
  412. func (r Result) ByCycle() bool {
  413. return r.flags&reportByCycle != 0
  414. }
  415. type resultFlags uint
  416. const (
  417. _ resultFlags = (1 << iota) / 2
  418. reportEqual
  419. reportUnequal
  420. reportByIgnore
  421. reportByMethod
  422. reportByFunc
  423. reportByCycle
  424. )
  425. // Reporter is an Option that can be passed to Equal. When Equal traverses
  426. // the value trees, it calls PushStep as it descends into each node in the
  427. // tree and PopStep as it ascend out of the node. The leaves of the tree are
  428. // either compared (determined to be equal or not equal) or ignored and reported
  429. // as such by calling the Report method.
  430. func Reporter(r interface {
  431. // PushStep is called when a tree-traversal operation is performed.
  432. // The PathStep itself is only valid until the step is popped.
  433. // The PathStep.Values are valid for the duration of the entire traversal
  434. // and must not be mutated.
  435. //
  436. // Equal always calls PushStep at the start to provide an operation-less
  437. // PathStep used to report the root values.
  438. //
  439. // Within a slice, the exact set of inserted, removed, or modified elements
  440. // is unspecified and may change in future implementations.
  441. // The entries of a map are iterated through in an unspecified order.
  442. PushStep(PathStep)
  443. // Report is called exactly once on leaf nodes to report whether the
  444. // comparison identified the node as equal, unequal, or ignored.
  445. // A leaf node is one that is immediately preceded by and followed by
  446. // a pair of PushStep and PopStep calls.
  447. Report(Result)
  448. // PopStep ascends back up the value tree.
  449. // There is always a matching pop call for every push call.
  450. PopStep()
  451. }) Option {
  452. return reporter{r}
  453. }
  454. type reporter struct{ reporterIface }
  455. type reporterIface interface {
  456. PushStep(PathStep)
  457. Report(Result)
  458. PopStep()
  459. }
  460. func (reporter) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableOption {
  461. panic("not implemented")
  462. }
  463. // normalizeOption normalizes the input options such that all Options groups
  464. // are flattened and groups with a single element are reduced to that element.
  465. // Only coreOptions and Options containing coreOptions are allowed.
  466. func normalizeOption(src Option) Option {
  467. switch opts := flattenOptions(nil, Options{src}); len(opts) {
  468. case 0:
  469. return nil
  470. case 1:
  471. return opts[0]
  472. default:
  473. return opts
  474. }
  475. }
  476. // flattenOptions copies all options in src to dst as a flat list.
  477. // Only coreOptions and Options containing coreOptions are allowed.
  478. func flattenOptions(dst, src Options) Options {
  479. for _, opt := range src {
  480. switch opt := opt.(type) {
  481. case nil:
  482. continue
  483. case Options:
  484. dst = flattenOptions(dst, opt)
  485. case coreOption:
  486. dst = append(dst, opt)
  487. default:
  488. panic(fmt.Sprintf("invalid option type: %T", opt))
  489. }
  490. }
  491. return dst
  492. }