I have two separated modules, let's call them
- FileSaver
- VideoFileProducer
I want to separate them as much as possible, they shouldn't know about each others' structures and interfaces.
FileSaver module has these interfaces and structs: https://play.golang.org/p/OHObk0EPlG VideoFileProducer module has these interfaces and structs: https://play.golang.org/p/iBOOaSPLc0
As you can see:
fileSaver.Fileis identical to thevideoFileProducer.VideoFilefileSaver.FileSaverinterface is identical to thevideoFileProducer.VideoFileSaverinterface (the difference is that the methodSaveFilereceives different_but_identical structures:FileandVideoFile)fileSaver.fileSaverImplimplements thefileSaver.FileSaverinterfacevideoFileSaver.NewVideoFileProducerreceives implementation ofvideoFileSaver.VideoFileSaverinterface
In Main package I want to Inject FileSaver into the VideoFileProducer: https://play.golang.org/p/8B1iMCLLBE
Of course I'm getting an error (https://play.golang.org/p/Eqr2ylKiQ6):
FileSaver does not implement VideoFileSaver (wrong type for SaveFile method)
have SaveFile(File)
want SaveFile(VideoFile)
These two structures are identical, we can easily convert VideoFile to File and File to VideoFile, but anyway they have different types.
In short I want to create FileSaver interface in Main package, cast it to VideoFileSaver and send it to the VideoFileProducer.
Or somehow to adapt FileSaver interface to be used as VideoFileSaver interface.
Working solution #1 (anonymous structure)
https://play.golang.org/p/NJRUCCp2xS
This solution uses anonymous structure and it can be too big to declare it as anonymous in so many places (I've got only one method but there can be many of them in real application).
Not working approach #1 (Fileable/VideoFileable interfaces)
https://play.golang.org/p/0rP4nn7j-T
I've tried to make the structure (File,VideoFile) to implement the interface that has a method for conversion it to the specified anonymous structure, but haven't succeeded.
Not working approach #1 (interface{})
https://play.golang.org/p/8Q1KHhzl_g
Previous approach but with using interface{}. Not working because of Interface Assertion (underlying types are different)
Is there any way to convert interface directly to other type than the underlying one (without using reflection)?
Working solution #2 (Fileable/VideoFileable + interface{})
https://play.golang.org/p/xacNEjFG7D
I've combined last two approaches and added the using of switch to check "is underlying interface identical to the required one". So now I can to use VideoFileable as Fileable.
However it's not the best-looking solution and I'm still looking for a solution which uses as less anonymous structures and interface{} as possible.
Working solution #3 (Fileable/VideoFileable + reflection)
https://play.golang.org/p/4UPEmNkG-E
Converting VideoFileable to Fileable
Working solution #4 (interface{} + reflection)
https://play.golang.org/p/AneYQVm2Gc
VideoFile converts directly to File
I'm not satisfied with any of the solutions above and still looking for something more clean.