Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic when private fields differ #21

Closed
dgouldin opened this issue Jan 27, 2015 · 3 comments
Closed

Panic when private fields differ #21

dgouldin opened this issue Jan 27, 2015 · 3 comments

Comments

@dgouldin
Copy link

Executing the following code:

package main

import (
        "fmt"
        "github.com/kr/pretty"
)

type Foo struct {
        name string
}

func main() {
        fmt.Println(pretty.Diff(Foo{name: "foo"}, Foo{name: "bar"}))
}

Panics with the message:

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

I realize it may not be possible to actually produce the diff on private fields, but I doubt that a panic is what's intended.

@cee-dub
Copy link
Collaborator

cee-dub commented Jan 28, 2015

Thanks for the report, especially the simple code to reproduce. I'll see if I can take a look at this, unless you have started on a fix already?

@dgouldin
Copy link
Author

I don't have a fix, but my suggestion would probably be to call out the field that differs but not compute the diff itself when the field is private.

@cee-dub
Copy link
Collaborator

cee-dub commented Jan 28, 2015

I looked into this last night, but I had some degree of trouble deciphering the output from diff_test.go after making some changes in an attempt to fix the problem.

Your suggestion makes some sense, but it doesn't really address the use case of someone trying to determine how the values are actually different. I think that limits its utility (granted, it's better than a panic!).

I don't have more time in my schedule to look at it this week, unfortunately. If I had the time, I would start by updating the tests to cover your case (use a struct that has typed private fields, not just interface{} fields, including some as pointers) and also consider updating the test helpers to make it easier to decipher the failure messages. With that, the changes in diff.go should be fairly straightforward.

On Jan 27, 2015, at 19:37, David Gouldin notifications@github.com wrote:

I don't have a fix, but my suggestion would probably be to call out the field that differs but not compute the diff itself when the field is private.


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants