Code Monkey home page Code Monkey logo

Comments (8)

SamuilDichev avatar SamuilDichev commented on May 25, 2024 8

Basic usage of them seems simple enough, for example:

func (e Example) Equals(e2 Example) bool {
	return cmp.Equal(e, e2, cmp.AllowUnexported(Example{}))
}

But even a bit more advanced seems a little odd to me. For example, using AllowUnexported together with IgnoreFields. It still gives a name must be exported panic for the IgnoreFields option when I try to ignore an unexported field.

Some examples for this would be nice.

from go-cmp.

loozhengyuan avatar loozhengyuan commented on May 25, 2024 5

I think it may be reasonable for IgnoreUnexported to be the default behaviour only if there is a way to signal to the user that unexported fields are not checked.

When I first used cmp.Diff in replacement of reflect.DeepEqual, it didn't occur to me that unexported fields will not be checked until the tests panic-ed. If, hypothetically, IgnoreUnexported was set as the default, I would have taken the passing tests as indication that the swap was successful even though the new tests will not cover changes in unexported fields.

Additionally, I think the current behaviour provides an explicit visual indication to other readers that this comparison ignores unexported fields.

if diff := cmp.Diff(a, b, cmpopts.IgnoreUnexported(SomeStruct{})); diff != "" {
	t.Errorf("mismatch (-got +want):\n%s", diff)
}

I know that this behaviour is already documented in the package documentation but, perhaps, what may be helpful (especially to new users) are more examples on the usage of these options.

from go-cmp.

droslean avatar droslean commented on May 25, 2024 2

Is there any way to IgnoreUnexported when comparing slices of a specific struct?

from go-cmp.

the80srobot avatar the80srobot commented on May 25, 2024 1

IgnoreOptions also makes a confusing statement to the effect of "never use this for types you don't control" because of some hand-wavy statement about implementation details changing in the future.

Perhaps a controversial view: IgnoreUnexported should be the default for all types. You cannot check unexported fields anyway, and even if you could, doing so in test would violate the basic tenet of Go readability, which is to test public behavior.

from go-cmp.

maeglindeveloper avatar maeglindeveloper commented on May 25, 2024

Hey there ! any update on this one ? I'm actually interested too ! :p

from go-cmp.

dsnet avatar dsnet commented on May 25, 2024

Feel free to mail out a PR fixing this.

from go-cmp.

dsnet avatar dsnet commented on May 25, 2024

Can you give an example?

from go-cmp.

ochapman avatar ochapman commented on May 25, 2024

it's not easy to understand in my first impression
eg.
GetIssueAdvancedReply_Data is member of GetIssueAdvancedReply, GetIssueAdvancedReply_Data contains some unexported fields. if want to ingore it, it seems should be:

cmp.Equal(tt.want, tt.args.rsp, cmpopts.IgnoreUnexported(pb.GetIssueAdvancedReply{}, pb.GetIssueAdvancedReply_Data{})

hope help

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.