Skip to content

Commit c36e0ba

Browse files
authored
Merge pull request #1094 from ydb-platform/query-scan-optional
fixed `optional.castTo` with reflect
2 parents 132dfcc + 9379a91 commit c36e0ba

File tree

9 files changed

+1284
-1082
lines changed

9 files changed

+1284
-1082
lines changed

internal/types/types_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package types
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/require"
57
)
68

79
func TestTypeToString(t *testing.T) {
@@ -273,3 +275,101 @@ func TestTypeToString(t *testing.T) {
273275
})
274276
}
275277
}
278+
279+
func TestEqual(t *testing.T) {
280+
tests := []struct {
281+
lhs Type
282+
rhs Type
283+
equal bool
284+
}{
285+
{
286+
Bool,
287+
Bool,
288+
true,
289+
},
290+
{
291+
Bool,
292+
Text,
293+
false,
294+
},
295+
{
296+
Text,
297+
Text,
298+
true,
299+
},
300+
{
301+
NewOptional(Bool),
302+
NewOptional(Bool),
303+
true,
304+
},
305+
{
306+
NewOptional(Bool),
307+
NewOptional(Text),
308+
false,
309+
},
310+
{
311+
NewOptional(Text),
312+
NewOptional(Text),
313+
true,
314+
},
315+
}
316+
for _, tt := range tests {
317+
t.Run("", func(t *testing.T) {
318+
if equal := Equal(tt.lhs, tt.rhs); equal != tt.equal {
319+
t.Errorf("Equal(%s, %s) = %v, want %v", tt.lhs, tt.rhs, equal, tt.equal)
320+
}
321+
})
322+
}
323+
}
324+
325+
func TestOptionalInnerType(t *testing.T) {
326+
tests := []struct {
327+
src Type
328+
innerType Type
329+
isOptional bool
330+
}{
331+
{
332+
Bool,
333+
nil,
334+
false,
335+
},
336+
{
337+
Text,
338+
nil,
339+
false,
340+
},
341+
{
342+
NewOptional(Bool),
343+
Bool,
344+
true,
345+
},
346+
{
347+
NewOptional(Text),
348+
Text,
349+
true,
350+
},
351+
{
352+
NewOptional(NewTuple(Text, Bool, Uint64, NewOptional(Int64))),
353+
NewTuple(Text, Bool, Uint64, NewOptional(Int64)),
354+
true,
355+
},
356+
}
357+
for _, tt := range tests {
358+
t.Run("", func(t *testing.T) {
359+
optional, isOptional := tt.src.(interface {
360+
IsOptional()
361+
InnerType() Type
362+
})
363+
require.Equal(t, tt.isOptional, isOptional)
364+
var innerType Type
365+
if isOptional {
366+
innerType = optional.InnerType()
367+
}
368+
if tt.innerType == nil {
369+
require.Nil(t, innerType)
370+
} else {
371+
require.True(t, Equal(tt.innerType, innerType))
372+
}
373+
})
374+
}
375+
}

internal/value/errors.go

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

33
import "errors"
44

5-
var ErrCannotCast = errors.New("cannot cast")
5+
var (
6+
ErrCannotCast = errors.New("cannot cast")
7+
errDestinationTypeIsNotAPointer = errors.New("destination type is not a pointer")
8+
)

0 commit comments

Comments
 (0)