Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.13
v2.0.14
32 changes: 16 additions & 16 deletions assure.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ var AssureIntPositive = func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsInt() {
if v.ToInt() < 0 {
return fmt.Errorf("value must be positive, got %d", v.ToInt())
return ErrValue{ErrWayBePositive, v.ToInt(), 0}
}
return nil
}
Expand All @@ -287,7 +287,7 @@ var AssureIntNegative = func(value interface{}) error {
v := figFlesh{value, nil}
if v.IsInt() {
if v.ToInt() > 0 {
return fmt.Errorf("value must be negative, got %d", v.ToInt())
return ErrValue{ErrWayBeNegative, v.ToInt(), 0}
}
return nil
}
Expand All @@ -304,7 +304,7 @@ var AssureIntGreaterThan = func(above int) FigValidatorFunc {
}
i := v.ToInt()
if i < above {
return fmt.Errorf("value must be below %d", i)
return ErrValue{ErrWayBeBelow, i, above}
}
return nil
}
Expand All @@ -320,7 +320,7 @@ var AssureIntLessThan = func(below int) FigValidatorFunc {
}
i := v.ToInt()
if i > below {
return fmt.Errorf("value must be below %d", i)
return ErrValue{ErrWayBeBelow, i, below}
}
return nil
}
Expand All @@ -336,7 +336,7 @@ var AssureIntInRange = func(min, max int) FigValidatorFunc {
}
i := v.ToInt()
if i < min || i > max {
return fmt.Errorf("value must be between %d and %d, got %d", min, max, i)
return ErrValue{fmt.Sprintf(ErrWayBeBetweenFmt, min, max), i, nil}
}
return nil
}
Expand All @@ -352,7 +352,7 @@ var AssureInt64GreaterThan = func(above int64) FigValidatorFunc {
}
i := v.ToInt64()
if i < above {
return fmt.Errorf("value must be below %d", i)
return ErrValue{ErrWayBeAbove, i, above}
}
return nil
}
Expand All @@ -368,7 +368,7 @@ var AssureInt64LessThan = func(below int64) FigValidatorFunc {
}
i := v.ToInt64()
if i > below {
return fmt.Errorf("value must be below %d", i)
return ErrValue{ErrWayBeBelow, i, below}
}
return nil
}
Expand All @@ -383,7 +383,7 @@ var AssureInt64Positive = func(value interface{}) error {
}
i := v.ToInt64()
if i <= 0 {
return fmt.Errorf("value must be positive, got %d", i)
return ErrValue{ErrWayBePositive, i, 0}
}
return nil
}
Expand All @@ -398,7 +398,7 @@ var AssureInt64InRange = func(min, max int64) FigValidatorFunc {
}
i := v.ToInt64()
if i < min || i > max {
return fmt.Errorf("value must be between %d and %d, got %d", min, max, i)
return ErrValue{fmt.Sprintf(ErrWayBeBetweenFmt, min, max), i, nil}
}
return nil
}
Expand All @@ -413,7 +413,7 @@ var AssureFloat64Positive = func(value interface{}) error {
}
f := v.ToFloat64()
if f <= 0 {
return fmt.Errorf("value must be positive, got %f", f)
return ErrValue{ErrWayBePositive, f, 0}
}
return nil
}
Expand All @@ -428,7 +428,7 @@ var AssureFloat64InRange = func(min, max float64) FigValidatorFunc {
}
f := v.ToFloat64()
if f < min || f > max {
return fmt.Errorf("value must be between %f and %f, got %f", min, max, f)
return ErrValue{fmt.Sprintf(ErrWayBeBetweenFmt, min, max), f, nil}
}
return nil
}
Expand All @@ -444,7 +444,7 @@ var AssureFloat64GreaterThan = func(above float64) FigValidatorFunc {
}
f := v.ToFloat64()
if f < above {
return fmt.Errorf("value must be below %f", f)
return ErrValue{ErrWayBeBelow, f, above}
}
return nil
}
Expand All @@ -460,7 +460,7 @@ var AssureFloat64LessThan = func(below float64) FigValidatorFunc {
}
f := v.ToFloat64()
if f > below {
return fmt.Errorf("value must be below %f", f)
return ErrValue{ErrWayBeBelow, f, below}
}
return nil
}
Expand All @@ -475,7 +475,7 @@ var AssureFloat64NotNaN = func(value interface{}) error {
}
n := v.ToFloat64()
if math.IsNaN(n) {
return fmt.Errorf("value must not be NaN, got %f", n)
return ErrValue{ErrWayBeNotNaN, n, nil}
}
return nil
}
Expand All @@ -490,7 +490,7 @@ var AssureDurationGreaterThan = func(above time.Duration) FigValidatorFunc {
}
t := v.ToDuration()
if t < above {
return fmt.Errorf("value must be above %v, got = %v", above, t)
return ErrValue{ErrWayBeAbove, t, above}
}
return nil
}
Expand All @@ -506,7 +506,7 @@ var AssureDurationLessThan = func(below time.Duration) FigValidatorFunc {
}
t := v.ToDuration()
if t > below {
return fmt.Errorf("value must be below %v, got = %v", below, t)
return ErrValue{ErrWayBeBelow, t, below}
}
return nil
}
Expand Down
15 changes: 11 additions & 4 deletions conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package figtree

