Skip to content

Commit

Permalink
[knf] Add modificator support for 'GetD'
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Sep 13, 2023
1 parent 52086c7 commit 12a0a89
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 12.76.0

* `[knf]` Add modificator support for `GetD`

### 12.75.1

* `[terminal]` Improved `AlwaysYes` flag handling
Expand Down
12 changes: 8 additions & 4 deletions knf/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package knf

import (
"fmt"
"time"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -37,8 +38,11 @@ func ExampleGlobal() {
// Read file mode value
GetM("section:file-mode")

// Read duration in seconds
GetD("section:duration")
// Read duration as seconds
GetD("section:duration", time.Second)

// Read duration as minutes
GetD("section:duration", time.Minute)

// Check section
if HasSection("section") {
Expand Down Expand Up @@ -174,7 +178,7 @@ func ExampleGetD() {
return
}

fmt.Printf("Duration value from config: %v\n", GetD("section:duration"))
fmt.Printf("Duration value from config: %v\n", GetD("section:duration", time.Second))
}

func ExampleIs() {
Expand Down Expand Up @@ -344,7 +348,7 @@ func ExampleConfig_GetD() {
return
}

fmt.Printf("Duration value from config: %v\n", cfg.GetD("section:duration"))
fmt.Printf("Duration value from config: %v\n", cfg.GetD("section:duration", time.Second))
}

func ExampleConfig_Is() {
Expand Down
10 changes: 5 additions & 5 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func GetM(name string, defvals ...os.FileMode) os.FileMode {
}

// GetD returns configuration values as duration
func GetD(name string, defvals ...time.Duration) time.Duration {
func GetD(name string, mod time.Duration, defvals ...time.Duration) time.Duration {
if global == nil {
if len(defvals) == 0 {
return time.Duration(0)
Expand All @@ -195,7 +195,7 @@ func GetD(name string, defvals ...time.Duration) time.Duration {
return defvals[0]
}

return global.GetD(name, defvals...)
return global.GetD(name, mod, defvals...)
}

// Is checks if given property contains given value
Expand Down Expand Up @@ -476,7 +476,7 @@ func (c *Config) GetM(name string, defvals ...os.FileMode) os.FileMode {
}

// GetD returns configuration value as duration
func (c *Config) GetD(name string, defvals ...time.Duration) time.Duration {
func (c *Config) GetD(name string, mod time.Duration, defvals ...time.Duration) time.Duration {
if c == nil || c.mx == nil {
if len(defvals) == 0 {
return time.Duration(0)
Expand All @@ -497,7 +497,7 @@ func (c *Config) GetD(name string, defvals ...time.Duration) time.Duration {
return defvals[0]
}

return time.Duration(c.GetI64(name)) * time.Second
return time.Duration(c.GetI64(name)) * mod
}

// Is checks if given property contains given value
Expand All @@ -524,7 +524,7 @@ func (c *Config) Is(name string, value any) bool {
case os.FileMode:
return c.GetM(name) == t
case time.Duration:
return c.GetD(name) == t
return c.GetD(name, time.Second) == t
}

return false
Expand Down
20 changes: 10 additions & 10 deletions knf/knf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (s *KNFSuite) TestErrors(c *check.C) {
c.Assert(GetF("test"), check.Equals, 0.0)
c.Assert(GetB("test"), check.Equals, false)
c.Assert(GetM("test"), check.Equals, os.FileMode(0))
c.Assert(GetD("test"), check.Equals, time.Duration(0))
c.Assert(GetD("test", time.Second), check.Equals, time.Duration(0))
c.Assert(Is("test", ""), check.Equals, false)
c.Assert(HasSection("test"), check.Equals, false)
c.Assert(HasProp("test"), check.Equals, false)
Expand All @@ -199,7 +199,7 @@ func (s *KNFSuite) TestErrors(c *check.C) {
c.Assert(config.GetF("test"), check.Equals, 0.0)
c.Assert(config.GetB("test"), check.Equals, false)
c.Assert(config.GetM("test"), check.Equals, os.FileMode(0))
c.Assert(config.GetD("test"), check.Equals, time.Duration(0))
c.Assert(config.GetD("test", time.Second), check.Equals, time.Duration(0))
c.Assert(config.Is("test", ""), check.Equals, true)
c.Assert(config.HasSection("test"), check.Equals, false)
c.Assert(config.HasProp("test"), check.Equals, false)
Expand Down Expand Up @@ -369,10 +369,10 @@ func (s *KNFSuite) TestDuration(c *check.C) {
c.Assert(global, check.NotNil)
c.Assert(err, check.IsNil)

c.Assert(GetD("duration:test1"), check.Equals, time.Duration(0))
c.Assert(GetD("duration:test2"), check.Equals, time.Minute)
c.Assert(GetD("duration:test3"), check.Equals, time.Duration(0))
c.Assert(GetD("duration:test4"), check.Equals, time.Duration(0))
c.Assert(GetD("duration:test1", time.Second), check.Equals, time.Duration(0))
c.Assert(GetD("duration:test2", time.Second), check.Equals, time.Minute)
c.Assert(GetD("duration:test3", time.Second), check.Equals, time.Duration(0))
c.Assert(GetD("duration:test4", time.Second), check.Equals, time.Duration(0))
}

func (s *KNFSuite) TestIs(c *check.C) {
Expand Down Expand Up @@ -429,7 +429,7 @@ func (s *KNFSuite) TestNil(c *check.C) {
c.Assert(nilConf.GetF("formatting:test1"), check.Equals, 0.0)
c.Assert(nilConf.GetB("formatting:test1"), check.Equals, false)
c.Assert(nilConf.GetM("formatting:test1"), check.Equals, os.FileMode(0))
c.Assert(nilConf.GetD("formatting:test1"), check.Equals, time.Duration(0))
c.Assert(nilConf.GetD("formatting:test1", time.Second), check.Equals, time.Duration(0))
c.Assert(nilConf.Is("formatting:test1", ""), check.Equals, false)
c.Assert(nilConf.HasSection("formatting"), check.Equals, false)
c.Assert(nilConf.HasProp("formatting:test1"), check.Equals, false)
Expand Down Expand Up @@ -459,7 +459,7 @@ func (s *KNFSuite) TestDefault(c *check.C) {
c.Assert(GetU64("integer:test100", 9999), check.Equals, uint64(9999))
c.Assert(GetF("integer:test100", 123.45), check.Equals, 123.45)
c.Assert(GetM("file-mode:test100", 0755), check.Equals, os.FileMode(0755))
c.Assert(GetD("duration:test100", time.Minute), check.Equals, time.Minute)
c.Assert(GetD("duration:test100", time.Second, time.Minute), check.Equals, time.Minute)
c.Assert(GetS("string:test6", "fail"), check.Equals, "fail")

err := Global(s.ConfigPath)
Expand All @@ -475,7 +475,7 @@ func (s *KNFSuite) TestDefault(c *check.C) {
c.Assert(GetU64("integer:test100", 9999), check.Equals, uint64(9999))
c.Assert(GetF("integer:test100", 123.45), check.Equals, 123.45)
c.Assert(GetM("file-mode:test100", 0755), check.Equals, os.FileMode(0755))
c.Assert(GetD("duration:test100", time.Minute), check.Equals, time.Minute)
c.Assert(GetD("duration:test100", time.Second, time.Minute), check.Equals, time.Minute)
c.Assert(GetS("string:test6", "fail"), check.Equals, "fail")

var nc *Config
Expand All @@ -488,7 +488,7 @@ func (s *KNFSuite) TestDefault(c *check.C) {
c.Assert(nc.GetU64("integer:test100", 9999), check.Equals, uint64(9999))
c.Assert(nc.GetF("integer:test100", 123.45), check.Equals, 123.45)
c.Assert(nc.GetM("file-mode:test100", 0755), check.Equals, os.FileMode(0755))
c.Assert(nc.GetD("duration:test100", time.Minute), check.Equals, time.Minute)
c.Assert(nc.GetD("duration:test100", time.Second, time.Minute), check.Equals, time.Minute)
c.Assert(nc.GetS("string:test6", "fail"), check.Equals, "fail")
}

Expand Down

0 comments on commit 12a0a89

Please sign in to comment.