Comments (4)
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.
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.
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.
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)
- Default support for the `netip` package HOT 1
- New minor release of the library? HOT 4
- cmp.Diff() switching from property diffs to "new object" diff when object changes significantly HOT 1
- Inconsistent whitespace produced by cmp.Diff HOT 2
- cmpopts: add EquateUnsetFields(want) or FilterFieldsFrom(want) HOT 1
- IgnoreFields and nested struct HOT 2
- Enable a dependency update tool HOT 2
- Package Name Mismatches HOT 4
- unhelpful output for difference in slices HOT 1
- How to implement jests.js's `expect.any(String)` like functionality? HOT 1
- cmpopts.IgnoreFields does not work on structs with an Equal method HOT 1
- Performance Issue Comparing Array Types HOT 1
- Naming clash for new cmp standard package HOT 4
- Comparing two fields with diffirent types that are semantically equivalent HOT 3
- String diff for UUIDs can use strings.Join syntax which makes it more confusing HOT 4
- Reporter truncating important context on diff output but leaving others alone
- How to add more context to Diff output?
- Error at init when running on Windows ARM HOT 3
- `cmp.Diff`: want to compare unexport structs like `reflect.DeepEqual`
- Comparer doesn't run when type is wrapped in empty interface HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-cmp.