import (
"fmt"
"math"
"strconv"
"strings"
"time"
Expand All @@ -21,6 +22,12 @@ func toInt(value interface{}) (int, error) {
case *int:
return *v, nil
case int64:
if v > math.MaxInt32 {
return 0, fmt.Errorf("max int32 %d exceeded by %d", math.MaxInt32, v-math.MaxInt32)
}
if v < math.MinInt32 {
return 0, fmt.Errorf("min int32 %d exceeded by %d", math.MinInt32, math.MinInt32-v)
}
return int(v), nil
case *int64:
return int(*v), nil
Expand All @@ -29,13 +36,13 @@ func toInt(value interface{}) (int, error) {
case float64:
return int(v), nil
case *string:
if f, err := strconv.ParseFloat(*v, 64); err == nil {
return int(f), nil
if f, err := strconv.ParseInt(*v, 10, 32); err == nil {
return toInt(f)
}
return strconv.Atoi(*v)
case string:
if f, err := strconv.ParseFloat(v, 64); err == nil {
return int(f), nil
if f, err := strconv.ParseInt(v, 10, 64); err == nil {
return toInt(f)
}
return strconv.Atoi(v)
default:
Expand Down
4 changes: 2 additions & 2 deletions conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func Test_toInt(t *testing.T) {
{
name: "String float truncated",
args: args{value: "45.6"},
want: 45,
wantErr: assert.NoError,
want: 0,
wantErr: assert.Error,
},
{
name: "Invalid string",
Expand Down
51 changes: 51 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (tree *figTree) ErrorFor(name string) error {
return fruit.Error
}

func (fig *figFruit) Unwrap() error {
return fig.Error
}

type ErrInvalidType struct {
Wanted Mutagenesis
Got any
Expand Down Expand Up @@ -46,3 +50,50 @@ func (e ErrInvalidValue) Error() string {
func (e ErrInvalidValue) Unwrap() error {
return e.Err
}

const (
ErrWayBeBelow string = "be below"
ErrWayBeAbove string = "be above"
ErrWayBeBetweenFmt string = "be between %v and %v"
ErrWayBePositive string = "be positive"
ErrWayBeNegative string = "be negative"
ErrWayBeNotNaN string = "not be NaN"
)

type ErrValue struct {
Way string
Value any
Than any
}

func (e ErrValue) Error() string {
if e.Than != nil {
return fmt.Sprintf("invalid value ; must be %s than %v ; got %v", e.Way, e.Value, e.Than)
}
return fmt.Sprintf("invalid value ; must be %s ; got %v", e.Way, e.Value)
}

type ErrLoadFailure struct {
What string
Err error
}

func (e ErrLoadFailure) Error() string {
return fmt.Sprintf("failed to load %s: %s", e.What, e.Err.Error())
}

func (e ErrLoadFailure) Unwrap() error {
return e.Err
}

type ErrValidationFailure struct {
Err error
}

func (e ErrValidationFailure) Error() string {
return fmt.Sprintf("failed to validateAll with err: %v", e.Err)
}

func (e ErrValidationFailure) Unwrap() error {
return e.Err
}
2 changes: 0 additions & 2 deletions figtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func TestParse_InvalidFlagInput(t *testing.T) {
argValue string
}{
{"IntFlag_InvalidString", "port", zeroInt, "port number", "not-a-number"},
{"BoolFlag_InvalidString", "debug", zeroBool, "debug mode", "maybe"},
{"Float64Flag_InvalidString", "ratio", zeroFloat64, "ratio value", "bad-float"},
{"DurationFlag_InvalidString", "timeout", zeroDuration, "timeout duration", "invalid-duration"},
{"ListFlag_MalformedItem", "tags", []string{"a"}, "list of tags", "item1,item2=val"},
Expand Down Expand Up @@ -221,7 +220,6 @@ func TestEmptyStringInput(t *testing.T) {
usage string
}{
{"IntFlag_EmptyString", "count", zeroInt, "count"},
{"BoolFlag_EmptyString", "enabled", zeroBool, "enabled"},
{"Float64Flag_EmptyString", "ratio", zeroFloat64, "ratio"},
{"DurationFlag_EmptyString", "interval", zeroDuration, "interval"},
{"ListFlag_EmptyString", "items", zeroList, "items"},
Expand Down
Loading
Loading