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

Allow default values on array of struct #700

Closed
ghost opened this issue May 9, 2019 · 2 comments
Closed

Allow default values on array of struct #700

ghost opened this issue May 9, 2019 · 2 comments

Comments

@ghost
Copy link

ghost commented May 9, 2019

Let's say I have a YAML like this

connections:
  - name: demo
    hosts:
      - "demo.example.com:9200"
    username: admin
    password: password
    ssl: true
    ssl_verify: true
    version: 7
  - name: test
    hosts:
      - "test.example.com:9200"
    username: admin
    password: password

And a struct like this

type Config struct {
    Connections []struct {
        Name      string
        Hosts     []string
        Username  string
        Password  string
        Ssl       bool
        SslVerify bool
        Version   int
    }
}

And I want following code to set a default value for Config.Connections[*].Ssl :

viper.SetDefault("connections[].ssl", "true")

Right now it is not happening, probably because Config.Connections is not initialized before the Unmarshalling.

@ghost
Copy link
Author

ghost commented May 11, 2019

I realized I can set defaults in implementation of UnmarshalYAML interface for Connections struct, which will work for array of structs. So I am closing this issue.

@ghost ghost closed this as completed May 11, 2019
@renta
Copy link

renta commented Mar 26, 2020

@dealancer Have you fixed this problem a way you wanted? According to this #338 (comment) and my tests custom UnmarshalYAML will not work.

This issue was closed.
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

1 participant