-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Description
The mock package exposes public types that should have stayed implementation details:
const Anything = "mock.Anything": should be a constant of a private typefunc AnythingOfType(t string) AnythingOfTypeArgument:AnythingOfTypeArgumentshould have been made privatefunc IsType(t interface{}) *IsTypeArgument:IsTypeArgumentshould have been made privatefunc MatchedBy(fn interface{}) argumentMatcher:argumentMatcheris private (good) but should have been fully hidden behind an interface and not be exposed in the function signaturefunc FunctionalOptions(value ...interface{}) *FunctionalOptionsArgument:FunctionalOptionsArgumentshould have been made private
With #1441 a mitigation effort has been started by deprecating AnythingOfTypeArgument (released since v1.9.0). Unfortunately that mitigation can't be applied to the other cases.
Proposed solution
I propose to define the following private (can't be implemented outside of the package) interface:
type ArgumentMatcher interface {
matchesArg(arg interface{}) bool
}and then to retrofit the types and functions to use that interface:
const Anything = anythingArgument{}
type anythingArgument struct{}
func (anythingArgument) matchesArg(interface{}) bool {
return true
}
func AnythingOfType(string) ArgumentMatcher
type AnythingOfTypeArgument = anythingOfTypeArgument
type anythingOfTypeArgument string
func (anythingOfTypeArgument) matchesArg(interface{}) bool
func IsType(interface{}) ArgumentMatcher
type isTypeArgument struct {
T reflect.Type
}
func (*isTypeArgument) matchesArg(interface{}) bool
func MatchedBy(fn interface{}) ArgumentMatcher
type matchedByArgument struct {
F reflect.Value
}
func (*matchedByArgument) matchesArg(interface{}) bool
func FunctionalOptions(value ...interface{}) ArgumentMatcher
type functionalOptionsArgument struct {
values []interface{}
}
func (*functionalOptionsArgument) matchesArg(interface{}) boolMethod Arguments.Diff will be rewritten (simplified) by using the ArgumentsMatcher interface (instead of the type switch that directly refer to each type).
I think that this plan can be implemented without waiting for a v2 as the proposed changes would not affect correct uses of the existing API.
Misc
This ticket will also track effort to provide fixes to downstream projects which have strong references to those mock implementation details:
- fix uses of
mock.AnythingOfTypeArgument - fix uses of
mock.IsTypeArgument - fix uses of
mock.FunctionalOptionsArgument