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

Trouble accessing nested StringSlice #93

Closed
ghost opened this issue Jul 12, 2015 · 5 comments
Closed

Trouble accessing nested StringSlice #93

ghost opened this issue Jul 12, 2015 · 5 comments

Comments

@ghost
Copy link

ghost commented Jul 12, 2015

I have the following yaml.

filters:
  SomeTitle:
      reject:
         - "foo"
         - "bar"
      include:
         - "the"
         - "at"

I am trying to access the reject and include lists like so. viper.GetStringSlice("filters.SomeTitle.reject"), but I always end up with a empty slice.

@szuecs
Copy link

szuecs commented Sep 24, 2015

I have a similar issue.
If I request viper.Get("Bar") I get a map, but viper.Get("Bar.a") does not work.
In my usecase I would like to load the configuration which is a nested map, which does not work too. If I use a slice as base structure it works.

// I would like to "eager" load the whole nested config
viper.Marshal(&config)

json:

Bar:
  a: foo
  b:
  - 1
  - 2

@szuecs
Copy link

szuecs commented Sep 24, 2015

I found my bug.

type Config struct {
    Entry map[string]ConfigEntry
}

type ConfigEntry struct {
    Name     string   // URL key to request, p.e. "foo" for https://macarque:8082/foo
    UrlFmt   string   // URL formatstring to request on every HostPort, p.e. "https://%s/foo.json"
    HostPort []string // Host Port string, p.e. ["web01:80", "web02:80"]
    Agg      string   // Name of the aggregation function, p.e. "sum"
    Key      string   // Key from json result to pull out the data to aggregate on
}

and the wrong yaml was:

  elected:
    hostPort:
     - web01:80
    - web02:80
    - web03:80
    agg: sum
    key: bar
  cpus_percent:
    hostPort:
    - web01:80
    - web02:80
    - web03:80
    agg: sum
    key: foo

the right yaml would be:

Entities:             <---- was missing
  elected:
    hostPort:
     - web01:80
    - web02:80
    - web03:80
    agg: sum
    key: bar
  cpus_percent:
    hostPort:
    - web01:80
    - web02:80
    - web03:80
    agg: sum
    key: foo

@jgsqware
Copy link
Contributor

jgsqware commented Feb 5, 2016

@gregf Have you fin d a way? I got the same problem

@ghost
Copy link
Author

ghost commented Feb 7, 2016

@jgsqware No I haven't.

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 PR #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

3 participants