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

Issue-105 Support time.Duration in viper.Unmarshal #205

Merged
merged 4 commits into from
Sep 23, 2016
Merged

Issue-105 Support time.Duration in viper.Unmarshal #205

merged 4 commits into from
Sep 23, 2016

Conversation

yauhen-l
Copy link
Contributor

@yauhen-l yauhen-l commented Jul 9, 2016

Just add DecodeHook to DecoderConfig.
Allows to unmarshal into struct with time.Duration fields:

package main

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

type Config struct {
    Timeout time.Duration
    String  string
}

func main() {
    var cfg Config

    viper.SetConfigType("yaml")

    var yamlExample = []byte(`
Timeout: 200ms
String: it works!
`)

    viper.ReadConfig(bytes.NewBuffer(yamlExample))
    viper.Unmarshal(&cfg)

    // {Timeout:200ms String:it works!}
    fmt.Printf("%+v\n", cfg)
}

@CLAassistant
Copy link

CLAassistant commented Jul 9, 2016

CLA assistant check
All committers have signed the CLA.

@jyellick
Copy link

This changeset looks very much like the code used in UnmashalExact.

Fixing Issue #105 is something I'd certainly like to see done, but could this patch be refactored so that Unmarshal and UnmarshalExact are combined to use the same codepath? This patch, as is, I believe will still produce errors in UnmarshalExact.

@awfm9
Copy link

awfm9 commented Sep 20, 2016

I will try to take a look at this PR soon. In the meantime, if the suggestion by @jyellick could be implemented, that would be great.

@awfm9 awfm9 self-assigned this Sep 20, 2016
@awfm9
Copy link

awfm9 commented Sep 22, 2016

@yauhen-l it seems straight forward enough to add the hook to the UnmarshalExact as well, since it already uses a custom configuration. Could you add that?

@yauhen-l
Copy link
Contributor Author

@awishformore, I will take a look in the evening

@yauhen-l
Copy link
Contributor Author

@awishformore, @jyellick please review

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

Successfully merging this pull request may close these issues.

4 participants