GithubHelp home page GithubHelp logo

Question: How do I apply a transformer to a child field of a struct depending on the value of another field in the struct? about go-cmp HOT 4 CLOSED

F21 avatar F21 commented on August 19, 2024
Question: How do I apply a transformer to a child field of a struct depending on the value of another field in the struct?

from go-cmp.

Comments (4)

dsnet avatar dsnet commented on August 19, 2024 1

Glad I could help!

You should consider adding the example code as an example in the godoc.

Yea, the examples could use some love. It's on my list of things to do someday.

from go-cmp.

dsnet avatar dsnet commented on August 19, 2024

I think (according to my understanding, it will attempt to apply the transfer from the Mystruct struct and will not attempt to apply it to the fields within Mystruct.

You are correct that it will apply on Mystruct, which you can transform into something else entirely. The cmpopts.AcyclicTransformer would only be needed if the output is (or recursively contains) the Mystruct type.

Alternatively, you could use cmp.FilterPath, which since 0.3.0 is strictly more powerful than cmp.FilterValues. Every cmp.PathStep in the cmp.Path has a Values method, which can be used to interrogate information about the parent values.

Thus, you could do something like this:

cmp.FilterPath(func(p cmp.Path) bool {
    // Filter specifically for accesses to the Mystruct.Data field.
    if p.Index(-2).Type() != reflect.TypeOf(Mystruct{}) {
    	return false
    }
    if sf, ok := p.Index(-1).(cmp.StructField); !ok || sf.Name != "Data" {
    	return false
    }

    // Filter specifically for some specific data type.
    x, y :=  p.Index(-2).Values()
    return x.(Mystruct).DataType == myType && y.(Mystruct).DataType == myType
}, cmp.Transformer("unmarshalMyType", func(b []byte) T {
   return unmarshalMyType(b)
}))

from go-cmp.

dsnet avatar dsnet commented on August 19, 2024

Here's a more advanced example with multiple transformed types:

cmp.FilterPath(func(p cmp.Path) bool {
    // Filter specifically for accesses to the Mystruct.Data field.
    if p.Index(-2).Type() != reflect.TypeOf(Mystruct{}) {
    	return false
    }
    if sf, ok := p.Index(-1).(cmp.StructField); !ok || sf.Name != "Data" {
    	return false
    }
}, cmp.Options{
	// The sub-options below can safely assume that the filter above is applied.

	// This filter only applies on "fooType".
	cmp.FilterPath(func(p cmp.Path) bool {
	    x, y :=  p.Index(-2).Values()
	    return x.(Mystruct).DataType == fooType && y.(Mystruct).DataType == fooType
	}, cmp.Transformer("unmarshalFoo", func(b []byte) Foo {
		return unmarshalFoo(b)
	}),

	// This filter only applies on "barType".
	cmp.FilterPath(func(p cmp.Path) bool {
	    x, y :=  p.Index(-2).Values()
	    return x.(Mystruct).DataType == barType && y.(Mystruct).DataType == barType
	}, cmp.Transformer("unmarshalBar", func(b []byte) Bar {
		return unmarshalBar(b)
	}),
})

// In case you're wondering what happens when x.DataType != y.DataType,
// then none of the options apply and cmp will compare Mystruct as a normal
// struct, which would report a difference (which is probably what you want).

from go-cmp.

F21 avatar F21 commented on August 19, 2024

Thanks @dsnet ! That was very useful. You should consider adding the example code as an example in the godoc.

from go-cmp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.