Skip to content

Commit db99f6e

Browse files
authored
Merge pull request #766 from ydb-platform/is-optional
* Added `table/types.IsOptional()` helper
2 parents c636165 + 1dcffca commit db99f6e

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added `table/types.IsOptional()` helper
2+
13
## v3.48.2
24
* Refactored tests
35

table/types/cast.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ func CastTo(v Value, dst interface{}) error {
1818
return value.CastTo(v, dst)
1919
}
2020

21+
// IsOptional checks if type is optional and returns innerType if it is.
22+
func IsOptional(t Type) (isOptional bool, innerType Type) {
23+
if optionalType, isOptional := t.(interface {
24+
IsOptional()
25+
InnerType() Type
26+
}); isOptional {
27+
return isOptional, optionalType.InnerType()
28+
}
29+
return false, nil
30+
}
31+
2132
// ToDecimal returns Decimal struct from abstract Value
2233
func ToDecimal(v Value) (*Decimal, error) {
2334
if valuer, isDecimalValuer := v.(value.DecimalValuer); isDecimalValuer {

table/types/cast_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,41 @@ import (
77
"github.com/stretchr/testify/require"
88
)
99

10+
func TestIsOptional(t *testing.T) {
11+
for _, tt := range []struct {
12+
t Type
13+
isOptional bool
14+
innerType Type
15+
}{
16+
{
17+
t: nil,
18+
isOptional: false,
19+
innerType: nil,
20+
},
21+
{
22+
t: TypeBool,
23+
isOptional: false,
24+
innerType: nil,
25+
},
26+
{
27+
t: Optional(TypeBool),
28+
isOptional: true,
29+
innerType: TypeBool,
30+
},
31+
{
32+
t: Optional(Optional(TypeBool)),
33+
isOptional: true,
34+
innerType: Optional(TypeBool),
35+
},
36+
} {
37+
t.Run("", func(t *testing.T) {
38+
isOptional, innerType := IsOptional(tt.t)
39+
require.Equal(t, tt.isOptional, isOptional)
40+
require.Equal(t, tt.innerType, innerType)
41+
})
42+
}
43+
}
44+
1045
func TestToDecimal(t *testing.T) {
1146
for _, tt := range []struct {
1247
v Value

0 commit comments

Comments
 (0)