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

Reusing nested config structs #87

Open
zefirior opened this issue May 12, 2021 · 0 comments
Open

Reusing nested config structs #87

zefirior opened this issue May 12, 2021 · 0 comments

Comments

@zefirior
Copy link

zefirior commented May 12, 2021

Hi! I am using confita configs as API for internal libraries in my projects. I've tried to use config structs multiple times in single project setup. And I found a problem for my usage. Field tags can't repeat in one config:

type HTTPConfig struct {
	Addr string `config:"addr"`
	Port int `config:"port"`
}

type AppConfig struct {
	MetricsHTTPServer HTTPConfig
	LoggingHTTPServer HTTPConfig
}

There are not possibilities for defining different options for different servers. Also if we'll try to use flags as backend, we'll get panic (below):

panic: ./cmd/example/example flag redefined: addr

goroutine 1 [running]:
flag.(*FlagSet).Var(0xc0001371a0, 0x551e910, 0xc000614440, 0x4f288db, 0xc, 0x0, 0x0)
	/-S/contrib/go/_std/src/flag/flag.go:871 +0x485
flag.(*FlagSet).StringVar(...)
	/-S/contrib/go/_std/src/flag/flag.go:760
flag.(*FlagSet).String(0xc0001371a0, 0x4f288db, 0xc, 0x0, 0x0, 0x0, 0x0, 0xc000614430)
	/-S/contrib/go/_std/src/flag/flag.go:773 +0xa5
flag.String(...)
	/-S/contrib/go/_std/src/flag/flag.go:780
github.com/heetch/confita/backend/flags.(*Backend).LoadStruct(0x5d76df8, 0x552dea8, 0xc0005d2340, 0xc00025f080, 0x0, 0x0)
	/-S/vendor/github.com/heetch/confita/backend/flags/flags.go:73 +0x548
github.com/heetch/confita.(*Loader).resolve(0xc00025f050, 0x552dea8, 0xc0005d2340, 0xc00025f080, 0x5d30240, 0x199)
	/-S/vendor/github.com/heetch/confita/config.go:171 +0x788
github.com/heetch/confita.(*Loader).Load(0xc00025f050, 0x552dea8, 0xc0005d2340, 0x4f63ac0, 0x5d30240, 0x49, 0xd)
	/-S/vendor/github.com/heetch/confita/config.go:58 +0x20a

It may be solved using something like prefixes. Example usage is following:

type HTTPConfig struct {
	Addr string `config:"addr"`
	Port int `config:"port"`
}

type AppConfig struct {
	MetricsHTTPServer HTTPConfig `config:"prefix=metrics"`
	LoggingHTTPServer HTTPConfig `config:"prefix=logging"`
}

Or more idiomatic if it needs. I think this feature will be useful not only me))

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

1 participant