Skip to content

Commit 491e36d

Browse files
authored
Merge pull request #1053 from ydb-platform/move-types
separate types into internal/types package
2 parents 7d5e6a0 + dd7cfff commit 491e36d

File tree

26 files changed

+1757
-1673
lines changed

26 files changed

+1757
-1673
lines changed

internal/decimal/type.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package decimal
2+
3+
import "math/big"
4+
5+
type Decimal struct {
6+
Bytes [16]byte
7+
Precision uint32
8+
Scale uint32
9+
}
10+
11+
func (d *Decimal) String() string {
12+
v := FromInt128(d.Bytes, d.Precision, d.Scale)
13+
14+
return Format(v, d.Precision, d.Scale)
15+
}
16+
17+
func (d *Decimal) BigInt() *big.Int {
18+
return FromInt128(d.Bytes, d.Precision, d.Scale)
19+
}

internal/scanner/scanner.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package scanner
2+
3+
import (
4+
"io"
5+
"time"
6+
7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/decimal"
8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
9+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
10+
)
11+
12+
// RawValue scanning non-primitive yql types or for own implementation scanner native API
13+
type RawValue interface {
14+
Path() string
15+
WritePathTo(w io.Writer) (n int64, err error)
16+
Type() types.Type
17+
Bool() (v bool)
18+
Int8() (v int8)
19+
Uint8() (v uint8)
20+
Int16() (v int16)
21+
Uint16() (v uint16)
22+
Int32() (v int32)
23+
Uint32() (v uint32)
24+
Int64() (v int64)
25+
Uint64() (v uint64)
26+
Float() (v float32)
27+
Double() (v float64)
28+
Date() (v time.Time)
29+
Datetime() (v time.Time)
30+
Timestamp() (v time.Time)
31+
Interval() (v time.Duration)
32+
TzDate() (v time.Time)
33+
TzDatetime() (v time.Time)
34+
TzTimestamp() (v time.Time)
35+
String() (v []byte)
36+
UTF8() (v string)
37+
YSON() (v []byte)
38+
JSON() (v []byte)
39+
UUID() (v [16]byte)
40+
JSONDocument() (v []byte)
41+
DyNumber() (v string)
42+
Value() value.Value
43+
44+
// Any returns any primitive or optional value.
45+
// Currently, it may return one of these types:
46+
//
47+
// bool
48+
// int8
49+
// uint8
50+
// int16
51+
// uint16
52+
// int32
53+
// uint32
54+
// int64
55+
// uint64
56+
// float32
57+
// float64
58+
// []byte
59+
// string
60+
// [16]byte
61+
//
62+
Any() interface{}
63+
64+
// Unwrap unwraps current item under scan interpreting it as Optional<Type> types.
65+
Unwrap()
66+
AssertType(t types.Type) bool
67+
IsNull() bool
68+
IsOptional() bool
69+
70+
// ListIn interprets current item under scan as a ydb's list.
71+
// It returns the size of the nested items.
72+
// If current item under scan is not a list types, it returns -1.
73+
ListIn() (size int)
74+
75+
// ListItem selects current item i-th element as an item to scan.
76+
// ListIn() must be called before.
77+
ListItem(i int)
78+
79+
// ListOut leaves list entered before by ListIn() call.
80+
ListOut()
81+
82+
// TupleIn interprets current item under scan as a ydb's tuple.
83+
// It returns the size of the nested items.
84+
TupleIn() (size int)
85+
86+
// TupleItem selects current item i-th element as an item to scan.
87+
// Note that TupleIn() must be called before.
88+
// It panics if it is out of bounds.
89+
TupleItem(i int)
90+
91+
// TupleOut leaves tuple entered before by TupleIn() call.
92+
TupleOut()
93+
94+
// StructIn interprets current item under scan as a ydb's struct.
95+
// It returns the size of the nested items – the struct fields values.
96+
// If there is no current item under scan it returns -1.
97+
StructIn() (size int)
98+
99+
// StructField selects current item i-th field value as an item to scan.
100+
// Note that StructIn() must be called before.
101+
// It panics if i is out of bounds.
102+
StructField(i int) (name string)
103+
104+
// StructOut leaves struct entered before by StructIn() call.
105+
StructOut()
106+
107+
// DictIn interprets current item under scan as a ydb's dict.
108+
// It returns the size of the nested items pairs.
109+
// If there is no current item under scan it returns -1.
110+
DictIn() (size int)
111+
112+
// DictKey selects current item i-th pair key as an item to scan.
113+
// Note that DictIn() must be called before.
114+
// It panics if i is out of bounds.
115+
DictKey(i int)
116+
117+
// DictPayload selects current item i-th pair value as an item to scan.
118+
// Note that DictIn() must be called before.
119+
// It panics if i is out of bounds.
120+
DictPayload(i int)
121+
122+
// DictOut leaves dict entered before by DictIn() call.
123+
DictOut()
124+
125+
// Variant unwraps current item under scan interpreting it as Variant<Type> types.
126+
// It returns non-empty name of a field that is filled for struct-based
127+
// variant.
128+
// It always returns an index of filled field of a Type.
129+
Variant() (name string, index uint32)
130+
131+
// Decimal returns decimal value represented by big-endian 128 bit signed integer.
132+
Decimal(t types.Type) (v [16]byte)
133+
134+
// UnwrapDecimal returns decimal value represented by big-endian 128 bit signed
135+
// integer and its types information.
136+
UnwrapDecimal() decimal.Decimal
137+
IsDecimal() bool
138+
Err() error
139+
}
140+
141+
// Scanner scanning raw ydb types
142+
type Scanner interface {
143+
// UnmarshalYDB must be implemented on client-side for unmarshal raw ydb value.
144+
UnmarshalYDB(raw RawValue) error
145+
}

