Preflight Checklist

  • [X] I have searched the issue tracker for an issue that matches the one I want to file, without success.
  • [X] I am not looking for support or already pursued the available support channels without success.
  • [X] I have checked the troubleshooting guide for my problem, without success.

Viper Version

1.14.0

Go Version

1.18.3

Config Source

Files, Defaults

Format

YAML

Repl.it link

https://go.dev/play/p/UGzGMgL7nAg

Code reproducing the issue

package main

import (
    "fmt"
    "reflect"
    "strings"

    "github.com/spf13/viper"
)

const DefaultHost = "0.0.0.0"

type Addr struct {
    Host string `mapstructure:"host"` // optional, should default to 0.0.0.0
    Port int    `mapstructure:"port"` // required
}

type MyConfig struct {
    Addrs []Addr `mapstructure:"addrs"`
}

const someYaml = `
addrs:
- port: 8080
- port: 8081
  host: localhost
`

var expected = MyConfig{
    Addrs: []Addr{
        {Host: DefaultHost, Port: 8080},
        {Host: "localhost", Port: 8081},
    },
}

func main() {
    viper.SetConfigType("yaml")
    if err := viper.ReadConfig(strings.NewReader(someYaml)); err != nil {
        panic(err)
    }

    addrs := viper.Get("addrs").([]interface{})
    fmt.Println("addrs before setting defaults:", addrs)
    for i := range addrs {
        key := fmt.Sprintf("addrs.%d.host", i)
        fmt.Printf("setting key %q to default: %q\n", key, DefaultHost)
        viper.SetDefault(key, DefaultHost)
    }
    fmt.Println("addrs after setting defaults:", viper.Get("addrs"))

    cfg := MyConfig{}
    if err := viper.Unmarshal(&cfg); err != nil {
        panic(err)
    }

    if !reflect.DeepEqual(cfg, expected) {
        panic(fmt.Errorf("config not correct:\nexpected:\n%+v\ngot:\n%+v", expected, cfg))
    }
}

Expected Behavior

Expect to be able to set default values on slice elements, and have them set appropriately with viper.Get and viper.Unmarshal.

Actual Behavior

Default values on slice elements are ignored.

Steps To Reproduce

Run above code/playground. Notice that the "addrs before setting defaults:" and "addrs after setting defaults:" log lines are the same. Also note the panic when comparing the actual vs expected.

Additional Information

No response

Comment From: github-actions[bot]

👋 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! ❤️

Comment From: github-actions[bot]

Issues with no activity for 30 days are marked stale and subject to being closed.