Skip to content

Commit eb5c18a

Browse files
committed
Refactor: replace interface{} with any
1 parent 4ff3a0f commit eb5c18a

8 files changed

+38
-47
lines changed

csvlib.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ type EncodeFunc func(v reflect.Value, omitempty bool) (string, error)
4343
type ProcessorFunc func(s string) string
4444

4545
// ValidatorFunc function to validate the values of decoded cells
46-
type ValidatorFunc func(v interface{}) error
46+
type ValidatorFunc func(v any) error
4747

48-
type ParameterMap map[string]interface{}
48+
type ParameterMap map[string]any
4949

5050
// LocalizationFunc function to translate message into a specific language
5151
type LocalizationFunc func(key string, params ParameterMap) (string, error)
@@ -83,7 +83,7 @@ func Marshal(v any, options ...EncodeOption) ([]byte, error) {
8383
}
8484

8585
// GetHeaderDetails get CSV header details from the given struct type
86-
func GetHeaderDetails(v interface{}, tagName string) (columnDetails []ColumnDetail, err error) {
86+
func GetHeaderDetails(v any, tagName string) (columnDetails []ColumnDetail, err error) {
8787
t := reflect.TypeOf(v)
8888
t = indirectType(t)
8989
if t.Kind() != reflect.Struct {
@@ -108,7 +108,7 @@ func GetHeaderDetails(v interface{}, tagName string) (columnDetails []ColumnDeta
108108
}
109109

110110
// GetHeader get CSV header from the given struct
111-
func GetHeader(v interface{}, tagName string) ([]string, error) {
111+
func GetHeader(v any, tagName string) ([]string, error) {
112112
details, err := GetHeaderDetails(v, tagName)
113113
if err != nil {
114114
return nil, err

decoder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func NewDecoder(r Reader, options ...DecodeOption) *Decoder {
156156

157157
// Decode decode input data and store the result in the given variable
158158
// The input var must be a pointer to a slice, e.g. `*[]Student` (recommended) or `*[]*Student`
159-
func (d *Decoder) Decode(v interface{}) (*DecodeResult, error) {
159+
func (d *Decoder) Decode(v any) (*DecodeResult, error) {
160160
if d.finished {
161161
return nil, ErrFinished
162162
}
@@ -220,7 +220,7 @@ func (d *Decoder) Decode(v interface{}) (*DecodeResult, error) {
220220
// The input var must be a pointer to a struct (e.g. *Student)
221221
// This func returns error of the current row processing only, after finishing the last row decoding,
222222
// call Finish() to get the overall result and error.
223-
func (d *Decoder) DecodeOne(v interface{}) error {
223+
func (d *Decoder) DecodeOne(v any) error {
224224
if d.finished {
225225
return ErrFinished
226226
}

decoder_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -931,16 +931,16 @@ func Test_Decode_specialCases(t *testing.T) {
931931
}
932932

933933
func Test_Decode_specialTypes(t *testing.T) {
934-
t.Run("#1: interface{} type", func(t *testing.T) {
934+
t.Run("#1: any type", func(t *testing.T) {
935935
data := gofn.MultilineString(`col1,col2
936936
1,2.2
937937
100,3.3`)
938938

939939
type Item struct {
940940
ColX bool `csv:",optional"`
941941
ColY bool
942-
Col1 int `csv:"col1"`
943-
Col2 interface{} `csv:"col2"`
942+
Col1 int `csv:"col1"`
943+
Col2 any `csv:"col2"`
944944
}
945945

946946
var v []Item
@@ -950,24 +950,24 @@ func Test_Decode_specialTypes(t *testing.T) {
950950
assert.Equal(t, []Item{{Col1: 1, Col2: "2.2"}, {Col1: 100, Col2: "3.3"}}, v)
951951
})
952952

953-
t.Run("#2: ptr interface{} type", func(t *testing.T) {
953+
t.Run("#2: ptr to any type", func(t *testing.T) {
954954
data := gofn.MultilineString(`col1,col2
955955
1,2.2
956956
100,3.3`)
957957

958958
type Item struct {
959959
ColX bool `csv:",optional"`
960960
ColY bool
961-
Col1 int `csv:"col1"`
962-
Col2 *interface{} `csv:"col2"`
961+
Col1 int `csv:"col1"`
962+
Col2 *any `csv:"col2"`
963963
}
964964

965965
var v []Item
966966
ret, err := makeDecoder(data).Decode(&v)
967967
assert.Nil(t, err)
968968
assert.Equal(t, 3, ret.TotalRow())
969-
assert.Equal(t, []Item{{Col1: 1, Col2: gofn.New[interface{}]("2.2")},
970-
{Col1: 100, Col2: gofn.New[interface{}]("3.3")}}, v)
969+
assert.Equal(t, []Item{{Col1: 1, Col2: gofn.New[any]("2.2")},
970+
{Col1: 100, Col2: gofn.New[any]("3.3")}}, v)
971971
})
972972

973973
t.Run("#3: all base types", func(t *testing.T) {

encoder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func NewEncoder(w Writer, options ...EncodeOption) *Encoder {
8787

8888
// Encode encode input data stored in the given variable
8989
// The input var must be a slice, e.g. `[]Student` or `[]*Student`
90-
func (e *Encoder) Encode(v interface{}) error {
90+
func (e *Encoder) Encode(v any) error {
9191
if e.finished {
9292
return ErrFinished
9393
}
@@ -130,7 +130,7 @@ func (e *Encoder) Encode(v interface{}) error {
130130
}
131131

132132
// EncodeOne encode single object into a single CSV row
133-
func (e *Encoder) EncodeOne(v interface{}) error {
133+
func (e *Encoder) EncodeOne(v any) error {
134134
if e.finished {
135135
return ErrFinished
136136
}

encoder_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -756,10 +756,10 @@ func Test_Encode_specialCases(t *testing.T) {
756756
}
757757

758758
func Test_Encode_specialTypes(t *testing.T) {
759-
t.Run("#1: interface{} type", func(t *testing.T) {
759+
t.Run("#1: any type", func(t *testing.T) {
760760
type Item struct {
761-
Col1 int `csv:"col1"`
762-
Col2 interface{} `csv:"col2"`
761+
Col1 int `csv:"col1"`
762+
Col2 any `csv:"col2"`
763763
}
764764

765765
v := []Item{
@@ -779,17 +779,17 @@ func Test_Encode_specialTypes(t *testing.T) {
779779
`), string(data))
780780
})
781781

782-
t.Run("#2: ptr interface{} type", func(t *testing.T) {
782+
t.Run("#2: ptr to any type", func(t *testing.T) {
783783
type Item struct {
784-
Col1 int `csv:"col1"`
785-
Col2 *interface{} `csv:"col2"`
784+
Col1 int `csv:"col1"`
785+
Col2 *any `csv:"col2"`
786786
}
787787

788788
v := []*Item{
789-
{Col1: 1, Col2: gofn.New[interface{}](2.123)},
789+
{Col1: 1, Col2: gofn.New[any](2.123)},
790790
nil,
791-
{Col1: 100, Col2: gofn.New[interface{}]("200")},
792-
{Col1: 100, Col2: gofn.New[interface{}](true)},
791+
{Col1: 100, Col2: gofn.New[any]("200")},
792+
{Col1: 100, Col2: gofn.New[any](true)},
793793
}
794794
data, err := doEncode(v)
795795
assert.Nil(t, err)

errors.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (e *RowErrors) Unwrap() []error {
182182

183183
type CellError struct {
184184
err error
185-
fields map[string]interface{}
185+
fields map[string]any
186186
localizationKey string
187187

188188
column int
@@ -191,7 +191,7 @@ type CellError struct {
191191
}
192192

193193
func NewCellError(err error, column int, header string) *CellError {
194-
return &CellError{err: err, column: column, header: header, fields: map[string]interface{}{}}
194+
return &CellError{err: err, column: column, header: header, fields: map[string]any{}}
195195
}
196196

197197
func (e *CellError) Error() string {
@@ -225,7 +225,7 @@ func (e *CellError) Unwrap() error {
225225
return e.err
226226
}
227227

228-
func (e *CellError) WithParam(k string, v interface{}) *CellError {
228+
func (e *CellError) WithParam(k string, v any) *CellError {
229229
e.fields[k] = v
230230
return e
231231
}

reflect.go

-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ import (
44
"reflect"
55
)
66

7-
// func isKindOf(k reflect.Kind, kinds ...reflect.Kind) bool {
8-
// for _, kk := range kinds {
9-
// if k == kk {
10-
// return true
11-
// }
12-
// }
13-
// return false
14-
// }
15-
167
func isKindOrPtrOf(t reflect.Type, kinds ...reflect.Kind) bool {
178
k := t.Kind()
189
if k == reflect.Pointer {

validator.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// ValidatorLT validate a value to be less than the given value
1212
func ValidatorLT[T LTComparable](val T) ValidatorFunc {
13-
return func(v interface{}) error {
13+
return func(v any) error {
1414
v1, ok := v.(T)
1515
if !ok {
1616
return errValidationConversion(v, v1)
@@ -24,7 +24,7 @@ func ValidatorLT[T LTComparable](val T) ValidatorFunc {
2424

2525
// ValidatorLTE validate a value to be less than or equal to the given value
2626
func ValidatorLTE[T LTComparable](val T) ValidatorFunc {
27-
return func(v interface{}) error {
27+
return func(v any) error {
2828
v1, ok := v.(T)
2929
if !ok {
3030
return errValidationConversion(v, v1)
@@ -38,7 +38,7 @@ func ValidatorLTE[T LTComparable](val T) ValidatorFunc {
3838

3939
// ValidatorGT validate a value to be greater than the given value
4040
func ValidatorGT[T LTComparable](val T) ValidatorFunc {
41-
return func(v interface{}) error {
41+
return func(v any) error {
4242
v1, ok := v.(T)
4343
if !ok {
4444
return errValidationConversion(v, v1)
@@ -52,7 +52,7 @@ func ValidatorGT[T LTComparable](val T) ValidatorFunc {
5252

5353
// ValidatorGTE validate a value to be greater than or equal to the given value
5454
func ValidatorGTE[T LTComparable](val T) ValidatorFunc {
55-
return func(v interface{}) error {
55+
return func(v any) error {
5656
v1, ok := v.(T)
5757
if !ok {
5858
return errValidationConversion(v, v1)
@@ -66,7 +66,7 @@ func ValidatorGTE[T LTComparable](val T) ValidatorFunc {
6666

6767
// ValidatorRange validate a value to be in the given range (min and max are inclusive)
6868
func ValidatorRange[T LTComparable](min, max T) ValidatorFunc {
69-
return func(v interface{}) error {
69+
return func(v any) error {
7070
v1, ok := v.(T)
7171
if !ok {
7272
return errValidationConversion(v, v1)
@@ -80,7 +80,7 @@ func ValidatorRange[T LTComparable](min, max T) ValidatorFunc {
8080

8181
// ValidatorIN validate a value to be one of the specific values
8282
func ValidatorIN[T LTComparable](vals ...T) ValidatorFunc {
83-
return func(v interface{}) error {
83+
return func(v any) error {
8484
v1, ok := v.(T)
8585
if !ok {
8686
return errValidationConversion(v, v1)
@@ -97,7 +97,7 @@ func ValidatorIN[T LTComparable](vals ...T) ValidatorFunc {
9797
// ValidatorStrLen validate a string to have length in the given range
9898
// Pass argument -1 to skip the equivalent validation.
9999
func ValidatorStrLen[T StringEx](minLen, maxLen int, lenFuncs ...func(s string) int) ValidatorFunc {
100-
return func(v interface{}) error {
100+
return func(v any) error {
101101
s, ok := v.(T)
102102
if !ok {
103103
return errValidationConversion(v, s)
@@ -116,7 +116,7 @@ func ValidatorStrLen[T StringEx](minLen, maxLen int, lenFuncs ...func(s string)
116116

117117
// ValidatorStrPrefix validate a string to have prefix matching the given one
118118
func ValidatorStrPrefix[T StringEx](prefix string) ValidatorFunc {
119-
return func(v interface{}) error {
119+
return func(v any) error {
120120
s, ok := v.(T)
121121
if !ok {
122122
return errValidationConversion(v, s)
@@ -130,7 +130,7 @@ func ValidatorStrPrefix[T StringEx](prefix string) ValidatorFunc {
130130

131131
// ValidatorStrSuffix validate a string to have suffix matching the given one
132132
func ValidatorStrSuffix[T StringEx](suffix string) ValidatorFunc {
133-
return func(v interface{}) error {
133+
return func(v any) error {
134134
s, ok := v.(T)
135135
if !ok {
136136
return errValidationConversion(v, s)
@@ -142,6 +142,6 @@ func ValidatorStrSuffix[T StringEx](suffix string) ValidatorFunc {
142142
}
143143
}
144144

145-
func errValidationConversion[T any](v1 interface{}, v2 T) error {
145+
func errValidationConversion[T any](v1 any, v2 T) error {
146146
return fmt.Errorf("%w: (%v -> %v)", ErrValidationConversion, reflect.TypeOf(v1), reflect.TypeOf(v2))
147147
}

0 commit comments

Comments
 (0)