internal/scripting/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ import (
1515
"github.com/ydb-platform/ydb-go-sdk/v3/internal/scripting/config"
1616
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1717
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table/scanner"
18-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
18+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
1919
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
2020
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
2121
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
2222
"github.com/ydb-platform/ydb-go-sdk/v3/scripting"
2323
"github.com/ydb-platform/ydb-go-sdk/v3/table"
2424
"github.com/ydb-platform/ydb-go-sdk/v3/table/result"
25-
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
2625
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
2726
)
2827

@@ -184,7 +183,7 @@ func (c *Client) explain(
184183
ParameterTypes: make(map[string]types.Type, len(result.GetParametersTypes())),
185184
}
186185
for k, v := range result.GetParametersTypes() {
187-
e.ParameterTypes[k] = value.TypeFromYDB(v)
186+
e.ParameterTypes[k] = types.TypeFromYDB(v)
188187
}
189188

190189
return e, nil

internal/table/scanner/result.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
var errAlreadyClosed = xerrors.Wrap(errors.New("result closed early"))
1919

2020
type baseResult struct {
21-
scanner
21+
valueScanner
2222

2323
nextResultSetCounter atomic.Uint64
2424
statsMtx xsync.RWMutex
@@ -36,7 +36,7 @@ type streamResult struct {
3636

3737
// Err returns error caused Scanner to be broken.
3838
func (r *streamResult) Err() error {
39-
err := r.scanner.Err()
39+
err := r.valueScanner.Err()
4040
if err != nil {
4141
return xerrors.WithStackTrace(err)
4242
}
@@ -53,7 +53,7 @@ type unaryResult struct {
5353

5454
// Err returns error caused Scanner to be broken.
5555
func (r *unaryResult) Err() error {
56-
err := r.scanner.Err()
56+
err := r.valueScanner.Err()
5757
if err != nil {
5858
return xerrors.WithStackTrace(err)
5959
}
@@ -96,13 +96,13 @@ type option func(r *baseResult)
9696

9797
func WithIgnoreTruncated(ignoreTruncated bool) option {
9898
return func(r *baseResult) {
99-
r.scanner.ignoreTruncated = ignoreTruncated
99+
r.valueScanner.ignoreTruncated = ignoreTruncated
100100
}
101101
}
102102

103103
func WithMarkTruncatedAsRetryable() option {
104104
return func(r *baseResult) {
105-
r.scanner.markTruncatedAsRetryable = true
105+
r.valueScanner.markTruncatedAsRetryable = true
106106
}
107107
}
108108

internal/table/scanner/result_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@ import (
1313
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_TableStats"
1414

1515
"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
16+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
1617
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
1718
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
18-
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
1919
)
2020

2121
func TestResultAny(t *testing.T) {
2222
for _, test := range []struct {
2323
name string
2424
columns []options.Column
25-
values []types.Value
25+
values []value.Value
2626
exp []interface{}
2727
}{
2828
{
2929
columns: []options.Column{
3030
{
3131
Name: "column0",
32-
Type: types.Optional(types.TypeUint32),
32+
Type: types.NewOptional(types.Uint32),
3333
Family: "family0",
3434
},
3535
},
36-
values: []types.Value{
37-
types.OptionalValue(types.Uint32Value(43)),
38-
types.NullValue(types.TypeUint32),
36+
values: []value.Value{
37+
value.OptionalValue(value.Uint32Value(43)),
38+
value.NullValue(types.Uint32),
3939
},
4040
exp: []interface{}{
4141
uint32(43),
@@ -83,25 +83,25 @@ func TestResultOUint32(t *testing.T) {
8383
for _, test := range []struct {
8484
name string
8585
columns []options.Column
86-
values []types.Value
86+
values []value.Value
8787
exp []uint32
8888
}{
8989
{
9090
columns: []options.Column{
9191
{
9292
Name: "column0",
93-
Type: types.Optional(types.TypeUint32),
93+
Type: types.NewOptional(types.Uint32),
9494
Family: "family0",
9595
},
9696
{
9797
Name: "column1",
98-
Type: types.TypeUint32,
98+
Type: types.Uint32,
9999
Family: "family0",
100100
},
101101
},
102-
values: []types.Value{
103-
types.OptionalValue(types.Uint32Value(43)),
104-
types.Uint32Value(43),
102+
values: []value.Value{
103+
value.OptionalValue(value.Uint32Value(43)),
104+
value.Uint32Value(43),
105105
},
106106
exp: []uint32{
107107
43,
@@ -151,13 +151,13 @@ func WithColumns(cs ...options.Column) ResultSetOption {
151151
for _, c := range cs {
152152
r.Columns = append(r.Columns, &Ydb.Column{
153153
Name: c.Name,
154-
Type: value.TypeToYDB(c.Type, a),
154+
Type: types.TypeToYDB(c.Type, a),
155155
})
156156
}
157157
}
158158
}
159159

160-
func WithValues(vs ...types.Value) ResultSetOption {
160+
func WithValues(vs ...value.Value) ResultSetOption {
161161
return func(r *resultSetDesc, a *allocator.Allocator) {
162162
n := len(r.Columns)
163163
if n == 0 {
@@ -178,9 +178,9 @@ func WithValues(vs ...types.Value) ResultSetOption {
178178
}
179179
}
180180
tv := value.ToYDB(v, a)
181-
act := value.TypeFromYDB(tv.Type)
182-
exp := value.TypeFromYDB(r.Columns[j].Type)
183-
if !value.TypesEqual(act, exp) {
181+
act := types.TypeFromYDB(tv.Type)
182+
exp := types.TypeFromYDB(r.Columns[j].Type)
183+
if !types.Equal(act, exp) {
184184
panic(fmt.Sprintf(
185185
"unexpected types for #%d column: %s; want %s",
186186
j, act, exp,

0 commit comments

Comments
 (0)