From e85ab2ae72a58db328f942cde4655209117366f8 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 5 Mar 2024 01:55:17 +0300 Subject: [PATCH] [knf] Code refactoring --- .scripts/packages.list | 1 + CHANGELOG.md | 1 + knf/knf.go | 87 ++------------------ knf/value/value.go | 178 ++++++++++++++++++++++++++++++++++++++++ knf/value/value_test.go | 125 ++++++++++++++++++++++++++++ 5 files changed, 312 insertions(+), 80 deletions(-) create mode 100644 knf/value/value.go create mode 100644 knf/value/value_test.go diff --git a/.scripts/packages.list b/.scripts/packages.list index 99700359..acb89ee6 100644 --- a/.scripts/packages.list +++ b/.scripts/packages.list @@ -25,6 +25,7 @@ * + knf/validators/network * + knf/validators/regexp * + knf/validators/system +* + knf/value * + lock * + log * + lscolors diff --git a/CHANGELOG.md b/CHANGELOG.md index 87f084f1..d7ad3a69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * `[options]` Fixed panic when parsing unsupported option with value passed with equal sign (`=`) * `[options]` Code refactoring +* `[knf]` Code refactoring ### 12.102.0 diff --git a/knf/knf.go b/knf/knf.go index 8764da00..a0b840f1 100644 --- a/knf/knf.go +++ b/knf/knf.go @@ -13,10 +13,11 @@ import ( "errors" "os" "path" - "strconv" "strings" "sync" "time" + + "github.com/essentialkaos/ek/v12/knf/value" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -405,32 +406,7 @@ func (c *Config) GetI64(name string, defvals ...int64) int64 { val := c.data[strings.ToLower(name)] c.mx.RUnlock() - if val == "" { - if len(defvals) == 0 { - return 0 - } - - return defvals[0] - } - - // HEX Parsing - if len(val) >= 3 && val[0:2] == "0x" { - valHex, err := strconv.ParseInt(val[2:], 16, 0) - - if err != nil { - return 0 - } - - return valHex - } - - valInt, err := strconv.ParseInt(val, 10, 0) - - if err != nil { - return 0 - } - - return valInt + return value.ParseInt64(val, defvals...) } // GetI returns configuration value as int @@ -474,21 +450,7 @@ func (c *Config) GetF(name string, defvals ...float64) float64 { val := c.data[strings.ToLower(name)] c.mx.RUnlock() - if val == "" { - if len(defvals) == 0 { - return 0.0 - } - - return defvals[0] - } - - valFl, err := strconv.ParseFloat(val, 64) - - if err != nil { - return 0.0 - } - - return valFl + return value.ParseFloat(val, defvals...) } // GetB returns configuration value as boolean @@ -505,20 +467,7 @@ func (c *Config) GetB(name string, defvals ...bool) bool { val := c.data[strings.ToLower(name)] c.mx.RUnlock() - if val == "" { - if len(defvals) == 0 { - return false - } - - return defvals[0] - } - - switch val { - case "", "0", "false", "no": - return false - default: - return true - } + return value.ParseBool(val, defvals...) } // GetM returns configuration value as file mode @@ -535,21 +484,7 @@ func (c *Config) GetM(name string, defvals ...os.FileMode) os.FileMode { val := c.data[strings.ToLower(name)] c.mx.RUnlock() - if val == "" { - if len(defvals) == 0 { - return os.FileMode(0) - } - - return defvals[0] - } - - valM, err := strconv.ParseUint(val, 8, 32) - - if err != nil { - return 0 - } - - return os.FileMode(valM) + return value.ParseMode(val, defvals...) } // GetD returns configuration value as duration @@ -566,15 +501,7 @@ func (c *Config) GetD(name string, mod DurationMod, defvals ...time.Duration) ti val := c.data[strings.ToLower(name)] c.mx.RUnlock() - if val == "" { - if len(defvals) == 0 { - return time.Duration(0) - } - - return defvals[0] - } - - return time.Duration(c.GetI64(name)) * time.Duration(mod) + return value.ParseDuration(val, time.Duration(mod), defvals...) } // Is checks if given property contains given value diff --git a/knf/value/value.go b/knf/value/value.go new file mode 100644 index 00000000..1b8ed356 --- /dev/null +++ b/knf/value/value.go @@ -0,0 +1,178 @@ +package value + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2024 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +import ( + "os" + "strconv" + "strings" + "time" +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +// ParseInt64 parses value as Int64 +func ParseInt64(v string, defvals ...int64) int64 { + if v == "" { + if len(defvals) == 0 { + return 0 + } + + return defvals[0] + } + + // HEX Parsing + if len(v) >= 3 && v[0:2] == "0x" { + vHex, err := strconv.ParseInt(v[2:], 16, 0) + + if err != nil { + return 0 + } + + return vHex + } + + vInt, err := strconv.ParseInt(v, 10, 0) + + if err != nil { + return 0 + } + + return vInt +} + +// ParseInt parses value as Int +func ParseInt(v string, defvals ...int) int { + if len(defvals) != 0 { + return int(ParseInt64(v, int64(defvals[0]))) + } + + return int(ParseInt64(v)) +} + +// ParseUint parses value as Uint +func ParseUint(v string, defvals ...uint) uint { + if len(defvals) != 0 { + return uint(ParseInt64(v, int64(defvals[0]))) + } + + return uint(ParseInt64(v)) +} + +// ParseUint64 parses value as Uint64 +func ParseUint64(v string, defvals ...uint64) uint64 { + if len(defvals) != 0 { + return uint64(ParseInt64(v, int64(defvals[0]))) + } + + return uint64(ParseInt64(v)) +} + +// ParseFloat parses value as float +func ParseFloat(v string, defvals ...float64) float64 { + if v == "" { + if len(defvals) == 0 { + return 0.0 + } + + return defvals[0] + } + + vF, err := strconv.ParseFloat(v, 64) + + if err != nil { + return 0.0 + } + + return vF +} + +// ParseFloat parses value as boolean +func ParseBool(v string, defvals ...bool) bool { + if v == "" { + if len(defvals) == 0 { + return false + } + + return defvals[0] + } + + switch strings.ToLower(v) { + case "", "0", "false", "no": + return false + default: + return true + } +} + +// ParseMode parses value as file mode +func ParseMode(v string, defvals ...os.FileMode) os.FileMode { + if v == "" { + if len(defvals) == 0 { + return os.FileMode(0) + } + + return defvals[0] + } + + vM, err := strconv.ParseUint(v, 8, 32) + + if err != nil { + return 0 + } + + return os.FileMode(vM) +} + +// ParseDuration parses value as duration +func ParseDuration(v string, mod time.Duration, defvals ...time.Duration) time.Duration { + if v == "" { + if len(defvals) == 0 { + return time.Duration(0) + } + + return defvals[0] + } + + return time.Duration(ParseInt64(v)) * mod +} + +// ParseTime parses value as time duration +func ParseTime(v string, defvals ...time.Duration) time.Duration { + if v == "" { + if len(defvals) == 0 { + return time.Duration(0) + } + + return defvals[0] + } + + v = strings.ToLower(v) + m := 1 + + switch v[len(v)-1:] { + case "s": + v, m = v[:len(v)-1], 1 + case "m": + v, m = v[:len(v)-1], 60 + case "h": + v, m = v[:len(v)-1], 3600 + case "d": + v, m = v[:len(v)-1], 24*3600 + case "w": + v, m = v[:len(v)-1], 7*24*3600 + } + + i, err := strconv.Atoi(v) + + if err != nil { + return time.Duration(0) + } + + return time.Duration(i*m) * time.Second +} diff --git a/knf/value/value_test.go b/knf/value/value_test.go new file mode 100644 index 00000000..64a3e392 --- /dev/null +++ b/knf/value/value_test.go @@ -0,0 +1,125 @@ +package value + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2024 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +import ( + "os" + "testing" + "time" + + . "github.com/essentialkaos/check" +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +func Test(t *testing.T) { TestingT(t) } + +type ValuesSuite struct{} + +// ////////////////////////////////////////////////////////////////////////////////// // + +var _ = Suite(&ValuesSuite{}) + +// ////////////////////////////////////////////////////////////////////////////////// // + +func (s *ValuesSuite) TestParseInt64(c *C) { + c.Assert(ParseInt64(""), Equals, int64(0)) + c.Assert(ParseInt64("", 234), Equals, int64(234)) + + c.Assert(ParseInt64("123"), Equals, int64(123)) + c.Assert(ParseInt64("0xFF"), Equals, int64(255)) + + c.Assert(ParseInt64("ABCD"), Equals, int64(0)) + c.Assert(ParseInt64("0xZZ"), Equals, int64(0)) +} + +func (s *ValuesSuite) TestParseInt(c *C) { + c.Assert(ParseInt(""), Equals, 0) + c.Assert(ParseInt("", 234), Equals, 234) + + c.Assert(ParseInt("123"), Equals, 123) + c.Assert(ParseInt("0xFF"), Equals, 255) + + c.Assert(ParseInt("ABCD"), Equals, 0) + c.Assert(ParseInt("0xZZ"), Equals, 0) +} + +func (s *ValuesSuite) TestParseUint(c *C) { + c.Assert(ParseUint(""), Equals, uint(0)) + c.Assert(ParseUint("", 234), Equals, uint(234)) + + c.Assert(ParseUint("123"), Equals, uint(123)) + c.Assert(ParseUint("0xFF"), Equals, uint(255)) + + c.Assert(ParseUint("ABCD"), Equals, uint(0)) + c.Assert(ParseUint("0xZZ"), Equals, uint(0)) +} + +func (s *ValuesSuite) TestParseUint64(c *C) { + c.Assert(ParseUint64(""), Equals, uint64(0)) + c.Assert(ParseUint64("", 234), Equals, uint64(234)) + + c.Assert(ParseUint64("123"), Equals, uint64(123)) + c.Assert(ParseUint64("0xFF"), Equals, uint64(255)) + + c.Assert(ParseUint64("ABCD"), Equals, uint64(0)) + c.Assert(ParseUint64("0xZZ"), Equals, uint64(0)) +} + +func (s *ValuesSuite) TestParseFloat(c *C) { + c.Assert(ParseFloat(""), Equals, 0.0) + c.Assert(ParseFloat("", 234.0), Equals, 234.0) + + c.Assert(ParseFloat("123"), Equals, 123.0) + + c.Assert(ParseFloat("ABCD"), Equals, 0.0) +} + +func (s *ValuesSuite) TestParseBool(c *C) { + c.Assert(ParseBool(""), Equals, false) + c.Assert(ParseBool("", true), Equals, true) + + c.Assert(ParseBool("0"), Equals, false) + c.Assert(ParseBool("No"), Equals, false) + c.Assert(ParseBool("False"), Equals, false) + + c.Assert(ParseBool("true"), Equals, true) + c.Assert(ParseBool("abcd"), Equals, true) +} + +func (s *ValuesSuite) TestParseMode(c *C) { + c.Assert(ParseMode(""), Equals, os.FileMode(0)) + c.Assert(ParseMode("", 0600), Equals, os.FileMode(0600)) + + c.Assert(ParseMode("0600"), Equals, os.FileMode(0600)) + c.Assert(ParseMode("600"), Equals, os.FileMode(0600)) + + c.Assert(ParseMode("ABCD"), Equals, os.FileMode(0)) +} + +func (s *ValuesSuite) TestParseDuration(c *C) { + c.Assert(ParseDuration("", time.Minute), Equals, time.Duration(0)) + c.Assert(ParseDuration("", time.Minute, time.Hour), Equals, time.Hour) + + c.Assert(ParseDuration("3", time.Minute, time.Hour), Equals, 3*time.Minute) + + c.Assert(ParseDuration("ABCD", time.Minute), Equals, time.Duration(0)) +} + +func (s *ValuesSuite) TestParseTime(c *C) { + c.Assert(ParseTime(""), Equals, time.Duration(0)) + c.Assert(ParseTime("", time.Hour), Equals, time.Hour) + + c.Assert(ParseTime("7s"), Equals, 7*time.Second) + c.Assert(ParseTime("6m"), Equals, 6*time.Minute) + c.Assert(ParseTime("3h"), Equals, 3*time.Hour) + c.Assert(ParseTime("2d"), Equals, 48*time.Hour) + c.Assert(ParseTime("3w"), Equals, 3*7*24*time.Hour) + + c.Assert(ParseTime("ABCD"), Equals, time.Duration(0)) +}