Skip to content

Commit

Permalink
[knf] Use constants instead of variables for duration modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 13, 2024
1 parent 43aee19 commit af8b0a4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 46 deletions.
8 changes: 4 additions & 4 deletions knf/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func ExampleGlobal() {
GetM("section:file-mode")

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

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

// Read time duration
GetTD("section:time-duration")
Expand Down Expand Up @@ -230,7 +230,7 @@ func ExampleGetD() {
return
}

fmt.Printf("Value from config: %v\n", GetD("user:timeout", Minute))
fmt.Printf("Value from config: %v\n", GetD("user:timeout", MINUTE))
}

func ExampleGetTD() {
Expand Down Expand Up @@ -634,7 +634,7 @@ func ExampleConfig_GetD() {
return
}

fmt.Printf("Value from config: %v\n", cfg.GetD("user:timeout", Minute))
fmt.Printf("Value from config: %v\n", cfg.GetD("user:timeout", MINUTE))

// Output:
// Value from config: 3m0s
Expand Down
24 changes: 12 additions & 12 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ import (

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

const (
MILLISECOND = DurationMod(time.Millisecond)
SECOND = 1000 * MILLISECOND
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
WEEK = 7 * DAY
)

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

// IConfig is knf like configuration
type IConfig interface {
// GetS returns configuration value as string
Expand Down Expand Up @@ -102,17 +113,6 @@ var (

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

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

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

// global is global configuration file
var global *Config

Expand Down Expand Up @@ -715,7 +715,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, Second) == t
return c.GetD(name, SECOND) == t
case time.Time:
return c.GetTS(name).Unix() == t.Unix()
case *time.Location:
Expand Down
20 changes: 10 additions & 10 deletions knf/knf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (s *KNFSuite) TestErrors(c *check.C) {
c.Assert(GetF("test:test"), check.Equals, 0.0)
c.Assert(GetB("test:test"), check.Equals, false)
c.Assert(GetM("test:test"), check.Equals, os.FileMode(0))
c.Assert(GetD("test:test", Second), check.Equals, time.Duration(0))
c.Assert(GetD("test:test", SECOND), check.Equals, time.Duration(0))
c.Assert(GetTD("test:test"), check.Equals, time.Duration(0))
c.Assert(GetTS("test:test").IsZero(), check.Equals, true)
c.Assert(GetTZ("test:test"), check.IsNil)
Expand All @@ -236,7 +236,7 @@ func (s *KNFSuite) TestErrors(c *check.C) {
c.Assert(config.GetF("test:test"), check.Equals, 0.0)
c.Assert(config.GetB("test:test"), check.Equals, false)
c.Assert(config.GetM("test:test"), check.Equals, os.FileMode(0))
c.Assert(config.GetD("test:test", Second), check.Equals, time.Duration(0))
c.Assert(config.GetD("test:test", SECOND), check.Equals, time.Duration(0))
c.Assert(config.GetTD("test:test"), check.Equals, time.Duration(0))
c.Assert(config.GetTS("test:test").IsZero(), check.Equals, true)
c.Assert(config.GetTZ("test:test"), check.IsNil)
Expand Down Expand Up @@ -466,10 +466,10 @@ func (s *KNFSuite) TestDuration(c *check.C) {
c.Assert(global, check.NotNil)
c.Assert(err, check.IsNil)

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))
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) TestTimeDuration(c *check.C) {
Expand Down Expand Up @@ -582,7 +582,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", Second), check.Equals, time.Duration(0))
c.Assert(nilConf.GetD("formatting:test1", SECOND), check.Equals, time.Duration(0))
c.Assert(nilConf.GetTD("formatting:test1"), check.Equals, time.Duration(0))
c.Assert(nilConf.GetTS("formatting:test1").IsZero(), check.Equals, true)
c.Assert(nilConf.GetTZ("formatting:test1"), check.IsNil)
Expand Down Expand Up @@ -619,7 +619,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", Second, time.Minute), check.Equals, time.Minute)
c.Assert(GetD("duration:test100", SECOND, time.Minute), check.Equals, time.Minute)
c.Assert(GetTD("duration:test100", time.Minute), check.Equals, time.Minute)
c.Assert(GetTS("duration:test100", time.Now()).IsZero(), check.Equals, false)
c.Assert(GetTZ("duration:test100", l), check.Equals, l)
Expand All @@ -639,7 +639,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", Second, time.Minute), check.Equals, time.Minute)
c.Assert(GetD("duration:test100", SECOND, time.Minute), check.Equals, time.Minute)
c.Assert(GetTD("duration:test100", time.Minute), check.Equals, time.Minute)
c.Assert(GetTS("duration:test100", time.Now()).IsZero(), check.Equals, false)
c.Assert(GetTZ("duration:test100", l), check.Equals, l)
Expand All @@ -656,7 +656,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", Second, time.Minute), check.Equals, time.Minute)
c.Assert(nc.GetD("duration:test100", SECOND, time.Minute), check.Equals, time.Minute)
c.Assert(nc.GetTD("duration:test100", time.Minute), check.Equals, time.Minute)
c.Assert(nc.GetTS("duration:test100", time.Now()).IsZero(), check.Equals, false)
c.Assert(nc.GetTZ("duration:test100", l), check.Equals, l)
Expand Down
6 changes: 3 additions & 3 deletions knf/united/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func ExampleCombine() {
GetM("section:file-mode")

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

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

// Read time duration
GetTD("section:time-duration")
Expand Down Expand Up @@ -294,7 +294,7 @@ func ExampleGetD() {
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetD("user:timeout", Minute))
fmt.Printf("Value from config: %v\n", GetD("user:timeout", MINUTE))
}

func ExampleGetTD() {
Expand Down
22 changes: 11 additions & 11 deletions knf/united/united.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ import (

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

const (
MILLISECOND = knf.MILLISECOND
SECOND = knf.SECOND
MINUTE = knf.MINUTE
HOUR = knf.HOUR
DAY = knf.DAY
WEEK = knf.WEEK
)

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

// Mapping contains mapping [knf property] → [option] → [envvar]
type Mapping struct {
Property string // Property from KNF configuration file
Expand All @@ -44,17 +55,6 @@ type united struct {

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

var (
Millisecond = knf.Millisecond
Second = knf.Second
Minute = knf.Minute
Hour = knf.Hour
Day = knf.Day
Week = knf.Week
)

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

var global *united

var optionHasFunc = options.Has
Expand Down
10 changes: 5 additions & 5 deletions knf/united/united_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func (s *UnitedSuite) TestKNFOnly(c *C) {
c.Assert(GetB("test:boolean", true), Equals, true)
c.Assert(GetM("test:file-mode"), Equals, os.FileMode(0))
c.Assert(GetM("test:file-mode", 0600), Equals, os.FileMode(0600))
c.Assert(GetD("test:duration", knf.Minute), Equals, time.Duration(0))
c.Assert(GetD("test:duration", knf.Minute, time.Minute), Equals, time.Minute)
c.Assert(GetD("test:duration", MINUTE), Equals, time.Duration(0))
c.Assert(GetD("test:duration", MINUTE, time.Minute), Equals, time.Minute)
c.Assert(GetTD("test:time-duration"), Equals, time.Duration(0))
c.Assert(GetTD("test:time-duration", time.Minute), Equals, time.Minute)
c.Assert(GetTS("test:timestamp").IsZero(), Equals, true)
Expand Down Expand Up @@ -124,7 +124,7 @@ func (s *UnitedSuite) TestKNFOnly(c *C) {
c.Assert(GetF("test:float"), Equals, 234.5)
c.Assert(GetB("test:boolean"), Equals, true)
c.Assert(GetM("test:file-mode"), Equals, os.FileMode(0644))
c.Assert(GetD("test:duration", knf.Minute), Equals, 24*time.Minute)
c.Assert(GetD("test:duration", MINUTE), Equals, 24*time.Minute)
c.Assert(GetTD("test:time-duration"), Equals, 5*time.Minute)
c.Assert(GetTS("test:timestamp").Unix(), Equals, int64(1709629048))
c.Assert(GetTZ("test:timezone").String(), Equals, "Europe/Zurich")
Expand Down Expand Up @@ -194,7 +194,7 @@ func (s *UnitedSuite) TestWithOptions(c *C) {
c.Assert(GetF("test:float"), Equals, 567.8)
c.Assert(GetB("test:boolean"), Equals, false)
c.Assert(GetM("test:file-mode"), Equals, os.FileMode(0640))
c.Assert(GetD("test:duration", knf.Hour), Equals, 35*time.Hour)
c.Assert(GetD("test:duration", HOUR), Equals, 35*time.Hour)
c.Assert(GetTD("test:time-duration"), Equals, 3*time.Hour)
c.Assert(GetTS("test:timestamp").Unix(), Equals, int64(1704067200))
c.Assert(GetTZ("test:timezone").String(), Equals, "Europe/Prague")
Expand Down Expand Up @@ -238,7 +238,7 @@ func (s *UnitedSuite) TestWithEnv(c *C) {
c.Assert(GetF("test:float"), Equals, 678.9)
c.Assert(GetB("test:boolean"), Equals, false)
c.Assert(GetM("test:file-mode"), Equals, os.FileMode(0600))
c.Assert(GetD("test:duration", knf.Hour), Equals, 17*time.Hour)
c.Assert(GetD("test:duration", HOUR), Equals, 17*time.Hour)
c.Assert(GetTD("test:time-duration"), Equals, 19*time.Minute)
c.Assert(GetTS("test:timestamp").Unix(), Equals, int64(1591014600))
c.Assert(GetTZ("test:timezone").String(), Equals, "Europe/Berlin")
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ package ek
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "13.6.1"
const VERSION = "13.7.0"

0 comments on commit af8b0a4

Please sign in to comment.