mcuadros / go-defaults

Go structures with default values using tags

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Default for `time.Duration` not working when using units

wonderhoss opened this issue · comments

For fields of type time.Duration the documentation suggests that a default of "1m" should be parsed into the appropriate duration (i.e. 1 * time.Minute).
This does not seem to be working.

Defaults are only correctly applied when using numbers only (which are then interpreted as nanoseconds:

import (
	"fmt"
	"time"	
	"github.com/mcuadros/go-defaults"
)

type Foo struct {
	ConnectionTimeout time.Duration `default:"2000000000"`
}

func main() {
	c := &Foo{}
	defaults.SetDefaults(c)
	fmt.Printf("Foo: %+v", c)
}

Output:

Foo: &{ConnectionTimeout:2s}

However:

import (
	"fmt"
	"time"	
	"github.com/mcuadros/go-defaults"
)

type Foo struct {
	ConnectionTimeout time.Duration `default:"20s"`
}

func main() {
	c := &Foo{}
	defaults.SetDefaults(c)
	fmt.Printf("Foo: %+v", c)
}

Output:

Foo: &{ConnectionTimeout:0s}