Skip to content

Commit

Permalink
separate types into internal/types package
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Feb 20, 2024
1 parent 7d5e6a0 commit a31d84b
Show file tree
Hide file tree
Showing 25 changed files with 1,682 additions and 1,598 deletions.
19 changes: 19 additions & 0 deletions internal/decimal/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package decimal

import "math/big"

type Decimal struct {
Bytes [16]byte
Precision uint32
Scale uint32
}

func (d *Decimal) String() string {
v := FromInt128(d.Bytes, d.Precision, d.Scale)

return Format(v, d.Precision, d.Scale)
}

func (d *Decimal) BigInt() *big.Int {
return FromInt128(d.Bytes, d.Precision, d.Scale)
}
145 changes: 145 additions & 0 deletions internal/scanner/scanner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package scanner

import (
"io"
"time"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/decimal"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
)

// RawValue scanning non-primitive yql types or for own implementation scanner native API
type RawValue interface {
Path() string
WritePathTo(w io.Writer) (n int64, err error)
Type() types.Type
Bool() (v bool)
Int8() (v int8)
Uint8() (v uint8)
Int16() (v int16)
Uint16() (v uint16)
Int32() (v int32)
Uint32() (v uint32)
Int64() (v int64)
Uint64() (v uint64)
Float() (v float32)
Double() (v float64)
Date() (v time.Time)
Datetime() (v time.Time)
Timestamp() (v time.Time)
Interval() (v time.Duration)
TzDate() (v time.Time)
TzDatetime() (v time.Time)
TzTimestamp() (v time.Time)
String() (v []byte)
UTF8() (v string)
YSON() (v []byte)
JSON() (v []byte)
UUID() (v [16]byte)
JSONDocument() (v []byte)
DyNumber() (v string)
Value() value.Value

// Any returns any primitive or optional value.
// Currently, it may return one of these types:
//
// bool
// int8
// uint8
// int16
// uint16
// int32
// uint32
// int64
// uint64
// float32
// float64
// []byte
// string
// [16]byte
//
Any() interface{}

// Unwrap unwraps current item under scan interpreting it as Optional<Type> types.
Unwrap()
AssertType(t types.Type) bool
IsNull() bool
IsOptional() bool

// ListIn interprets current item under scan as a ydb's list.
// It returns the size of the nested items.
// If current item under scan is not a list types, it returns -1.
ListIn() (size int)

// ListItem selects current item i-th element as an item to scan.
// ListIn() must be called before.
ListItem(i int)

// ListOut leaves list entered before by ListIn() call.
ListOut()

// TupleIn interprets current item under scan as a ydb's tuple.
// It returns the size of the nested items.
TupleIn() (size int)

// TupleItem selects current item i-th element as an item to scan.
// Note that TupleIn() must be called before.
// It panics if it is out of bounds.
TupleItem(i int)

// TupleOut leaves tuple entered before by TupleIn() call.
TupleOut()

// StructIn interprets current item under scan as a ydb's struct.
// It returns the size of the nested items – the struct fields values.
// If there is no current item under scan it returns -1.
StructIn() (size int)

// StructField selects current item i-th field value as an item to scan.
// Note that StructIn() must be called before.
// It panics if i is out of bounds.
StructField(i int) (name string)

// StructOut leaves struct entered before by StructIn() call.
StructOut()

// DictIn interprets current item under scan as a ydb's dict.
// It returns the size of the nested items pairs.
// If there is no current item under scan it returns -1.
DictIn() (size int)

// DictKey selects current item i-th pair key as an item to scan.
// Note that DictIn() must be called before.
// It panics if i is out of bounds.
DictKey(i int)

// DictPayload selects current item i-th pair value as an item to scan.
// Note that DictIn() must be called before.
// It panics if i is out of bounds.
DictPayload(i int)

// DictOut leaves dict entered before by DictIn() call.
DictOut()

// Variant unwraps current item under scan interpreting it as Variant<Type> types.
// It returns non-empty name of a field that is filled for struct-based
// variant.
// It always returns an index of filled field of a Type.
Variant() (name string, index uint32)

// Decimal returns decimal value represented by big-endian 128 bit signed integer.
Decimal(t types.Type) (v [16]byte)

// UnwrapDecimal returns decimal value represented by big-endian 128 bit signed
// integer and its types information.
UnwrapDecimal() decimal.Decimal
IsDecimal() bool
Err() error
}

// Scanner scanning raw ydb types
type Scanner interface {
// UnmarshalYDB must be implemented on client-side for unmarshal raw ydb value.
UnmarshalYDB(raw RawValue) error
}
5 changes: 2 additions & 3 deletions internal/scripting/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/internal/scripting/config"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table/scanner"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
"github.com/ydb-platform/ydb-go-sdk/v3/scripting"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/result"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)

Expand Down Expand Up @@ -184,7 +183,7 @@ func (c *Client) explain(
ParameterTypes: make(map[string]types.Type, len(result.GetParametersTypes())),
}
for k, v := range result.GetParametersTypes() {
e.ParameterTypes[k] = value.TypeFromYDB(v)
e.ParameterTypes[k] = types.TypeFromYDB(v)
}

return e, nil
Expand Down
34 changes: 17 additions & 17 deletions internal/table/scanner/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ import (
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_TableStats"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
)

