Closed criscola closed 1 year ago
Turns out, it is pretty simple! Here FilterPath
is necessary because FilterValues
is used to ignore based on values, for example to compare float values etc. but in my case I needed to ignore a path which includes the left-hand side of the "map entry" in the predicate. Simple get all the leaves with p.Last()
and you're set:
opt := cmp.FilterPath(func(p cmp.Path) bool {
vx := p.Last().String()
if vx == `["create_time"]` || vx == `["update_time"]` {
return true
}
return false
}, cmp.Ignore())
note also that if you know your struct in advance, you can use IgnoreFields
as in the docs (in my case, I don't know, hence I had to use the jsonTransformer as in #156).
Hello, I hope you can help me. I would like to know if it's possible to ignore a specific field during comparison. I had a look at this https://github.com/google/go-cmp/issues/156 and tried to modify it a bit to make it work for my use case, but I don't understand how to do it. For more context: my objective is to compare two JSON strings, ignoring a specific field by name present in various levels of the hierarchy. Thank you!
Test samples:
compared to this (only create and update times are changed):
should return a match. I know that I can use some trick like reflection to zero-out the fields, or unmarshal into a struct omitting the values, but if I can implement what I found with
go-cmp
it would definitely lead to more clear, concise and maintenable code.