-
Notifications
You must be signed in to change notification settings - Fork 53
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
Allow Equal() to be used by different packages in one program. #20
base: master
Are you sure you want to change the base?
Conversation
Some packages may require Equal()'s parameters to be set in a particular way that is incompatible with other users within the same program. The global configuration parameters can be changed and restored, but this could lead to bugs due to race conditions. This commit makes the parameters that control Equal()'s operation part of a structure, Comparer, for which Equal() is now a method. Users can configure their own Comparer struct if desired. To preserve the existing package interface, the package-level Equals() method will use a default Comparer object that relies on pointers to the current global configuration parameters (pointers so that the operation of the global Equals() function will change immediately upon changing the value of any global configuration setting).
It's a good idea and would make a good change for v1.1. There's a couple things I'd change wrt the design, etc.
var defaultComparer = Comparer{
// default values
} Might have more feedback after seeing changes, if you decided to make them (and merge latest to fix the merge conflicts). |
Hi - thanks for considering the PR :-) I'll update the branch so it's based off of latest and resubmit. I'll rename MakeComparer to NewComparer, and hide the default, as you suggest. For point (2) - it seemed to me that the current implementation made FloatPrecision, MaxDepth, etc a dynamic part of the interface in that each call to deep.MaxDepth = 5
Equal(a, b) the result might be different from deep.MaxDepth = 10
Equal(a, b) To preserve that behavior, I made the Comparer struct use pointers for these fields, and by default I made the pointers point to these exported variables. So even though, with this PR, I'll definitely simplify things if you think that's the best way to go. |
Sorry for jumping in, but I was also toying around with this idea for fun. deep.CompareUnexportedFields = false
deep.FloatPrecision = 1
s1 := s{a: 0.0078125} // 1/128
s2 := s{a: 0.00390625} // 1/256
deep.Equal(s1, s2) // nil
deep.Equal(s1, s2, deep.WithCompareUnexportedFields()) // nil
deep.Equal(s1, s2, deep.WithCompareUnexportedFields(), deep.WithFloatPrecision(3)) // "a: 0.0078125 != 0.00390625" |
Hi @flga - your patch would definitely work for my needs, and I'd be happy to just switch over to your implementation :-) |
Some packages may require Equal()'s parameters to be set in a
particular way that is incompatible with other users within
the same program. The global configuration parameters can be
changed and restored, but this could lead to bugs due to race
conditions. This commit makes the parameters that control
Equal()'s operation part of a structure, Comparer, for which
Equal() is now a method. Users can configure their own
Comparer struct if desired. To preserve the existing package
interface, the package-level Equals() method will use a
default Comparer object that relies on pointers to the current
global configuration parameters (pointers so that the
operation of the global Equals() function will change
immediately upon changing the value of any global
configuration setting).