Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

36 lignes
1.1 KiB

  1. // Copyright 2018, 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.md file.
  4. package cmpopts
  5. import (
  6. "github.com/google/go-cmp/cmp"
  7. )
  8. type xformFilter struct{ xform cmp.Option }
  9. func (xf xformFilter) filter(p cmp.Path) bool {
  10. for _, ps := range p {
  11. if t, ok := ps.(cmp.Transform); ok && t.Option() == xf.xform {
  12. return false
  13. }
  14. }
  15. return true
  16. }
  17. // AcyclicTransformer returns a Transformer with a filter applied that ensures
  18. // that the transformer cannot be recursively applied upon its own output.
  19. //
  20. // An example use case is a transformer that splits a string by lines:
  21. // AcyclicTransformer("SplitLines", func(s string) []string{
  22. // return strings.Split(s, "\n")
  23. // })
  24. //
  25. // Had this been an unfiltered Transformer instead, this would result in an
  26. // infinite cycle converting a string to []string to [][]string and so on.
  27. func AcyclicTransformer(name string, xformFunc interface{}) cmp.Option {
  28. xf := xformFilter{cmp.Transformer(name, xformFunc)}
  29. return cmp.FilterPath(xf.filter, xf.xform)
  30. }