File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ * Added ` table/types.IsOptional() ` helper
2+
13## v3.48.2
24* Refactored tests
35
Original file line number Diff line number Diff 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
2233func ToDecimal (v Value ) (* Decimal , error ) {
2334 if valuer , isDecimalValuer := v .(value.DecimalValuer ); isDecimalValuer {
Original file line number Diff line number Diff 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+
1045func TestToDecimal (t * testing.T ) {
1146 for _ , tt := range []struct {
1247 v Value
You can’t perform that action at this time.
0 commit comments