Skip to content

Commit 6ed6b52

Browse files
feat(valueparser): Use yaerror
1 parent 7985830 commit 6ed6b52

12 files changed

Lines changed: 123 additions & 33 deletions

File tree

valueparser/array_parser.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package valueparser
33
import (
44
"fmt"
55
"strings"
6+
7+
"github.com/YaCodeDev/GoYaCodeDevUtils/yaerrors"
68
)
79

810
// ParseArray splits a string by 'separator' and parses each part into T.
@@ -19,7 +21,7 @@ import (
1921
func ParseArray[T ParsableType](
2022
str string,
2123
separator *string,
22-
) ([]T, error) {
24+
) ([]T, yaerrors.Error) {
2325
if str == "" {
2426
return []T{}, nil
2527
}
@@ -31,7 +33,7 @@ func ParseArray[T ParsableType](
3133

3234
var (
3335
parsed T
34-
err error
36+
err yaerrors.Error
3537
)
3638

3739
parts := strings.Split(str, *separator)
@@ -42,7 +44,12 @@ func ParseArray[T ParsableType](
4244
if parsed, err = ParseValue[T](trimmed); err == nil {
4345
result = append(result, parsed)
4446
} else {
45-
return nil, fmt.Errorf("failed to parse part '%s': %w", trimmed, err)
47+
return nil, err.Wrap(
48+
fmt.Sprintf(
49+
"parse array: failed to parse part '%s'",
50+
trimmed,
51+
),
52+
)
4653
}
4754
}
4855

valueparser/map_parser.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package valueparser
22

33
import (
44
"fmt"
5+
"net/http"
56
"strings"
7+
8+
"github.com/YaCodeDev/GoYaCodeDevUtils/yaerrors"
69
)
710

811
// ParseMapFrom parses a string into a map[K]V using the provided separators.
@@ -22,7 +25,7 @@ func ParseMap[K ParsableComparableType, V ParsableType](
2225
str string,
2326
entrySeparator *string,
2427
kvSeparator *string,
25-
) (map[K]V, error) {
28+
) (map[K]V, yaerrors.Error) {
2629
result := make(map[K]V)
2730

2831
if str == "" {
@@ -51,25 +54,39 @@ func ParseMap[K ParsableComparableType, V ParsableType](
5154
if len(parts) == MapPartsCount {
5255
k, err = ParseValue[K](strings.TrimSpace(parts[0]))
5356
if err != nil {
54-
return nil, fmt.Errorf(
55-
"failed to parse key '%s': %w",
56-
strings.TrimSpace(parts[0]),
57+
return nil, yaerrors.FromError(
58+
http.StatusInternalServerError,
5759
err,
60+
fmt.Sprintf(
61+
"parse map: failed to parse key '%s'",
62+
strings.TrimSpace(parts[0]),
63+
),
5864
)
5965
}
6066

6167
v, err = ParseValue[V](strings.TrimSpace(parts[1]))
6268
if err != nil {
63-
return nil, fmt.Errorf(
64-
"failed to parse value '%s': %w",
65-
strings.TrimSpace(parts[1]),
69+
return nil, yaerrors.FromError(
70+
http.StatusInternalServerError,
6671
err,
72+
fmt.Sprintf(
73+
"parse map: failed to parse value '%s'",
74+
strings.TrimSpace(parts[1]),
75+
),
6776
)
6877
}
6978

7079
result[k] = v
7180
} else {
72-
return nil, fmt.Errorf("%w: expected %d parts, got %d", ErrInvalidEntry, MapPartsCount, len(parts))
81+
return nil, yaerrors.FromError(
82+
http.StatusInternalServerError,
83+
ErrInvalidEntry,
84+
fmt.Sprintf(
85+
"parse map: expected %d parts, got %d",
86+
MapPartsCount,
87+
len(parts),
88+
),
89+
)
7390
}
7491
}
7592

valueparser/try_unmarshal.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package valueparser
33
import (
44
"encoding"
55
"fmt"
6+
"net/http"
67
"reflect"
8+
9+
"github.com/YaCodeDev/GoYaCodeDevUtils/yaerrors"
710
)
811

912
// TryUnmarshal is a generic function that converts a string value to the specified type T.
@@ -16,32 +19,63 @@ import (
1619
// if err != nil {
1720
// // Handle error
1821
// }
19-
func TryUnmarshal[T ParsableType](value string, valueType reflect.Type) (T, error) {
22+
func TryUnmarshal[T ParsableType](value string, valueType reflect.Type) (T, yaerrors.Error) {
2023
var zero T
2124

2225
typ := reflect.TypeOf(zero)
2326
ptr := reflect.New(valueType)
2427

2528
if unmarshaler, ok := ptr.Interface().(encoding.TextUnmarshaler); ok {
2629
if err := unmarshaler.UnmarshalText([]byte(value)); err != nil {
27-
return zero, fmt.Errorf("cannot convert value %v to type %s: %w", value, typ, err)
30+
return zero, yaerrors.FromError(
31+
http.StatusInternalServerError,
32+
err,
33+
fmt.Sprintf(
34+
"try unmarshal: cannot convert value %v to type %s",
35+
value,
36+
typ,
37+
),
38+
)
2839
}
2940
} else if unmarshaler, ok := ptr.Interface().(Unmarshalable); ok {
3041
if err := unmarshaler.Unmarshal(value); err != nil {
31-
return zero, ErrUnparsableValue
42+
return zero, yaerrors.FromError(
43+
http.StatusInternalServerError,
44+
ErrUnparsableValue,
45+
fmt.Sprintf(
46+
"try unmarshal: %v cannot be unmarshaled to type %s: %v",
47+
value,
48+
typ,
49+
err,
50+
),
51+
)
3252
}
3353
} else {
34-
return zero, ErrUnparsableValue
54+
return zero, yaerrors.FromError(
55+
http.StatusInternalServerError,
56+
ErrUnparsableValue,
57+
"try unmarshal: Unmarshalable interface not implemented",
58+
)
3559
}
3660

3761
val, err := ConvertValue(ptr.Elem(), typ)
3862
if err != nil {
39-
return zero, fmt.Errorf("cannot convert value %v to type %s: %w", value, typ, err)
63+
return zero, err.Wrap(
64+
fmt.Sprintf(
65+
"try unmarshal: cannot convert value %v to type %s",
66+
value,
67+
typ,
68+
),
69+
)
4070
}
4171

4272
if val, ok := val.Interface().(T); ok {
4373
return val, nil
4474
}
4575

46-
return zero, ErrInvalidValue
76+
return zero, yaerrors.FromError(
77+
http.StatusInternalServerError,
78+
ErrInvalidValue,
79+
"try unmarshal",
80+
)
4781
}

valueparser/value_convertor.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package valueparser
22

33
import (
4+
"net/http"
45
"reflect"
6+
7+
"github.com/YaCodeDev/GoYaCodeDevUtils/yaerrors"
58
)
69

710
// ConvertValue converts a reflect.Value to the specified target type.
811
// It checks if the value is valid and convertible to the target type.
912
// If the value is valid and convertible, it returns the converted value.
1013
// If the value is invalid, it returns a zero value of the target type.
1114
// If the value is valid but not convertible, it panics with an error message.
12-
func ConvertValue(val reflect.Value, targetType reflect.Type) (reflect.Value, error) {
15+
func ConvertValue(val reflect.Value, targetType reflect.Type) (reflect.Value, yaerrors.Error) {
1316
if !val.IsValid() {
1417
return reflect.Zero(targetType), nil
1518
}
@@ -18,5 +21,9 @@ func ConvertValue(val reflect.Value, targetType reflect.Type) (reflect.Value, er
1821
return val.Convert(targetType), nil
1922
}
2023

21-
return reflect.Value{}, ErrUnparsableValue
24+
return reflect.Value{}, yaerrors.FromError(
25+
http.StatusInternalServerError,
26+
ErrInvalidValue,
27+
"convert value: value is not convertible to target type",
28+
)
2229
}

valueparser/value_parser.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package valueparser
22

33
import (
4+
"net/http"
45
"reflect"
56
"strconv"
7+
8+
"github.com/YaCodeDev/GoYaCodeDevUtils/yaerrors"
69
)
710

811
// ParseValue is a generic function that converts a string value to the specified type T.
@@ -15,7 +18,7 @@ import (
1518
// if err != nil {
1619
// // Handle error
1720
// }
18-
func ParseValue[T ParsableType](value string) (T, error) {
21+
func ParseValue[T ParsableType](value string) (T, yaerrors.Error) {
1922
var zero T
2023

2124
typ := reflect.TypeOf(zero)
@@ -26,7 +29,11 @@ func ParseValue[T ParsableType](value string) (T, error) {
2629
return val, nil
2730
}
2831

29-
return zero, ErrInvalidValue
32+
return zero, yaerrors.FromError(
33+
http.StatusInternalServerError,
34+
ErrInvalidValue,
35+
"parse value: value is not a string",
36+
)
3037

3138
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
3239
if intValue, err := strconv.ParseInt(value, 10, 64); err == nil {
@@ -75,7 +82,11 @@ func ParseValue[T ParsableType](value string) (T, error) {
7582
reflect.Complex128,
7683
reflect.Array,
7784
reflect.UnsafePointer:
78-
return zero, ErrInvalidType
85+
return zero, yaerrors.FromError(
86+
http.StatusInternalServerError,
87+
ErrInvalidValue,
88+
"parse value: unsupported type "+typ.String(),
89+
)
7990
}
8091

8192
val, err := TryUnmarshal[T](value, typ)

yaautoflags/errors.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var (
88
ErrInstanceNil = errors.New("instance cannot be nil")
99
ErrInstanceNotStruct = errors.New("instance must be a struct")
1010
ErrFlagsFieldNotFound = errors.New("flags field not found in struct")
11-
ErrFlagsFieldTypeMismatch = errors.New("flags field must be of type uint64, uint32, uint16, uint8, uint or uintptr")
12-
ErrTooManyFlags = errors.New("too many flags set")
11+
ErrFlagsFieldTypeMismatch = errors.New(
12+
"flags field must be of type uint64, uint32, uint16, uint8, uint or uintptr",
13+
)
14+
ErrTooManyFlags = errors.New("too many flags set")
1315
)

yaautoflags/yaautoflags.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ func PackFlags[T any](instance *T) yaerrors.Error {
7474

7575
// nolint: exhaustive
7676
switch flagsField.Kind() {
77-
case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint, reflect.Uintptr:
77+
case reflect.Uint64,
78+
reflect.Uint32,
79+
reflect.Uint16,
80+
reflect.Uint8,
81+
reflect.Uint,
82+
reflect.Uintptr:
7883
default:
7984
return yaerrors.FromError(
8085
http.StatusInternalServerError,
@@ -160,7 +165,12 @@ func UnpackFlags[T any](instance *T) yaerrors.Error {
160165

161166
// nolint: exhaustive
162167
switch flagsField.Kind() {
163-
case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint, reflect.Uintptr:
168+
case reflect.Uint64,
169+
reflect.Uint32,
170+
reflect.Uint16,
171+
reflect.Uint8,
172+
reflect.Uint,
173+
reflect.Uintptr:
164174
default:
165175
return yaerrors.FromError(
166176
http.StatusInternalServerError,

yabackoff/exponential.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Exponential struct {
2929
// Example:
3030
//
3131
// backoff := yabackoff.NewExponential(0, 0, 0) // uses all defaults
32-
// fmt.Println(backoff.Current()) // 500 ms (default)
32+
// fmt.Println(backoff.Current()) // http.StatusInternalServerError ms (default)
3333
func NewExponential(
3434
initialInterval time.Duration,
3535
multiplier float64,
@@ -48,7 +48,7 @@ func NewExponential(
4848
// Example:
4949
//
5050
// backoff := yabackoff.NewExponential(250*time.Millisecond, 2, time.Second)
51-
// _ = backoff.Next() // 500 ms
51+
// _ = backoff.Next() // http.StatusInternalServerError ms
5252
// backoff.Reset()
5353
// fmt.Println(backoff.Current()) // 250 ms
5454
func (e *Exponential) Reset() {

yabackoff/yabackoff.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// # Quick start
66
//
7-
// backoff := yabackoff.NewExponential(500*time.Millisecond, 1.5, 60*time.Second)
7+
// backoff := yabackoff.NewExponential(http.StatusInternalServerError*time.Millisecond, 1.5, 60*time.Second)
88
// for {
99
// if err := doWork(); err == nil {
1010
// break // success – stop retrying
@@ -16,6 +16,7 @@
1616
package yabackoff
1717

1818
import (
19+
"net/http"
1920
"time"
2021
)
2122

@@ -24,7 +25,7 @@ import (
2425
// as a zero value and used without initialisation.
2526
const (
2627
// DefaultInitialInterval is used when initialInterval == 0.
27-
DefaultInitialInterval = 500 * time.Millisecond
28+
DefaultInitialInterval = http.StatusInternalServerError * time.Millisecond
2829

2930
// DefaultMultiplier is applied when multiplier == 0.
3031
DefaultMultiplier = 1.5

yabackoff/yabackoff_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package yabackoff_test
22

33
import (
4+
"net/http"
45
"testing"
56
"time"
67

@@ -24,7 +25,7 @@ func TestEmptySafety_Works(t *testing.T) {
2425
}
2526

2627
func TestNext_Works(t *testing.T) {
27-
start := 500 * time.Millisecond
28+
start := http.StatusInternalServerError * time.Millisecond
2829
multiplier := 1.5
2930
maxInterval := 10 * time.Second
3031

0 commit comments

Comments
 (0)