github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/table/types/cast.go (about) 1 package types 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/ydb-platform/ydb-go-sdk/v3/internal/value" 8 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" 9 ) 10 11 var errNilValue = errors.New("nil value") 12 13 // CastTo try cast value to destination type value 14 func CastTo(v Value, dst interface{}) error { 15 if v == nil { 16 return xerrors.WithStackTrace(errNilValue) 17 } 18 19 return value.CastTo(v, dst) 20 } 21 22 // IsOptional checks if type is optional and returns innerType if it is. 23 func IsOptional(t Type) (isOptional bool, innerType Type) { 24 if optionalType, isOptional := t.(interface { 25 IsOptional() 26 InnerType() Type 27 }); isOptional { 28 return isOptional, optionalType.InnerType() 29 } 30 31 return false, nil 32 } 33 34 // ToDecimal returns Decimal struct from abstract Value 35 func ToDecimal(v Value) (*Decimal, error) { 36 if valuer, isDecimalValuer := v.(value.DecimalValuer); isDecimalValuer { 37 return &Decimal{ 38 Bytes: valuer.Value(), 39 Precision: valuer.Precision(), 40 Scale: valuer.Scale(), 41 }, nil 42 } 43 44 return nil, xerrors.WithStackTrace(fmt.Errorf("value type '%s' is not decimal type", v.Type().Yql())) 45 } 46 47 // ListItems returns list items from abstract Value 48 func ListItems(v Value) ([]Value, error) { 49 if vv, has := v.(interface { 50 ListItems() []Value 51 }); has { 52 return vv.ListItems(), nil 53 } 54 55 return nil, xerrors.WithStackTrace(fmt.Errorf("cannot get list items from '%s'", v.Type().Yql())) 56 } 57 58 // TupleItems returns tuple items from abstract Value 59 func TupleItems(v Value) ([]Value, error) { 60 if vv, has := v.(interface { 61 TupleItems() []Value 62 }); has { 63 return vv.TupleItems(), nil 64 } 65 66 return nil, xerrors.WithStackTrace(fmt.Errorf("cannot get tuple items from '%s'", v.Type().Yql())) 67 } 68 69 // StructFields returns struct fields from abstract Value 70 func StructFields(v Value) (map[string]Value, error) { 71 if vv, has := v.(interface { 72 StructFields() map[string]Value 73 }); has { 74 return vv.StructFields(), nil 75 } 76 77 return nil, xerrors.WithStackTrace(fmt.Errorf("cannot get struct fields from '%s'", v.Type().Yql())) 78 } 79 80 // VariantValue returns variant value from abstract Value 81 func VariantValue(v Value) (name string, idx uint32, _ Value, _ error) { 82 if vv, has := v.(interface { 83 Variant() (name string, index uint32) 84 Value() Value 85 }); has { 86 name, idx := vv.Variant() 87 88 return name, idx, vv.Value(), nil 89 } 90 91 return "", 0, nil, xerrors.WithStackTrace(fmt.Errorf("cannot get variant value from '%s'", v.Type().Yql())) 92 } 93 94 // DictFields returns dict values from abstract Value 95 // 96 // Deprecated: use DictValues instead 97 func DictFields(v Value) (map[Value]Value, error) { 98 return DictValues(v) 99 } 100 101 // DictValues returns dict values from abstract Value 102 func DictValues(v Value) (map[Value]Value, error) { 103 if vv, has := v.(interface { 104 DictValues() map[Value]Value 105 }); has { 106 return vv.DictValues(), nil 107 } 108 109 return nil, xerrors.WithStackTrace(fmt.Errorf("cannot get dict values from '%s'", v.Type().Yql())) 110 }