Open jpopadak opened 5 years ago
@jpopadak Trying to understand how you can call auth.SomeThing(...)
from outside this package as it takes a private interface as parameter. Is this even possible? Even if it is, it seems strange.
If Something
is part of your public API of that package, its parameter type TokenRetriever
by definition should be public too.
All that being said, I guess the code could be changed, that it makes it possible to include _test
packages too, but I'd first like to understand if this is indeed a realistic use case.
@petergtz This is possible. There are many locations in the Go code base that does this. You can pass in any object that satisfies the private interface. The idea is, we dont want anyone directly using that private interface directly in their code (no references) so we can rename or move it around to another package and refactor as needed.
I think this is a limitation of the language / reflection itself. Mainly because these are non-exported structures.
Hey @jpopadak, sorry I never followed up on this. It was forever in my TODO box, but never got to it. My idea was not to close it until I have dived deeper into that part of the language.
Since you closed the issue, does it mean this is not a problem for you anymore or you worked around it?
I accidentally closed it. I must have clicked the button when reading back through it. 🤦♂ Sorry!
In my code base, I use many private interfaces to prevent package pollution.
I have an interface in my source code:
However, I cannot use pegomock to generate a mock for the given interface even though it is exported. I dont want to change the package my test file to be
auth
as it allows direct access to package members (we want to do API only testing and remove dependency on source code).