Skip to content
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

Add support for loading into interface fields which have a *struct{} in them #75

Open
nickpalmer opened this issue Jan 26, 2022 · 0 comments
Assignees

Comments

@nickpalmer
Copy link

Describe the feature

Add the ability to load a struct that has an interface element which has a struct assigned to it

Motivation

This would support go style polymorphism, where a portion of the configuration is set before Load() is called.

Consider the following:

type DatabaseProvider interface {
  GetDatabase() *gorm.DB
}
type SqliteDatabase struct {
  File string `env:"SQLITE_DATABASE_FILE"`
  // ...
}
func (s *SqliteDatabase) GetDB() *gorm.DB {
  // ...
}
type PostgresqlDatabase struct {
  Name string `env:"PSQL_DATABASE_NAME"`
  // ...
}
func (p *PostgresqlDatabase) GetDB() *gorm.DB {
  // ...
}
type AppConfig struct {
  DatabaseProvider `anonymous:"true"`
}
func NewAppConfig() (*AppConfig, error) {
  env := os.GetEnvironment("ENVIRONMENT")
  var appConfig *AppConfig
  switch env {
  case "dev":
    appConfig = &AppConfig{ &SqliteDatabase{} }
  default:
    appConfig = &AppConfig{ &PostgresqlDatabase{} }
  }
  return appConfig, configor.New(&configor.Config{}).Load(appConfig)
}

Currently configor will not load the underlying database configuration, but there is no reason that it cannot do so since the value of the interface is a struct.

Related Issues

nickpalmer added a commit to nick-codes/configor that referenced this issue Jan 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants