-
Notifications
You must be signed in to change notification settings - Fork 125
internal: add structdiff.IsEqual() #4203
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
Conversation
| zero1 := v1Field.IsZero() | ||
| zero2 := v2Field.IsZero() | ||
|
|
||
| if zero1 || zero2 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the other way around? Fields that are nil but are present in ForceSendFields should be set to their zero value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure what you mean, can you post an example testcase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider:
a = foo{
myfield: nil,
ForceSendFields: []{"myfield"}
}
b = foo{
myfield: "",
ForceSendFields: []{"myfield"}
}
a and b should be equal in this case, regardless of the omitempty annotation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what type is myfield? it's not a string, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Go SDK ForceSendFields is only meaningful on "basic" types https://github.com/databricks/databricks-sdk-go/blob/main/marshal/types.go#L5
in cli repo libs/structs and libs/dyn we also apply on maps and slices. However, AFAIK it is never applied it to pointers.
|
Commit: 091ee38
26 interesting tests: 21 KNOWN, 2 flaky, 2 FAIL, 1 SKIP
Top 50 slowest tests (at least 2 minutes):
|
| return false | ||
| } | ||
|
|
||
| return equalValues(v1, v2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just use reflect.DeepEqual here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, see the comment about ForceSendFields
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is prompted by a real issue btw where I did use reflect.DeepEqual but it did not do what's expected because ForceSendFields in one case was set but not in the other.
SDK always adds fields to ForceSendFields regardless of whether they are needed there or not. Our code in many places only adds it if the value is zero.
|
Commit: 2f558af
65 interesting tests: 23 MISS, 21 KNOWN, 16 FAIL, 3 flaky, 1 PANIC, 1 SKIP
Top 50 slowest tests (at least 2 minutes):
|
Changes
New function structdiff.IsEqual() which follows the same logic as structdiff.GetStructDiff() but does not build a diff.
Why
Need this in #4201
reflect.DeepEqual() does not work for types with ForceSendFields because ForceSendFields can have more or less fields in it without changing the actual value.
Tests
Unit tests.