From a3d62151ddfc408ab338846d9638b4e078a35758 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 11 Dec 2024 16:08:44 +0300 Subject: [PATCH 1/3] [options] Fixed return type of 'Argument.Uint' --- CHANGELOG.md | 5 +++++ options/arguments.go | 13 +++++++++++-- options/options_test.go | 14 +++++++++++--- version.go | 2 +- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2a95f36..c8302ca0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## Changelog +### [13.15.0](https://kaos.sh/ek/13.15.1) + +- `[options]` Added method `Argument.Uint64` +- `[options]` Return type of method `Argument.Uint` set from `uint64` to `uint` + ### [13.15.0](https://kaos.sh/ek/13.15.0) - `[color]` Added `fmt.GoStringer` support for structs diff --git a/options/arguments.go b/options/arguments.go index e0a34f73..39498597 100644 --- a/options/arguments.go +++ b/options/arguments.go @@ -164,9 +164,12 @@ func (a Argument) Is(value any) bool { case int64: v, err := a.Int64() return v == t && err == nil - case uint64: + case uint: v, err := a.Uint() return v == t && err == nil + case uint64: + v, err := a.Uint64() + return v == t && err == nil case float64: v, err := a.Float() return v == t && err == nil @@ -189,7 +192,13 @@ func (a Argument) Int64() (int64, error) { } // Uint converts argument to uint -func (a Argument) Uint() (uint64, error) { +func (a Argument) Uint() (uint, error) { + u, err := strconv.ParseUint(string(a), 10, 64) + return uint(u), err +} + +// Uint converts argument to uint64 +func (a Argument) Uint64() (uint64, error) { return strconv.ParseUint(string(a), 10, 64) } diff --git a/options/options_test.go b/options/options_test.go index 1d9ae661..a36d600b 100644 --- a/options/options_test.go +++ b/options/options_test.go @@ -628,7 +628,7 @@ func (s *OptUtilSuite) TestArgumentsConversion(c *C) { c.Assert(a.Get(1).Is(12), Equals, false) c.Assert(a.Get(1).Is(int64(6)), Equals, true) c.Assert(a.Get(1).Is(int64(12)), Equals, false) - c.Assert(a.Get(1).Is(uint64(6)), Equals, true) + c.Assert(a.Get(1).Is(uint(6)), Equals, true) c.Assert(a.Get(1).Is(uint64(12)), Equals, false) c.Assert(a.Get(2).Is(2.67), Equals, true) c.Assert(a.Get(2).Is(3.14), Equals, false) @@ -656,10 +656,18 @@ func (s *OptUtilSuite) TestArgumentsConversion(c *C) { vu, err := a.Get(0).Uint() c.Assert(err, NotNil) c.Assert(err, ErrorMatches, `strconv.ParseUint: parsing "test": invalid syntax`) - c.Assert(vu, Equals, uint64(0)) + c.Assert(vu, Equals, uint(0)) vu, err = a.Get(1).Uint() c.Assert(err, IsNil) - c.Assert(vu, Equals, uint64(6)) + c.Assert(vu, Equals, uint(6)) + + vu64, err := a.Get(0).Uint64() + c.Assert(err, NotNil) + c.Assert(err, ErrorMatches, `strconv.ParseUint: parsing "test": invalid syntax`) + c.Assert(vu64, Equals, uint64(0)) + vu64, err = a.Get(1).Uint64() + c.Assert(err, IsNil) + c.Assert(vu64, Equals, uint64(6)) fv, err := a.Get(0).Float() c.Assert(err, NotNil) diff --git a/version.go b/version.go index 25ee0447..bba105b8 100644 --- a/version.go +++ b/version.go @@ -8,4 +8,4 @@ package ek // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "13.15.0" +const VERSION = "13.15.1" From 737f2bfb1cea8f6938d5d2249990be6c8c3e5ac5 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 11 Dec 2024 18:27:41 +0300 Subject: [PATCH 2/3] [options] Add check for MaxUint --- options/arguments.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/options/arguments.go b/options/arguments.go index 39498597..15fa17ad 100644 --- a/options/arguments.go +++ b/options/arguments.go @@ -9,6 +9,7 @@ package options import ( "fmt" + "math" "strconv" "strings" @@ -194,6 +195,11 @@ func (a Argument) Int64() (int64, error) { // Uint converts argument to uint func (a Argument) Uint() (uint, error) { u, err := strconv.ParseUint(string(a), 10, 64) + + if u > math.MaxUint { + return math.MaxUint, fmt.Errorf("Argument value exceeds maximum uint size") + } + return uint(u), err } From a38a569eb84ebf96a3875c07a4c888825602338f Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 12 Dec 2024 00:13:18 +0300 Subject: [PATCH 3/3] [options] Add check for MaxUint --- options/arguments.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/options/arguments.go b/options/arguments.go index 15fa17ad..626a2cea 100644 --- a/options/arguments.go +++ b/options/arguments.go @@ -13,6 +13,7 @@ import ( "strconv" "strings" + "github.com/essentialkaos/ek/v13/mathutil" "github.com/essentialkaos/ek/v13/path" ) @@ -195,12 +196,7 @@ func (a Argument) Int64() (int64, error) { // Uint converts argument to uint func (a Argument) Uint() (uint, error) { u, err := strconv.ParseUint(string(a), 10, 64) - - if u > math.MaxUint { - return math.MaxUint, fmt.Errorf("Argument value exceeds maximum uint size") - } - - return uint(u), err + return uint(mathutil.Min(u, math.MaxUint)), err } // Uint converts argument to uint64