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

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

Closed
1 task done
mehdy opened this issue Mar 31, 2024 · 3 comments
Closed
1 task done

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

mehdy opened this issue Mar 31, 2024 · 3 comments
Labels
kind/enhancement New feature or request

Comments

@mehdy
Copy link

mehdy commented Mar 31, 2024

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

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

@mehdy mehdy added the kind/enhancement New feature or request label Mar 31, 2024
Copy link

👋 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
Copy link

zyy17 commented Apr 10, 2024

@mehdy I have the same issue as you. Your method seems similar to the one mentioned in #584. Viper has implemented BindStruct in #1429 but didn't turn on for backward compatibility(#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
Copy link
Author

mehdy commented Apr 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants