spf13 / viper

Go configuration with fangs
MIT License
26.29k stars 2.01k forks source link

Dynamically bind all environment variables based on a config struct #1797

Closed mehdy closed 2 months ago

mehdy commented 3 months ago

Preflight Checklist

Problem Description

When you want to use environment variables as a source of configuration you need to bind each item explicitly or have another source of configuration to populate the store and then be able to use AutomaticEnv to override the values from the environment variables.

So this piece of code is not doing what I'd expect:

package main

import (
        "fmt"
        "strings"

        "github.com/spf13/viper"
)

type Config struct {
        This  string `mapstructure:"this"`
        That  string `mapstructure:"that"`
        Other struct {
                Thing string `mapstructure:"thing"`
        } `mapstructure:"other"`
}

func main() {
        viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
        viper.AutomaticEnv()

        var config Config
        viper.Unmarshal(&config)
        fmt.Printf("This: %+v\n", config)
}

P.S. This feels like an issue that should've been brought up many times but couldn't find another issue like this so I apologize in-advance if it's redundant.

Proposed Solution

To solve the issue I wrote this function and it could be a part of Viper itself.

package main

import (
        "fmt"
        "reflect"
        "strings"

        "github.com/spf13/viper"
)

type Config struct {
        This  string `mapstructure:"this"`
        That  string `mapstructure:"that"`
        Other struct {
                Thing string `mapstructure:"thing"`
        } `mapstructure:"other"`
}

func listStructKeys(s interface{}) ([]string, error) {
        // Recursively get the config struct tag mapstructure
        keys := []string{}
        ct := reflect.TypeOf(s)

        if ct.Kind() != reflect.Struct {
                return nil, fmt.Errorf("listStructKeys: %v is not a struct", ct)
        }

        for i := range ct.NumField() {
                field := ct.Field(i)
                tag := field.Tag.Get("mapstructure")

                if field.Type.Kind() == reflect.Struct {
                        res, err := listStructKeys(reflect.New(field.Type).Elem().Interface())
                        if err != nil {
                                return nil, err
                        }
                        for _, k := range res {
                                keys = append(keys, fmt.Sprintf("%s.%s", tag, k))
                        }
                } else {
                        keys = append(keys, tag)
                }
        }

        return keys, nil
}

func bindAllEnv(s interface{}) error {
        keys, err := listStructKeys(s)
        if err != nil {
                return err
        }

        for _, k := range keys {
                viper.BindEnv(k)
        }

        return nil
}

func main() {
        viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
        viper.AutomaticEnv()
        bindAllEnv(Config{})

        var config Config
        viper.Unmarshal(&config)
        fmt.Printf("This: %+v\n", config)
}

So if Viper had a method like this it'd be so easy to tell it to bind all configurations defined by my Config struct.

viper.BindEnvByStruct(config{})
// or
viper.BindAllEnv(config{})
// or
viper.AutomaticEnv(config{}) // with changing the signature to `func AutomaticEnv(s ...interface{}) error` which I'd assume you wouldn't like since it could be considered a breaking change.

If you're up for it I'd like to make a PR for it.

Alternatives Considered

No response

Additional Information

No response

github-actions[bot] commented 3 months ago

👋 Thanks for reporting!

A maintainer will take a look at your issue shortly. 👀

In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.

⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9

📣 If you've already given us your feedback, you can still help by spreading the news, either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️

zyy17 commented 2 months ago

@mehdy I have the same issue as you. Your method seems similar to the one mentioned in https://github.com/spf13/viper/issues/584. Viper has implemented BindStruct in https://github.com/spf13/viper/pull/1429 but didn't turn on for backward compatibility(https://github.com/spf13/viper/issues/1706). For your case, you can turn on viper_bind_struct as the following command:

THAT=123 go run -tags viper_bind_struct test.go
mehdy commented 2 months ago

That's weird and undocumented! But then it means I should close this issue.