diff --git a/CHANGELOG.md b/CHANGELOG.md index 60ffa8d8..1cd96341 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### 12.76.0 + +* `[knf]` Add modificator support for `GetD` + ### 12.75.1 * `[terminal]` Improved `AlwaysYes` flag handling diff --git a/knf/example_test.go b/knf/example_test.go index 8d5ab8b7..5a7262a1 100644 --- a/knf/example_test.go +++ b/knf/example_test.go @@ -9,6 +9,7 @@ package knf import ( "fmt" + "time" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -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") { @@ -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() { @@ -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() { diff --git a/knf/knf.go b/knf/knf.go index 713e9887..8372a1ff 100644 --- a/knf/knf.go +++ b/knf/knf.go @@ -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) @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/knf/knf_test.go b/knf/knf_test.go index a8880699..2954b336 100644 --- a/knf/knf_test.go +++ b/knf/knf_test.go @@ -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) @@ -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) @@ -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) { @@ -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) @@ -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) @@ -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 @@ -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") }