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

defaults for nested keys don't work with AllSettings or Unmarshal #162

Closed
jbeda opened this issue Feb 13, 2016 · 1 comment
Closed

defaults for nested keys don't work with AllSettings or Unmarshal #162

jbeda opened this issue Feb 13, 2016 · 1 comment

Comments

@jbeda
Copy link

jbeda commented Feb 13, 2016

package main

import (
    "bytes"
    "fmt"
    "github.com/kr/pretty"
    "github.com/spf13/viper"
)

type Config struct {
    Explicit int
    Nested   struct {
        Explicit int
        Default  int
    }
}

func main() {
    var input = []byte(`
  explicit: 1
  nested:
    explicit: 2
`)
    viper.SetDefault("nested.default", 3)
    viper.SetConfigType("yaml")
    viper.ReadConfig(bytes.NewBuffer(input))

    fmt.Println("viper.AllSettings:")
    pretty.Println(viper.AllSettings())

    var c Config
    viper.Unmarshal(&c)
    fmt.Println("\nviper.Unmarshal:")
    pretty.Println(c)
}

Produces:

viper.AllSettings:
map[string]interface {}{
    "explicit": int(1),
    "nested":   map[interface {}]interface {}{
        "explicit": int(2),
    },
    "nested.default": nil,
}

viper.Unmarshal:
main.Config{
    Explicit: 1,
    Nested:   struct { Explicit int; Default int }{Explicit:2, Default:0},
}

I would expect the defaults would be applied in both of these cases.

awfm9 pushed a commit that referenced this issue Oct 8, 2016
Fixes #71, #93, #158, #168, #209, #141, #160, #162, #190

* Fixed: indentation in comment
* Fixed: Get() returns nil when nested element not found
* Fixed: insensitiviseMaps() made recursive so that nested keys are lowercased
* Fixed: order of expected<=>actual in assert.Equal() statements
* Fixed: find() looks into "overrides" first
* Fixed: TestBindPFlags() to use a new Viper instance
* Fixed: removed extra aliases from display in Debug()
* Added: test for checking precedence of dot-containing keys.
* Fixed: Set() and SetDefault() insert nested values
* Added: tests for overriding nested values
* Changed: AllKeys() includes all keys / AllSettings() includes overridden nested values
* Added: test for shadowed nested key
* Fixed: properties parsing generates nested maps
* Fixed: Get() and IsSet() work correctly on nested values
* Changed: modifier README.md to reflect changes
@awfm9
Copy link

awfm9 commented Oct 8, 2016

Fixed by #195

@awfm9 awfm9 closed this as completed Oct 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants