Skip to content

Reusing nested config structs #87

Description

@zefirior

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))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions