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