I have a global config for most settings (e.g. Logfiles) which I use in all Projects, for example:
project config
type Config struct {
Logfile string
Loglevel string
Database *database.Database
}
The config itself has its own project, the database has its own projects and the implementation is done in its own project. (So I have config project, database project, and app project).
I want every Project to implement the database itself since all of them have different functions attached to it. Since I don't want multiple fields for every database, I have a minimum interface which is satisfied by all Database Implementations
project database
// This interface is referenced by the Config struct using *database.Database
type Database interface {
InitDatabase() error
}
type AppDatabase interface {
InitDatabase() error // To satisfy the Database interface
AdditionalFunction1()
AdditionalFunction2()
}
type OtherAppDatabase interface {
InitDatabase() error
OtherFunc1()
}
When I want to implement the database, I want to assign the new database struct to the (project-) global configuration (e.g config.Database).
However, this does not work as I cannot assign the new struct (which implements the database interface) to the conf.Database interface, e.g.
project app
type OtherAppDatabase struct {
*sql.DB
}
func (db *OtherAppDatabase) InitDatabase() error {
conf := config.GetConfig() // this just returns the global config file
conf.Database = d // This does not work [1]
}
func (db *OtherAppDatabase) OtherFunc1() {}
[1]: Cannot use 'db' (type *OtherAppDatabase) as type *database.Database in assignment Inspection info: Reports incompatible types in binary and unary expressions.
Is there a way to keep one database field in the global config and assign the specific database to it as needed?
Thanks and best regards,