Skip to content

Commit 1c71011

Browse files
committed
feat: #108 add Duration() for read time.Duration type value
1 parent a5a75e7 commit 1c71011

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/gookit/ini/v2 v2.1.3
99
github.com/gookit/properties v0.2.1
1010
github.com/hashicorp/hcl v1.0.0
11-
github.com/hashicorp/hcl/v2 v2.15.0
11+
github.com/hashicorp/hcl/v2 v2.14.1
1212
github.com/imdario/mergo v0.3.13
1313
github.com/json-iterator/go v1.1.12
1414
github.com/mitchellh/mapstructure v1.5.0

go.sum

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ github.com/gookit/properties v0.2.1 h1:L86ziBZKHL6coQh1AyGyMD8V5Okj1eWN3k9Deo+hY
2929
github.com/gookit/properties v0.2.1/go.mod h1:hEmnTl5DLbGKfodoIIS698l8hqHpbhWvIznY/WAyUHc=
3030
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
3131
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
32-
github.com/hashicorp/hcl/v2 v2.15.0 h1:CPDXO6+uORPjKflkWCCwoWc9uRp+zSIPcCQ+BrxV7m8=
33-
github.com/hashicorp/hcl/v2 v2.15.0/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng=
32+
github.com/hashicorp/hcl/v2 v2.14.1 h1:x0BpjfZ+CYdbiz+8yZTQ+gdLO7IXvOut7Da+XJayx34=
33+
github.com/hashicorp/hcl/v2 v2.14.1/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
3434
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
3535
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
3636
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -74,8 +74,8 @@ github.com/yosuke-furukawa/json5 v0.1.1 h1:0F9mNwTvOuDNH243hoPqvf+dxa5QsKnZzU20u
7474
github.com/yosuke-furukawa/json5 v0.1.1/go.mod h1:sw49aWDqNdRJ6DYUtIQiaA3xyj2IL9tjeNYmX2ixwcU=
7575
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
7676
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
77-
github.com/zclconf/go-cty v1.12.1 h1:PcupnljUm9EIvbgSHQnHhUr3fO6oFmkOrvs2BAFNXXY=
78-
github.com/zclconf/go-cty v1.12.1/go.mod h1:s9IfD1LK5ccNMSWCVFCE2rJfHiZgi7JijgeWIMfhLvA=
77+
github.com/zclconf/go-cty v1.8.0 h1:s4AvqaeQzJIu3ndv4gVIhplVD0krU+bgrcLSVUnaWuA=
78+
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
7979
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
8080
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8181
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -110,6 +110,7 @@ golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuX
110110
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
111111
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
112112
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
113+
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
113114
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
114115
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
115116
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=

read.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"strconv"
55
"strings"
6+
"time"
67

78
"github.com/gookit/goutil/envutil"
89
"github.com/gookit/goutil/maputil"
@@ -327,6 +328,19 @@ func (c *Config) tryInt64(key string) (value int64, ok bool) {
327328
return
328329
}
329330

331+
// Duration get a time.Duration type value. if not found return default value
332+
func Duration(key string, defVal ...time.Duration) time.Duration { return dc.Duration(key, defVal...) }
333+
334+
// Duration get a time.Duration type value. if not found return default value
335+
func (c *Config) Duration(key string, defVal ...time.Duration) time.Duration {
336+
value, exist := c.tryInt64(key)
337+
338+
if !exist && len(defVal) > 0 {
339+
return defVal[0]
340+
}
341+
return time.Duration(value)
342+
}
343+
330344
// Float get a float64 value, if not found return default value
331345
func Float(key string, defVal ...float64) float64 { return dc.Float(key, defVal...) }
332346

read_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"fmt"
55
"testing"
6+
"time"
67

78
"github.com/gookit/goutil/testutil"
89
"github.com/gookit/goutil/testutil/assert"
@@ -276,6 +277,10 @@ func TestInt(t *testing.T) {
276277
uiv := Uint("age")
277278
is.Eq(uint(123), uiv)
278279

280+
dur := Duration("age")
281+
is.Eq(time.Duration(123), dur)
282+
is.Eq(time.Duration(340), Duration("not-exist", 340))
283+
279284
ClearAll()
280285
}
281286

0 commit comments

Comments
 (0)