Comments (6)
I apologize, but I don't understand what you're trying to do. Do you have an example?
from go-cmp.
Sorry I needed more coffee before I created this issue.
What I'm trying to do is create an audit history of user modifications to a business entity (represented by a struct). So after I saw the support for custom reporters in cmp.Diff I thought I'd compare the modified and original instances of the struct, and use a custom reporter to create the list of changes.
However I also don't want to expose the presence of any embedded types in the reported paths, so the path to each change matches the field location when serialised to JSON.
In code what I'm trying to do is something like this :
type Name struct {
FirstName string
LastName string
}
type Human struct {
Name
Age int
}
Alice := Human{
Age: 31,
Name: Name{
FirstName:"Alice",
LastName: "Example",
},
}
// Alice's evil twin
Malice := Human{
Age: 31,
Name: Name{
FirstName:"Malice",
LastName: "Example",
},
}
cmp.Diff(Alice, Malice, cmp.Reporter(exampleReporter))
// currently in PushStep(PathStep) when the reporter visits the "Name" field and I have to
// append that to the field path, and I end up with:
// {
// {
// Path : "Name.FirstName"
// ...
// },
// }
// But if I knew the field in the PathStep was an embedded type field I could skip that PathStep, and get :
// {
// {
// Path : "FirstName"
// ...
// },
// }
from go-cmp.
I see. For this use case, I don't think any modification is necessary.
I have a forked implementation that copies the value of the Anonymous from reflect.StructField to cmp.StructField in the compareStruct and that seems to do the job with minimal modification.
Given the cmp.Path
where the n
th step is a cmp.StructField
, you can obtain the original reflect.StructField
from the type information held in the n-1
th step.
Specifically:
var p cmp.Path = ...
f, _ := p[n-1].Type().FieldByName(p[n].Name())
From that reflect.StructField
you can interrogate whether Anonymous
was set or not.
from go-cmp.
Also, since Reporter
doesn't provide you a Path
, you can trivially re-construct it by push/popping the provided PathSteps
. See the example.
from go-cmp.
Thanks for the fast replies! And for taking the time to point me in the right direction, I'll give it a go.
from go-cmp.
Glad it helped, I'm going to close this as resolved. Feel free to respond if you need more assistance.
from go-cmp.
Related Issues (20)
- 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
- Pointers as Map index keys are not respected as equal HOT 3
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.