Skip to content

Commit f91d7ea

Browse files
authored
Merge pull request #1263 from ydb-platform/cast-to-test
added test for internal/value.CastTo()
2 parents e2f6d46 + e1b847d commit f91d7ea

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

internal/value/cast.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package value
22

33
func CastTo(v Value, dst interface{}) error {
4+
if dst == nil {
5+
return errNilDestination
6+
}
7+
if ptr, has := dst.(*Value); has {
8+
*ptr = v
9+
10+
return nil
11+
}
12+
413
return v.castTo(dst)
514
}

internal/value/cast_test.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package value
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
10+
)
11+
12+
func ptr[T any]() interface{} {
13+
var zeroValue T
14+
15+
return &zeroValue
16+
}
17+
18+
func value2ptr[T any](v T) *T {
19+
return &v
20+
}
21+
22+
func unwrapPtr(v interface{}) interface{} {
23+
return reflect.ValueOf(v).Elem().Interface()
24+
}
25+
26+
func TestCastTo(t *testing.T) {
27+
for _, tt := range []struct {
28+
name string
29+
value Value
30+
dst interface{}
31+
exp interface{}
32+
err error
33+
}{
34+
{
35+
name: xtest.CurrentFileLine(),
36+
value: TextValue("test"),
37+
dst: (interface{})(nil),
38+
err: errNilDestination,
39+
},
40+
41+
{
42+
name: xtest.CurrentFileLine(),
43+
value: TextValue("test"),
44+
dst: ptr[Value](),
45+
exp: TextValue("test"),
46+
err: nil,
47+
},
48+
{
49+
name: xtest.CurrentFileLine(),
50+
value: OptionalValue(TextValue("test")),
51+
dst: ptr[Value](),
52+
exp: OptionalValue(TextValue("test")),
53+
err: nil,
54+
},
55+
{
56+
name: xtest.CurrentFileLine(),
57+
value: TextValue("test"),
58+
dst: ptr[string](),
59+
exp: "test",
60+
err: nil,
61+
},
62+
{
63+
name: xtest.CurrentFileLine(),
64+
value: OptionalValue(TextValue("test")),
65+
dst: ptr[*string](),
66+
exp: value2ptr("test"),
67+
err: nil,
68+
},
69+
{
70+
name: xtest.CurrentFileLine(),
71+
value: TextValue("test"),
72+
dst: ptr[[]byte](),
73+
exp: []byte("test"),
74+
err: nil,
75+
},
76+
{
77+
name: xtest.CurrentFileLine(),
78+
value: OptionalValue(TextValue("test")),
79+
dst: ptr[*[]byte](),
80+
exp: value2ptr([]byte("test")),
81+
err: nil,
82+
},
83+
{
84+
name: xtest.CurrentFileLine(),
85+
value: TextValue("test"),
86+
dst: ptr[[]byte](),
87+
exp: []byte("test"),
88+
err: nil,
89+
},
90+
{
91+
name: xtest.CurrentFileLine(),
92+
value: TextValue("test"),
93+
dst: ptr[int](),
94+
err: ErrCannotCast,
95+
},
96+
97+
{
98+
name: xtest.CurrentFileLine(),
99+
value: BytesValue([]byte("test")),
100+
dst: ptr[Value](),
101+
exp: BytesValue([]byte("test")),
102+
err: nil,
103+
},
104+
{
105+
name: xtest.CurrentFileLine(),
106+
value: BytesValue([]byte("test")),
107+
dst: ptr[string](),
108+
exp: "test",
109+
err: nil,
110+
},
111+
{
112+
name: xtest.CurrentFileLine(),
113+
value: BytesValue([]byte("test")),
114+
dst: ptr[[]byte](),
115+
exp: []byte("test"),
116+
err: nil,
117+
},
118+
{
119+
name: xtest.CurrentFileLine(),
120+
value: BytesValue([]byte("test")),
121+
dst: ptr[[]byte](),
122+
exp: []byte("test"),
123+
err: nil,
124+
},
125+
{
126+
name: xtest.CurrentFileLine(),
127+
value: BytesValue([]byte("test")),
128+
dst: ptr[int](),
129+
err: ErrCannotCast,
130+
},
131+
} {
132+
t.Run(tt.name, func(t *testing.T) {
133+
if tt.err == nil {
134+
require.NoError(t, CastTo(tt.value, tt.dst))
135+
require.Equal(t, tt.exp, unwrapPtr(tt.dst))
136+
} else {
137+
require.ErrorIs(t, CastTo(tt.value, tt.dst), tt.err)
138+
}
139+
})
140+
}
141+
}

internal/value/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ import "errors"
55
var (
66
ErrCannotCast = errors.New("cannot cast")
77
errDestinationTypeIsNotAPointer = errors.New("destination type is not a pointer")
8+
errNilDestination = errors.New("destination is nil")
89
)

0 commit comments

Comments
 (0)