Git Product home page Git Product logo

Comments (6)

dsnet avatar dsnet commented on May 3, 2024

I apologize, but I don't understand what you're trying to do. Do you have an example?

from go-cmp.

akakjs avatar akakjs commented on May 3, 2024

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.

dsnet avatar dsnet commented on May 3, 2024

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 nth step is a cmp.StructField, you can obtain the original reflect.StructField from the type information held in the n-1th 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.

dsnet avatar dsnet commented on May 3, 2024

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.

akakjs avatar akakjs commented on May 3, 2024

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.

dsnet avatar dsnet commented on May 3, 2024

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)

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.