koding / multiconfig

Load configuration from multiple sources in Go
http://godoc.org/github.com/koding/multiconfig
MIT License
454 stars 64 forks source link

add full support for time.Duration #65

Closed arne-cl closed 6 years ago

arne-cl commented 6 years ago

Support for time.Duration was added in #28, but its usage is inconsistent between the different loaders. While it is possible to specify a duration as 10s as an ENV variable, the same is not possible e.g. for TOML files. Here, you'd have to set the same duration as 10000000000.

cihangir commented 6 years ago

Hi @arne-cl

Custom type support comes with TOML parser. You just need a duration type that satisfies the encoding.TextUnmarshaler interface:

type Config struct { // sample config struct
     Tick duration
}

type duration struct {
    time.Duration
}

func (d *duration) UnmarshalText(text []byte) error {
    var err error
    d.Duration, err = time.ParseDuration(string(text))
    return err
}
arne-cl commented 6 years ago

Hi @cihangir,

thank you for responding so quickly. If I understand you correctly, I would need to add code to make this work with TOML while it works out of the box with ENV or commandline parameters. Is there a reason for this or could I implement it for TOML and make a pull request?

Best regards, Arne

cihangir commented 6 years ago

Hi @arne-cl ,

Parsing operation is not handled in our package for TOML and YAML types. So we can't add the support in our package. The config struct given by the user passed directly to the underlying parsers. All other parsing/setting operations are handled in our package here

arne-cl commented 6 years ago

Dear @cihangir, thank you for your help! I'll see if I can get this feature into BurntSushi/toml somehow.