Skip to content

Commit 3e7f0b6

Browse files
committed
With[Custom]UserAgent + refactoring of integration tests + table.types.Type.String()
1 parent ec27580 commit 3e7f0b6

File tree

16 files changed

+650
-504
lines changed

16 files changed

+650
-504
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* Removed experimental `balancer.PreferEndpoints[WithFallback][RegEx]` balancers
55
* Supported connections `TTL` with `Option` `WithConnectionTTL`
66
* Remove unnecessary `WithFastDial` option (lazy connections are always fast inserts into cluster)
7-
* Added `ScriptingYQL` service client
7+
* Added `Scripting` service client with API methods `Execute()`, `StreamExecute()` and `Explain()`
8+
* Added `String()` method to `table.types.Type` interface
9+
* Added `With[Custom]UserAgent()` `Option` and `CustomOption` constructors
810

911
## 3.6.2
1012
* Refactored table retry helpers

config/config.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type config struct {
115115
endpoint string
116116
database string
117117
requestsType string
118+
userAgent string
118119
grpcOptions []grpc.DialOption
119120
credentials credentials.Credentials
120121
tlsConfig *tls.Config
@@ -247,6 +248,12 @@ func WithTrace(trace trace.Driver) Option {
247248
}
248249
}
249250

251+
func WithUserAgent(userAgent string) Option {
252+
return func(c *config) {
253+
c.userAgent = userAgent
254+
}
255+
}
256+
250257
func WithConnectionTTL(ttl time.Duration) Option {
251258
return func(c *config) {
252259
c.connectionTTL = ttl
@@ -324,7 +331,13 @@ func New(opts ...Option) Config {
324331
if c.discoveryInterval == 0 {
325332
c.balancer = balancer.SingleConn()
326333
}
327-
c.meta = meta.New(c.database, c.credentials, c.trace, c.requestsType)
334+
c.meta = meta.New(
335+
c.database,
336+
c.credentials,
337+
c.trace,
338+
c.requestsType,
339+
c.userAgent,
340+
)
328341
return c
329342
}
330343

connection.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,11 @@ func (c *connection) meta(opts ...CustomOption) meta.Meta {
188188
if len(opts) == 0 {
189189
return c.config.Meta()
190190
}
191-
options := customOptions{
192-
database: c.config.Database(),
193-
credentials: c.config.Credentials(),
194-
}
195-
for _, o := range opts {
196-
o(&options)
197-
}
198-
return meta.New(
199-
options.database,
200-
options.credentials,
201-
c.config.Trace(),
202-
c.config.RequestsType(),
203-
)
191+
options := &customOptions{meta: c.config.Meta()}
192+
for _, opt := range opts {
193+
opt(options)
194+
}
195+
return options.meta
204196
}
205197

206198
// New connects to name and return name runtime holder

custom_options.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,34 @@ import (
44
"fmt"
55

66
"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials"
7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
78
)
89

910
type customOptions struct {
10-
database string
11-
credentials credentials.Credentials
11+
meta meta.Meta
1212
}
1313

1414
type CustomOption func(opts *customOptions)
1515

1616
func WithCustomToken(accessToken string) CustomOption {
1717
return func(opts *customOptions) {
18-
opts.credentials = credentials.NewAccessTokenCredentials(
19-
accessToken,
20-
fmt.Sprintf(`WithCustomToken("%s")`, accessToken),
18+
opts.meta = opts.meta.WithCredentials(
19+
credentials.NewAccessTokenCredentials(
20+
accessToken,
21+
fmt.Sprintf(`WithCustomToken("%s")`, accessToken),
22+
),
2123
)
2224
}
2325
}
2426

27+
func WithCustomUserAgent(userAgent string) CustomOption {
28+
return func(opts *customOptions) {
29+
opts.meta = opts.meta.WithUserAgent(userAgent)
30+
}
31+
}
32+
2533
func WithCustomDatabase(database string) CustomOption {
2634
return func(opts *customOptions) {
27-
opts.database = database
35+
opts.meta = opts.meta.WithDatabase(database)
2836
}
2937
}

internal/lazy/scripting.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@ type lazyScripting struct {
1717
m sync.Mutex
1818
}
1919

20-
func (s *lazyScripting) ExecuteYql(
20+
func (s *lazyScripting) Execute(
2121
ctx context.Context,
2222
query string,
2323
params *table.QueryParameters,
2424
) (result.Result, error) {
2525
s.init()
26-
return s.client.ExecuteYql(ctx, query, params)
26+
return s.client.Execute(ctx, query, params)
2727
}
2828

29-
func (s *lazyScripting) ExplainYql(
29+
func (s *lazyScripting) Explain(
3030
ctx context.Context,
3131
query string,
3232
mode scripting.ExplainMode,
3333
) (table.ScriptingYQLExplanation, error) {
3434
s.init()
35-
return s.client.ExplainYql(ctx, query, mode)
35+
return s.client.Explain(ctx, query, mode)
3636
}
3737

38-
func (s *lazyScripting) StreamExecuteYql(
38+
func (s *lazyScripting) StreamExecute(
3939
ctx context.Context,
4040
query string,
4141
params *table.QueryParameters,
4242
) (result.StreamResult, error) {
4343
s.init()
44-
return s.client.StreamExecuteYql(ctx, query, params)
44+
return s.client.StreamExecute(ctx, query, params)
4545
}
4646

4747
func (s *lazyScripting) Close(ctx context.Context) error {

internal/meta/meta.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ const (
2121

2222
type Meta interface {
2323
Meta(ctx context.Context) (context.Context, error)
24+
25+
WithDatabase(database string) Meta
26+
WithCredentials(creds credentials.Credentials) Meta
27+
WithUserAgent(userAgent string) Meta
2428
}
2529

2630
func New(
2731
database string,
2832
credentials credentials.Credentials,
2933
trace trace.Driver,
3034
requestsType string,
35+
userAgent string,
3136
) Meta {
3237
return &meta{
3338
trace: trace,
3439
credentials: credentials,
3540
database: database,
3641
requestsType: requestsType,
42+
userAgent: userAgent,
3743
}
3844
}
3945

@@ -42,6 +48,25 @@ type meta struct {
4248
credentials credentials.Credentials
4349
database string
4450
requestsType string
51+
userAgent string
52+
}
53+
54+
func (m *meta) WithDatabase(database string) Meta {
55+
mm := *m
56+
mm.database = database
57+
return &mm
58+
}
59+
60+
func (m *meta) WithCredentials(creds credentials.Credentials) Meta {
61+
mm := *m
62+
mm.credentials = creds
63+
return &mm
64+
}
65+
66+
func (m *meta) WithUserAgent(userAgent string) Meta {
67+
mm := *m
68+
mm.userAgent = userAgent
69+
return &mm
4570
}
4671

4772
func (m *meta) meta(ctx context.Context) (_ metadata.MD, err error) {
@@ -60,6 +85,11 @@ func (m *meta) meta(ctx context.Context) (_ metadata.MD, err error) {
6085
md.Set(metaRequestType, m.requestsType)
6186
}
6287
}
88+
if m.userAgent != "" {
89+
if len(md.Get(metaUserAgent)) == 0 {
90+
md.Set(metaUserAgent, m.userAgent)
91+
}
92+
}
6393
if m.credentials == nil {
6494
return md, nil
6595
}

internal/proxy/scripting.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type proxyScripting struct {
1414
meta meta.Meta
1515
}
1616

17-
func (d *proxyScripting) ExecuteYql(
17+
func (d *proxyScripting) Execute(
1818
ctx context.Context,
1919
query string,
2020
params *table.QueryParameters,
@@ -23,10 +23,10 @@ func (d *proxyScripting) ExecuteYql(
2323
if err != nil {
2424
return nil, err
2525
}
26-
return d.client.ExecuteYql(ctx, query, params)
26+
return d.client.Execute(ctx, query, params)
2727
}
2828

29-
func (d *proxyScripting) ExplainYql(
29+
func (d *proxyScripting) Explain(
3030
ctx context.Context,
3131
query string,
3232
mode scripting.ExplainMode,
@@ -35,10 +35,10 @@ func (d *proxyScripting) ExplainYql(
3535
if err != nil {
3636
return e, err
3737
}
38-
return d.client.ExplainYql(ctx, query, mode)
38+
return d.client.Explain(ctx, query, mode)
3939
}
4040

41-
func (d *proxyScripting) StreamExecuteYql(
41+
func (d *proxyScripting) StreamExecute(
4242
ctx context.Context,
4343
query string,
4444
params *table.QueryParameters,
@@ -47,7 +47,7 @@ func (d *proxyScripting) StreamExecuteYql(
4747
if err != nil {
4848
return nil, err
4949
}
50-
return d.client.StreamExecuteYql(ctx, query, params)
50+
return d.client.StreamExecute(ctx, query, params)
5151
}
5252

5353
func Scripting(client scripting.Client, meta meta.Meta) scripting.Client {

internal/scripting/scripting.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ import (
1212

1313
"github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
1414
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table/scanner"
15+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
1516
"github.com/ydb-platform/ydb-go-sdk/v3/scripting"
1617
"github.com/ydb-platform/ydb-go-sdk/v3/table"
1718
"github.com/ydb-platform/ydb-go-sdk/v3/table/result"
19+
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
1820
)
1921

2022
type client struct {
2123
service Ydb_Scripting_V1.ScriptingServiceClient
2224
}
2325

24-
func (c *client) ExecuteYql(
26+
func (c *client) Execute(
2527
ctx context.Context,
2628
query string,
2729
params *table.QueryParameters,
@@ -53,11 +55,11 @@ func mode2mode(mode scripting.ExplainMode) Ydb_Scripting.ExplainYqlRequest_Mode
5355
}
5456
}
5557

56-
func (c *client) ExplainYql(
58+
func (c *client) Explain(
5759
ctx context.Context,
5860
query string,
5961
mode scripting.ExplainMode,
60-
) (_ table.ScriptingYQLExplanation, err error) {
62+
) (e table.ScriptingYQLExplanation, err error) {
6163
var (
6264
request = &Ydb_Scripting.ExplainYqlRequest{
6365
Script: query,
@@ -74,14 +76,20 @@ func (c *client) ExplainYql(
7476
if err != nil {
7577
return
7678
}
77-
return table.ScriptingYQLExplanation{
79+
result.GetParametersTypes()
80+
e = table.ScriptingYQLExplanation{
7881
Explanation: table.Explanation{
7982
Plan: result.GetPlan(),
8083
},
81-
}, nil
84+
ParameterTypes: make(map[string]types.Type, len(result.GetParametersTypes())),
85+
}
86+
for k, v := range result.GetParametersTypes() {
87+
e.ParameterTypes[k] = value.TypeFromYDB(v)
88+
}
89+
return e, nil
8290
}
8391

84-
func (c *client) StreamExecuteYql(
92+
func (c *client) StreamExecute(
8593
ctx context.Context,
8694
query string,
8795
params *table.QueryParameters,

internal/value/type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
type T interface {
11+
String() string
1112
toYDB() *Ydb.Type
1213
toString(*bytes.Buffer)
1314
equal(T) bool

options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ func WithAccessTokenCredentials(accessToken string) Option {
3131
)
3232
}
3333

34+
func WithUserAgent(userAgent string) Option {
35+
return func(ctx context.Context, c *connection) error {
36+
c.options = append(c.options, config.WithUserAgent(userAgent))
37+
return nil
38+
}
39+
}
40+
3441
func WithConnectionString(dsn string) Option {
3542
return func(ctx context.Context, c *connection) error {
3643
params, err := ConnectionString(dsn)

0 commit comments

Comments
 (0)