gojekfarm / xtools

XTools is a submodule based repo to host re-usable Golang code.
MIT License
5 stars 0 forks source link

xload: Missing key causes key collision check to report false positive #49

Closed ajatprabha closed 1 week ago

ajatprabha commented 2 weeks ago

Issue

When a key is missing that has a prefix as well, in that case, collision detection reports a false positive. The latter example shows how missing key SERVER_ADDRESS causes the error output.

Working example ```go package main import ( "context" "fmt" "time" "github.com/gojekfarm/xtools/xload" xloadtype "github.com/gojekfarm/xtools/xload/type" ) type Pprof struct { Enabled bool `env:"ENABLED"` Address xloadtype.Listener `env:"ADDRESS"` } type ServerConfig struct { HTTP *xloadtype.Listener `env:"ADDRESS"` } type Config struct { Pprof Pprof `env:",prefix=PPROF_"` Server ServerConfig `env:",prefix=SERVER_"` ShutdownDelay time.Duration `env:"SHUTDOWN_DELAY"` } func main() { ml := xload.MapLoader{ "LOG_ENABLED": "true", "LOG_LEVEL": "debug", "PPROF_ENABLED": "true", "PPROF_ADDRESS": ":9090", "SHUTDOWN_DELAY": "4s", "SERVER_ADDRESS": ":8000", } cfg := new(Config) if err := xload.Load(context.TODO(), cfg, xload.SerialLoader(ml, xload.OSLoader()), ); err != nil { panic(err) } fmt.Printf("%+v\n", cfg) } ```
Problematic Example ```go package main import ( "context" "fmt" "time" "github.com/gojekfarm/xtools/xload" xloadtype "github.com/gojekfarm/xtools/xload/type" ) type Pprof struct { Enabled bool `env:"ENABLED"` Address xloadtype.Listener `env:"ADDRESS"` } type ServerConfig struct { HTTP *xloadtype.Listener `env:"ADDRESS"` } type Config struct { Pprof Pprof `env:",prefix=PPROF_"` Server ServerConfig `env:",prefix=SERVER_"` ShutdownDelay time.Duration `env:"SHUTDOWN_DELAY"` } func main() { ml := xload.MapLoader{ "LOG_ENABLED": "true", "LOG_LEVEL": "debug", "PPROF_ENABLED": "true", "PPROF_ADDRESS": ":9090", "SHUTDOWN_DELAY": "4s", //"SERVER_ADDRESS": ":8000", } cfg := new(Config) if err := xload.Load(context.TODO(), cfg, xload.SerialLoader(ml, xload.OSLoader()), ); err != nil { panic(err) } fmt.Printf("%+v\n", cfg) } ```
panic: xload: key collisions detected for keys: [SERVER_]