heetch / heetch/confita

Reusing nested config structs

Open
#87 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
507
Forks
49
PR merge metrics
No merged PRs in 30d

Description

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:
```golang
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))

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading backend/flags/flags.go and config.go, especially the struct-loading and loader resolution paths shown in the panic trace. Trace how nested config tags are converted into backend option names and how repeated flags are registered. Done means nested structs can use distinct prefixes or equivalent options without duplicate flag registration, with behavior consistent across supported backends.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.