func TestResultAny(t *testing.T) {
for _, test := range []struct {
name string
columns []options.Column
values []types.Value
values []value.Value
exp []interface{}
}{
{
columns: []options.Column{
{
Name: "column0",
Type: types.Optional(types.TypeUint32),
Type: types.NewOptional(types.Uint32),
Family: "family0",
},
},
values: []types.Value{
types.OptionalValue(types.Uint32Value(43)),
types.NullValue(types.TypeUint32),
values: []value.Value{
value.OptionalValue(value.Uint32Value(43)),
value.NullValue(types.Uint32),
},
exp: []interface{}{
uint32(43),
Expand Down Expand Up @@ -83,25 +83,25 @@ func TestResultOUint32(t *testing.T) {
for _, test := range []struct {
name string
columns []options.Column
values []types.Value
values []value.Value
exp []uint32
}{
{
columns: []options.Column{
{
Name: "column0",
Type: types.Optional(types.TypeUint32),
Type: types.NewOptional(types.Uint32),
Family: "family0",
},
{
Name: "column1",
Type: types.TypeUint32,
Type: types.Uint32,
Family: "family0",
},
},
values: []types.Value{
types.OptionalValue(types.Uint32Value(43)),
types.Uint32Value(43),
values: []value.Value{
value.OptionalValue(value.Uint32Value(43)),
value.Uint32Value(43),
},
exp: []uint32{
43,
Expand Down Expand Up @@ -151,13 +151,13 @@ func WithColumns(cs ...options.Column) ResultSetOption {
for _, c := range cs {
r.Columns = append(r.Columns, &Ydb.Column{
Name: c.Name,
Type: value.TypeToYDB(c.Type, a),
Type: types.TypeToYDB(c.Type, a),
})
}
}
}

func WithValues(vs ...types.Value) ResultSetOption {
func WithValues(vs ...value.Value) ResultSetOption {
return func(r *resultSetDesc, a *allocator.Allocator) {
n := len(r.Columns)
if n == 0 {
Expand All @@ -178,9 +178,9 @@ func WithValues(vs ...types.Value) ResultSetOption {
}
}
tv := value.ToYDB(v, a)
act := value.TypeFromYDB(tv.Type)
exp := value.TypeFromYDB(r.Columns[j].Type)
if !value.TypesEqual(act, exp) {
act := types.TypeFromYDB(tv.Type)
exp := types.TypeFromYDB(r.Columns[j].Type)
if !types.Equal(act, exp) {
panic(fmt.Sprintf(
"unexpected types for #%d column: %s; want %s",
j, act, exp,
Expand Down
25 changes: 13 additions & 12 deletions internal/table/scanner/scan_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/decimal"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
)

type rawConverter struct {
Expand Down Expand Up @@ -282,8 +283,8 @@ func (s *rawConverter) Any() interface{} {
return s.any()
}

// Value returns current item under scan as ydb.Value types.
func (s *rawConverter) Value() types.Value {
// Value returns current item under scan as value
func (s *rawConverter) Value() value.Value {
if s.Err() != nil {
return nil
}
Expand Down Expand Up @@ -557,17 +558,17 @@ func (s *rawConverter) Decimal(t types.Type) (v [16]byte) {
return s.uint128()
}

func (s *rawConverter) UnwrapDecimal() (v types.Decimal) {
func (s *rawConverter) UnwrapDecimal() decimal.Decimal {
if s.Err() != nil {
return
return decimal.Decimal{}
}
s.unwrap()
d := s.assertTypeDecimal(s.stack.current().t)
if d == nil {
return
return decimal.Decimal{}
}

return types.Decimal{
return decimal.Decimal{
Bytes: s.uint128(),
Precision: d.DecimalType.Precision,
Scale: d.DecimalType.Scale,
Expand All @@ -583,9 +584,9 @@ func (s *rawConverter) IsDecimal() bool {
}

func isEqualDecimal(d *Ydb.DecimalType, t types.Type) bool {
w := t.(*value.DecimalType)
w := t.(*types.Decimal)

return d.Precision == w.Precision && d.Scale == w.Scale
return d.Precision == w.Precision() && d.Scale == w.Scale()
}

func (s *rawConverter) isCurrentTypeDecimal() bool {
Expand Down Expand Up @@ -712,8 +713,8 @@ func (s *rawConverter) assertCurrentTypeNullable() bool {

func (s *rawConverter) assertCurrentTypeIs(t types.Type) bool {
c := s.stack.current()
act := value.TypeFromYDB(c.t)
if !value.TypesEqual(act, t) {
act := types.TypeFromYDB(c.t)
if !types.Equal(act, t) {
_ = s.errorf(
1,
"unexpected types at %q %s: %s; want %s",
Expand Down Expand Up @@ -818,7 +819,7 @@ func nameIface(v interface{}) string {
t := reflect.TypeOf(v)
s := t.String()
s = strings.TrimPrefix(s, "*Ydb.Value_")
s = strings.TrimSuffix(s, "Value")
s = strings.TrimSuffix(s, "valueType")
s = strings.TrimPrefix(s, "*Ydb.Type_")
s = strings.TrimSuffix(s, "Type")

Expand Down
Loading

0 comments on commit a31d84b

Please sign in to comment.