Skip to content

Commit fab3464

Browse files
author
Dat Nguyen
committed
chore: add test for getPrimitiveValue util
1 parent 0f275e9 commit fab3464

File tree

4 files changed

+137
-5
lines changed

4 files changed

+137
-5
lines changed

errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ var (
2020

2121
// ErrUnsupportedType describes an unsupported field type
2222
type ErrUnsupportedType struct {
23-
typ reflect.Type
23+
typ reflect.Kind
2424
}
2525

2626
// Error returns the UnsupportedType error text
2727
func (e *ErrUnsupportedType) Error() string {
28-
return fmt.Sprintf("mold: unsupported field type: %s", e.typ.Kind())
28+
return fmt.Sprintf("mold: unsupported field type: %s", e.typ)
2929
}
3030

3131
// ErrUndefinedTag defines a tag that does not exist

modifiers/multi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func setValue(ctx context.Context, fl mold.FieldLevel) error {
132132
// 2. Attempting to set its underlying value
133133
// Try to convert the parameter string to the appropriate primitive type
134134
// that the pointer references (e.g., *string, *int, *bool)
135-
value, err := mold.GetPrimitiveValue(fl.Field().Type().Elem(), fl.Param())
135+
value, err := mold.GetPrimitiveValue(fl.Field().Type().Elem().Kind(), fl.Param())
136136
if err != nil {
137137
// If ErrUnsupportedType: leave as zero value
138138
if _, isUnsupportedType := err.(*mold.ErrUnsupportedType); isUnsupportedType {

util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func HasValue(field reflect.Value) bool {
3838
}
3939
}
4040

41-
func GetPrimitiveValue(typ reflect.Type, value string) (reflect.Value, error) {
42-
switch typ.Kind() {
41+
func GetPrimitiveValue(typ reflect.Kind, value string) (reflect.Value, error) {
42+
switch typ {
4343

4444
case reflect.String:
4545
return reflect.ValueOf(value), nil

util_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package mold
2+
3+
import (
4+
"math"
5+
"reflect"
6+
"testing"
7+
8+
. "github.com/go-playground/assert/v2"
9+
)
10+
11+
func TestGetPrimitiveValue(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
typ reflect.Kind
15+
value string
16+
expected reflect.Value
17+
expectError bool
18+
}{
19+
{
20+
name: "string",
21+
typ: reflect.String,
22+
value: "test",
23+
expected: reflect.ValueOf("test"),
24+
},
25+
{
26+
name: "int",
27+
typ: reflect.Int,
28+
value: "123",
29+
expected: reflect.ValueOf(123),
30+
},
31+
{
32+
name: "int8",
33+
typ: reflect.Int8,
34+
value: "123",
35+
expected: reflect.ValueOf(int8(123)),
36+
},
37+
{
38+
name: "int16",
39+
typ: reflect.Int16,
40+
value: "123",
41+
expected: reflect.ValueOf(int16(123)),
42+
},
43+
{
44+
name: "bool",
45+
typ: reflect.Bool,
46+
value: "true",
47+
expected: reflect.ValueOf(true),
48+
},
49+
{
50+
name: "error",
51+
typ: reflect.Int,
52+
value: "abc",
53+
expectError: true,
54+
},
55+
{
56+
name: "unsupported type",
57+
typ: reflect.Struct,
58+
value: "abc",
59+
expectError: true,
60+
},
61+
{
62+
name: "uint",
63+
typ: reflect.Uint,
64+
value: "123",
65+
expected: reflect.ValueOf(uint(123)),
66+
},
67+
{
68+
name: "uint8",
69+
typ: reflect.Uint8,
70+
value: "123",
71+
expected: reflect.ValueOf(uint8(123)),
72+
},
73+
{
74+
name: "uint16",
75+
typ: reflect.Uint16,
76+
value: "123",
77+
expected: reflect.ValueOf(uint16(123)),
78+
},
79+
{
80+
name: "uint32",
81+
typ: reflect.Uint32,
82+
value: "123",
83+
expected: reflect.ValueOf(uint32(123)),
84+
},
85+
{
86+
name: "uint64",
87+
typ: reflect.Uint64,
88+
value: "123",
89+
expected: reflect.ValueOf(uint64(123)),
90+
},
91+
{
92+
name: "float32",
93+
typ: reflect.Float32,
94+
value: "123.45",
95+
expected: reflect.ValueOf(float32(123.45)),
96+
},
97+
{
98+
name: "float64",
99+
typ: reflect.Float64,
100+
value: "123.45",
101+
expected: reflect.ValueOf(float64(123.45)),
102+
},
103+
}
104+
105+
for _, tc := range tests {
106+
t.Run(tc.name, func(t *testing.T) {
107+
actual, err := GetPrimitiveValue(tc.typ, tc.value)
108+
if tc.expectError {
109+
NotEqual(t, nil, err)
110+
} else {
111+
Equal(t, nil, err)
112+
switch tc.typ {
113+
case reflect.String:
114+
Equal(t, tc.expected.String(), actual.String())
115+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
116+
Equal(t, tc.expected.Int(), actual.Int())
117+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
118+
Equal(t, tc.expected.Uint(), actual.Uint())
119+
case reflect.Float32, reflect.Float64:
120+
// could not assert equal float because of precision issues
121+
// so we just check in 4 decimal places
122+
decimalMask := math.Pow(10, 4)
123+
Equal(t, math.Round(tc.expected.Float()*decimalMask), math.Round(actual.Float()*decimalMask))
124+
case reflect.Bool:
125+
Equal(t, tc.expected.Bool(), actual.Bool())
126+
default:
127+
Equal(t, tc.expected.Interface(), actual.Interface())
128+
}
129+
}
130+
})
131+
}
132+
}

0 commit comments

Comments
 (0)