spf13 / viper

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

New release 1.18 is now returning an error when loading "expected a map, got 'ptr'" #1702

Closed mrz1836 closed 7 months ago

mrz1836 commented 7 months ago

Preflight Checklist

Viper Version

1.18.0

Go Version

1.20

Config Source

Environment variables, Files

Format

JSON

Repl.it link

No response

Code reproducing the issue

Here is a snippet for how we are loading our config from JSON files.

We have a very large amount of configuration settings which is a series of structs or nested structs.

/*
// Set a replacer for replacing double underscore with nested period
    replacer := strings.NewReplacer(".", "__")
    viper.SetEnvKeyReplacer(replacer)

    // Set the prefix
    viper.SetEnvPrefix(EnvironmentPrefix)

    // Use env vars
    viper.AutomaticEnv()

    // Get the embedded envs directory
    var files []fs.DirEntry
    if files, err = envDir.ReadDir("envs"); err != nil {
        return nil, err
    }

    // Set the configuration type
    viper.SetConfigType("json")

    // Loop through the files
    for _, file := range files {
        if file.Name() == environment+".json" {
            var f fs.File
            if f, err = envDir.Open("envs/" + file.Name()); err != nil {
                return nil, err
            }
            if err = viper.ReadConfig(f); err != nil {
                return nil, err
            }
        }
    }

    // Unmarshal into values struct
    if err = viper.Unmarshal(&_appConfig); err != nil {
        err = fmt.Errorf("error loading viper values: %w", err)
        logger.Data(2, logger.ERROR, err.Error())
        return nil, err
    }
*/

Expected Behavior

Our project has been using viper for the past year with no issues. Just installed the new version and now we cannot load our config.

Actual Behavior

Received unexpected error: error loading viper values: '' expected a map, got 'ptr'

Steps To Reproduce

Working on steps to reproduce since I cannot copy our private code into this issue tracker.

Additional Information

For now we will be targeting the previous release of Viper.

Is there something that changed in this new version that would should be aware of?

github-actions[bot] commented 7 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! ❤️

mrz1836 commented 7 months ago

I think I figured it out:

Changed my config to be a value instead of a pointer:

_appConfig := Config{
AWS:       &AWSConfig{},
Datastore: &DatastoreConfig{},
Email:     &EmailConfig{},
Services:  &Services{},
}

vs my old way:

_appConfig := &Config{
AWS:       &AWSConfig{},
Datastore: &DatastoreConfig{},
Email:     &EmailConfig{},
Services:  &Services{},
}

Now you unmarshal into a pointer of a value

// Unmarshal into values struct
    if err = viper.Unmarshal(&_appConfig); err != nil {
        err = fmt.Errorf("error loading viper values: %w", err)
        logger.Data(2, logger.ERROR, err.Error())
        return nil, err
    }