-
Notifications
You must be signed in to change notification settings - Fork 4
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
verify.Values should compare private fields like reflect.DeepEqual #3
Comments
Deep equals uses unsafe and the functionality is not exported in the reflection package. Of course verify.Values could use reflect.DeepEqual for unexported fields yet that also introduces NaN != NaN. The only way I see to do this right is a values equivalent from @awalterschulze his goredrive. |
This should be easy to do with goderive and much harder with reflect and unsafe. |
verify.Values is used for testing. Why should I generate and maintain another method just for this purpose? It is not relevant whether the implementation is pure. It only matters whether it works.
|
On second thought the Oh, relying on generating the |
vendored in structs should not be exposed. Or can you give an example, maybe I am not understanding the problem. |
If I have to generate an As for vendored structs that shouldn't be exposed: How about |
Aha makes sense. Good point. I'll have to think about that. |
Well you could just take the bloody minded (aka engineering) approach:
Then your test is 100% safe and you will probably get some info from the verify, if not just have to dig a bit deeper. |
|
@sandymcp The point is not whether or not I can work around it. Of course, I can and it isn't much work. I think the main argument is that the results from Since you use |
The common usecase is testing indeed. That |
So there are two issues here:
|
goderive can now derive equal for structs in other packages as well as private fields. So I think its now comparable to reflect.DeepEqual func deriveEqualextra_PrivateFieldAndNoEqualMethod(this, that extra.PrivateFieldAndNoEqualMethod) bool {
thisv := reflect.Indirect(reflect.ValueOf(&this))
thatv := reflect.Indirect(reflect.ValueOf(&that))
return *(*int64)(unsafe.Pointer(thisv.FieldByName("number").UnsafeAddr())) == *(*int64)(unsafe.Pointer(thatv.FieldByName("number").UnsafeAddr())) &&
deriveEqualSliceOfint64(*(*[]int64)(unsafe.Pointer(thisv.FieldByName("numbers").UnsafeAddr())), *(*[]int64)(unsafe.Pointer(thatv.FieldByName("numbers").UnsafeAddr()))) &&
((*(**int64)(unsafe.Pointer(thisv.FieldByName("ptr").UnsafeAddr())) == nil && *(**int64)(unsafe.Pointer(thatv.FieldByName("ptr").UnsafeAddr())) == nil) || (*(**int64)(unsafe.Pointer(thisv.FieldByName("ptr").UnsafeAddr())) != nil && *(**int64)(unsafe.Pointer(thatv.FieldByName("ptr").UnsafeAddr())) != nil && **(**int64)(unsafe.Pointer(thisv.FieldByName("ptr").UnsafeAddr())) == **(**int64)(unsafe.Pointer(thatv.FieldByName("ptr").UnsafeAddr())))) &&
deriveEqualSliceOfPtrToint64(*(*[]*int64)(unsafe.Pointer(thisv.FieldByName("numberpts").UnsafeAddr())), *(*[]*int64)(unsafe.Pointer(thatv.FieldByName("numberpts").UnsafeAddr()))) &&
deriveEqualPtrToextra_StructWithoutEqualMethod(*(**extra.StructWithoutEqualMethod)(unsafe.Pointer(thisv.FieldByName("strct").UnsafeAddr())), *(**extra.StructWithoutEqualMethod)(unsafe.Pointer(thatv.FieldByName("strct").UnsafeAddr())))
} |
Writing a modification of equal function to rather turn an error with a description of the diff should now be an easy modification of the equal generator, but I have other functions with a higher priority for now. |
Just found this newcomer: https://godoc.org/github.com/google/go-cmp/cmp |
I have already added it as an issue :) |
Sjoerd told me about it. There was a talk at GopherCon. |
The following test case shows that
reflect.DeepEqual
can compare private fields whereasverify.Values
cannot. Tested with Go 1.8This prints
The text was updated successfully, but these errors were encountered: