2022-03-01 10:34:33 -05:00
|
|
|
// Copyright 2018, The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmpopts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type xformFilter struct{ xform cmp.Option }
|
|
|
|
|
|
|
|
func (xf xformFilter) filter(p cmp.Path) bool {
|
|
|
|
for _, ps := range p {
|
|
|
|
if t, ok := ps.(cmp.Transform); ok && t.Option() == xf.xform {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-01-08 04:25:45 -05:00
|
|
|
// AcyclicTransformer returns a [cmp.Transformer] with a filter applied that ensures
|
2022-03-01 10:34:33 -05:00
|
|
|
// that the transformer cannot be recursively applied upon its own output.
|
|
|
|
//
|
|
|
|
// An example use case is a transformer that splits a string by lines:
|
2022-11-05 18:24:04 -04:00
|
|
|
//
|
2022-03-01 10:34:33 -05:00
|
|
|
// AcyclicTransformer("SplitLines", func(s string) []string{
|
|
|
|
// return strings.Split(s, "\n")
|
|
|
|
// })
|
|
|
|
//
|
2024-01-08 04:25:45 -05:00
|
|
|
// Had this been an unfiltered [cmp.Transformer] instead, this would result in an
|
2022-03-01 10:34:33 -05:00
|
|
|
// infinite cycle converting a string to []string to [][]string and so on.
|
|
|
|
func AcyclicTransformer(name string, xformFunc interface{}) cmp.Option {
|
|
|
|
xf := xformFilter{cmp.Transformer(name, xformFunc)}
|
|
|
|
return cmp.FilterPath(xf.filter, xf.xform)
|
|
|
|
}
|