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

Default values doesn't work (SetDefault function) #158

Closed
vlkv opened this issue Feb 5, 2016 · 10 comments
Closed

Default values doesn't work (SetDefault function) #158

vlkv opened this issue Feb 5, 2016 · 10 comments

Comments

@vlkv
Copy link

vlkv commented Feb 5, 2016

Hi! I'm trying to use default values for my configuration params. They seem to be NOT working. Here is the example:

package main
import (
    "fmt"
    "github.com/spf13/viper"
)

func main() {
    c := viper.New()
    c.SetConfigFile("./config.json")
    c.SetConfigType("json")
    err := c.ReadInConfig()
    if err != nil {
        fmt.Errorf("Error %v\n", err)
    }

    //c.SetTypeByDefaultValue(false)
    c.SetDefault("params.p2", "TWO")
    c.SetDefault("params.p3", "333")

    fmt.Println(c.Get("params.p1"))
    fmt.Println(c.Get("params.p2"))
    fmt.Println(c.Get("params.p3"))
    fmt.Println(c.Get("params.p4"))

    /* Output:
    one   - correct
    two   - correct
    <nil> - WRONG! Expected 333
    <nil> - correct
    */
}

And the config.json is

{
    "params": {
        "p1": "one",
        "p2": "two"
    }
}
@vlkv
Copy link
Author

vlkv commented Feb 5, 2016

If I'm doing something wrong, please correct my example. Thanks in advance

@vlkv vlkv closed this as completed Feb 5, 2016
@vlkv vlkv reopened this Feb 5, 2016
@kofalt
Copy link

kofalt commented Feb 8, 2016

I think this is a duplicate of #71. A workaround can be found in that ticket: #71 (comment).

@vlkv
Copy link
Author

vlkv commented Feb 9, 2016

Thanks for response, but after changing the line

c.SetDefault("params.p3", "333")

with

if c.Get("params.p3") == nil {
            c.SetDefault("params.p3", "333")
    }

It still doesn't work. The output is the same

@jgsqware
Copy link
Contributor

@vlkv Do you find a way to make it work finally?

I made it like that:

if c.Get("params.p3") == nil {
  c.Set("params.p3", "333")
}

@vlkv
Copy link
Author

vlkv commented Apr 1, 2016

c.Set("params.p3", "333") <<<<< I'd like to do this BEFORE load actual values from file. If I would do this AFTER - then defaults lose their sense, becase I would do this every time in every place after load.

So, I've created my own config module https://github.com/vlkv/ya-go-config It's not as powerfull as viper, it's tiny little simple. But I don't need more.

@jgsqware
Copy link
Contributor

jgsqware commented Apr 1, 2016

@vlkv I'm reading the conf file before the c.Get()

If Get == nilmeans that the parameter was not in the file. So it set a value.

It's behaviour of default value isn't it?

@vlkv
Copy link
Author

vlkv commented Apr 1, 2016

#158 (comment) Here is what I've expected

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
@ghost
Copy link

ghost commented Jul 10, 2022

func getEnvValue(key string, defaultValue string) string {
value := viper.GetString(key)
if value != "" {
return value
}
return defaultValue
}

@Zanda256
Copy link

func getEnvValue(key string, defaultValue string) string {
value := viper.GetString(key)
if value != "" {
return value
}
return defaultValue
}

This surely works.

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

5 participants