Skip to content

Commit 922a135

Browse files
jeeyoungkntindall
authored andcommitted
Adding time.Duration support (#12)
1 parent cc99489 commit 922a135

File tree

3 files changed

+50
-36
lines changed

3 files changed

+50
-36
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type serverEnvVars struct {
3737
// time.Time in order to set the value of the field. In this case, the
3838
// UnmarshalText method expects the string value to be in RFC 3339 format.
3939
StartTime time.Time `envvar:"START_TIME" default:"2017-10-31T14:18:00Z"`
40+
// Duration is supported via time.ParseDuration()
41+
Timeout time.Duration `envvar:"TIMEOUT" default:"30m"`
4042
}
4143

4244
func main() {

envvar/envvar.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212
"syscall"
13+
"time"
1314
)
1415

1516
// Parse parses environment variables into v, which must be a pointer to a
@@ -191,11 +192,20 @@ func setFieldVal(structField reflect.Value, name string, v string) error {
191192
case reflect.String:
192193
structField.SetString(v)
193194
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
194-
vInt, err := strconv.Atoi(v)
195-
if err != nil {
196-
return InvalidVariableError{name, v, err}
195+
if structField.Type() == reflect.TypeOf(time.Duration(0)) {
196+
// special handling for duration types.
197+
dur, err := time.ParseDuration(v)
198+
if err != nil {
199+
return InvalidVariableError{name, v, err}
200+
}
201+
structField.SetInt(int64(dur))
202+
} else {
203+
vInt, err := strconv.Atoi(v)
204+
if err != nil {
205+
return InvalidVariableError{name, v, err}
206+
}
207+
structField.SetInt(int64(vInt))
197208
}
198-
structField.SetInt(int64(vInt))
199209
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
200210
vUint, err := strconv.ParseUint(v, 10, 64)
201211
if err != nil {

envvar/envvar_test.go

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,22 @@ func TestParseCustomNames(t *testing.T) {
8585

8686
func TestParseDefaultVals(t *testing.T) {
8787
expected := defaultVars{
88-
STRING: "foo",
89-
INT: 272309480983,
90-
INT8: -4,
91-
INT16: 15893,
92-
INT32: -230984,
93-
INT64: 12,
94-
UINT: 42,
95-
UINT8: 13,
96-
UINT16: 1337,
97-
UINT32: 348904,
98-
UINT64: 12093803,
99-
FLOAT32: 0.001234,
100-
FLOAT64: 23.7,
101-
BOOL: true,
102-
TIME: time.Date(1992, 9, 29, 0, 0, 0, 0, time.UTC),
88+
STRING: "foo",
89+
INT: 272309480983,
90+
INT8: -4,
91+
INT16: 15893,
92+
INT32: -230984,
93+
INT64: 12,
94+
UINT: 42,
95+
UINT8: 13,
96+
UINT16: 1337,
97+
UINT32: 348904,
98+
UINT64: 12093803,
99+
FLOAT32: 0.001234,
100+
FLOAT64: 23.7,
101+
BOOL: true,
102+
DURATION: time.Minute * 30,
103+
TIME: time.Date(1992, 9, 29, 0, 0, 0, 0, time.UTC),
103104
CUSTOM: customUnmarshaler{
104105
strings: []string{"one", "two", "three"},
105106
},
@@ -365,23 +366,24 @@ type customNamedVars struct {
365366
}
366367

367368
type defaultVars struct {
368-
STRING string `default:"foo"`
369-
INT int `default:"272309480983"`
370-
INT8 int8 `default:"-4"`
371-
INT16 int16 `default:"15893"`
372-
INT32 int32 `default:"-230984"`
373-
INT64 int64 `default:"12"`
374-
UINT uint `default:"42"`
375-
UINT8 uint8 `default:"13"`
376-
UINT16 uint16 `default:"1337"`
377-
UINT32 uint32 `default:"348904"`
378-
UINT64 uint64 `default:"12093803"`
379-
FLOAT32 float32 `default:"0.001234"`
380-
FLOAT64 float64 `default:"23.7"`
381-
BOOL bool `default:"true"`
382-
TIME time.Time `default:"1992-09-29T00:00:00Z"`
383-
CUSTOM customUnmarshaler `default:"one,two,three"`
384-
WRAPPER customUnmarshalerWrapper `default:"apple,banana,cranberry"`
369+
STRING string `default:"foo"`
370+
INT int `default:"272309480983"`
371+
INT8 int8 `default:"-4"`
372+
INT16 int16 `default:"15893"`
373+
INT32 int32 `default:"-230984"`
374+
INT64 int64 `default:"12"`
375+
UINT uint `default:"42"`
376+
UINT8 uint8 `default:"13"`
377+
UINT16 uint16 `default:"1337"`
378+
UINT32 uint32 `default:"348904"`
379+
UINT64 uint64 `default:"12093803"`
380+
FLOAT32 float32 `default:"0.001234"`
381+
FLOAT64 float64 `default:"23.7"`
382+
BOOL bool `default:"true"`
383+
DURATION time.Duration `default:"30m"`
384+
TIME time.Time `default:"1992-09-29T00:00:00Z"`
385+
CUSTOM customUnmarshaler `default:"one,two,three"`
386+
WRAPPER customUnmarshalerWrapper `default:"apple,banana,cranberry"`
385387
}
386388

387389
type customNameAndDefaultVars struct {

0 commit comments

Comments
 (0)