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

feat: Support string duration format in config files #408

Merged
merged 3 commits into from
Oct 7, 2019
Merged

feat: Support string duration format in config files #408

merged 3 commits into from
Oct 7, 2019

Conversation

rewenset
Copy link
Contributor

@rewenset rewenset commented Sep 30, 2019

Issue #399

This PR allows setting duration fields, which have time.Duration type, in config files not only in nanoseconds:

reply-timeout: 5000000000

but as a string too

reply-timeout: 5s

@rewenset rewenset changed the title Create new type to use when you need duration in config improvement: Parse durations values in config files (WIP) Sep 30, 2019
@rewenset
Copy link
Contributor Author

rewenset commented Oct 1, 2019

So if I change for example in ETCD config.go this field

DialTimeout time.Duration `json:"dial-timeout"`

to be not time.Duration, but config.Duration (which is newly created type introduced in this PR)
then I'll need to find all places this field is used and add .Duration, e.g.:

        dialTimeout := defaultDialTimeout
-       if yc.DialTimeout != 0 {
-               dialTimeout = yc.DialTimeout
+       if yc.DialTimeout.Duration != 0 {
+               dialTimeout = yc.DialTimeout.Duration
        }

For now, I didn't find better solution.

@ondrej-fabry please advise

@rewenset rewenset changed the title improvement: Parse durations values in config files (WIP) improvement: Parse durations values in config files Oct 3, 2019
@rewenset rewenset changed the title improvement: Parse durations values in config files feat: Support string duration format in config files Oct 3, 2019
config/parser.go Outdated
dc := &mapstructure.DecoderConfig{
DecodeHook: func(in, out reflect.Type, data interface{}) (interface{}, error) {
// Only intended to help with cases when string must be set to `time.Duration`
if in.Kind() != reflect.String || out.Name() != "Duration" {
Copy link
Member

@ondrej-fabry ondrej-fabry Oct 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking by type name is not very certain as it will match for other types named Duration.

However you can compare types directly, because the reflect.Type values are comparable:

reflect.TypeOf(out) == reflect.TypeOf(time.Duration(0))

Play

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants