Skip to content

Commit

Permalink
[knf] Added dedicated type for duration modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Sep 14, 2023
1 parent 4aa6afa commit 53210a7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 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.1

* `[knf]` Added dedicated type for duration modifiers

### 12.76.0

* `[knf]` Added modificator support for `GetD`
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.76.0"
const VERSION = "12.76.1"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
13 changes: 6 additions & 7 deletions knf/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package knf

import (
"fmt"
"time"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -39,10 +38,10 @@ func ExampleGlobal() {
GetM("section:file-mode")

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

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

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

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

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

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

func ExampleConfig_Is() {
Expand Down
21 changes: 17 additions & 4 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Validator struct {
// PropertyValidator is default type of property validation function
type PropertyValidator func(config *Config, prop string, value any) error

// DurationMod is type for duration modificator
type DurationMod int64

// ////////////////////////////////////////////////////////////////////////////////// //

var (
Expand All @@ -48,6 +51,16 @@ var (

// ////////////////////////////////////////////////////////////////////////////////// //

var (
Millisecond = DurationMod(time.Millisecond)
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
Day = 24 * Hour
)

// ////////////////////////////////////////////////////////////////////////////////// //

// global is global configuration file
var global *Config

Expand Down Expand Up @@ -186,7 +199,7 @@ func GetM(name string, defvals ...os.FileMode) os.FileMode {
}

// GetD returns configuration values as duration
func GetD(name string, mod time.Duration, defvals ...time.Duration) time.Duration {
func GetD(name string, mod DurationMod, defvals ...time.Duration) time.Duration {
if global == nil {
if len(defvals) == 0 {
return time.Duration(0)
Expand Down Expand Up @@ -476,7 +489,7 @@ func (c *Config) GetM(name string, defvals ...os.FileMode) os.FileMode {
}

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

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

// Is checks if given property contains given value
Expand All @@ -524,7 +537,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, time.Second) == t
return c.GetD(name, 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", time.Second), check.Equals, time.Duration(0))
c.Assert(GetD("test", 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", time.Second), check.Equals, time.Duration(0))
c.Assert(config.GetD("test", 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", 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))
c.Assert(GetD("duration:test1", Second), check.Equals, time.Duration(0))
c.Assert(GetD("duration:test2", Second), check.Equals, time.Minute)
c.Assert(GetD("duration:test3", Second), check.Equals, time.Duration(0))
c.Assert(GetD("duration:test4", 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", time.Second), check.Equals, time.Duration(0))
c.Assert(nilConf.GetD("formatting:test1", 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.Second, time.Minute), check.Equals, time.Minute)
c.Assert(GetD("duration:test100", 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.Second, time.Minute), check.Equals, time.Minute)
c.Assert(GetD("duration:test100", 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.Second, time.Minute), check.Equals, time.Minute)
c.Assert(nc.GetD("duration:test100", Second, time.Minute), check.Equals, time.Minute)
c.Assert(nc.GetS("string:test6", "fail"), check.Equals, "fail")
}

Expand Down

0 comments on commit 53210a7

Please sign in to comment.