Skip to content

Commit

Permalink
changed in ydb.ParamsBuilder() container type constructors from `Li…
Browse files Browse the repository at this point in the history
…st()...Build()` to `BeginList()...EndList()` (`Dict`, `Set`, `Optional`)
  • Loading branch information
asmyasnikov committed Mar 14, 2024
1 parent f1a46a6 commit fad3140
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 87 deletions.
65 changes: 65 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ydb_test
import (
"context"
"database/sql"
"errors"
"fmt"
"io"
"log"
Expand All @@ -14,13 +15,77 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
"github.com/ydb-platform/ydb-go-sdk/v3/config"
"github.com/ydb-platform/ydb-go-sdk/v3/query"
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions"
)

//nolint:testableexamples, nonamedreturns
func Example_query() {
ctx := context.TODO()
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
log.Fatal(err)
}
defer db.Close(ctx) // cleanup resources

err = db.Query().Do( // Do retry operation on errors with best effort
ctx, // context manage exiting from Do
func(ctx context.Context, s query.Session) (err error) { // retry operation
_, res, err := s.Execute(ctx,
`SELECT $id as myId, $str as myStr`,
query.WithParameters(
ydb.ParamsBuilder().
Param("$id").Uint64(42).
Param("$str").Text("my string").
Build(),
),
)
if err != nil {
return err // for auto-retry with driver
}
defer func() { _ = res.Close(ctx) }() // cleanup resources
for { // iterate over result sets
rs, err := res.NextResultSet(ctx)
if err != nil {
if errors.Is(err, io.EOF) {
break
}

return err
}
for { // iterate over rows
row, err := rs.NextRow(ctx)
if err != nil {
if errors.Is(err, io.EOF) {
break
}

return err
}
type myStruct struct {
id uint64 `sql:"id"`
str string `sql:"myStr"`
}
var s myStruct
if err = row.ScanStruct(&s); err != nil {
return err // generally scan error not retryable, return it for driver check error
}
}
}

return res.Err() // return finally result error for auto-retry with driver
},
query.WithIdempotent(),
)
if err != nil {
log.Printf("unexpected error: %v", err)
}
}

//nolint:testableexamples, nonamedreturns
func Example_table() {
ctx := context.TODO()
Expand Down
2 changes: 1 addition & 1 deletion internal/params/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func TestBuilder(t *testing.T) {
{
name: xtest.CurrentFileLine(),
builder: Builder{}.
Param("$x").List().Build().
Param("$x").BeginList().EndList().
Build(),
params: map[string]*Ydb.TypedValue{
"$x": {
Expand Down
4 changes: 2 additions & 2 deletions internal/params/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type (
}
)

func (d *dict) Pair() *dictPair {
func (d *dict) Add() *dictPair {
return &dictPair{
parent: d,
}
Expand Down Expand Up @@ -407,7 +407,7 @@ func (d *dictValue) UUID(v [16]byte) *dict {
return d.pair.parent
}

func (d *dict) Build() Builder {
func (d *dict) EndDict() Builder {
d.parent.params = append(d.parent.params, &Parameter{
parent: d.parent,
name: d.name,
Expand Down
6 changes: 3 additions & 3 deletions internal/params/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,15 @@ func TestDict(t *testing.T) {
a := allocator.New()
defer a.Free()

item := Builder{}.Param("$x").Dict().Pair()
item := Builder{}.Param("$x").BeginDict().Add()

addedKey, ok := xtest.CallMethod(item, key.method, key.args...)[0].(*dictValue)
require.True(t, ok)

d, ok := xtest.CallMethod(addedKey, val.method, val.args...)[0].(*dict)
require.True(t, ok)

params := d.Build().Build().ToYDB(a)
params := d.EndDict().Build().ToYDB(a)
require.Equal(t, paramsToJSON(
map[string]*Ydb.TypedValue{
"$x": {
Expand Down Expand Up @@ -420,7 +420,7 @@ func TestDict_AddPairs(t *testing.T) {
},
}

params := Builder{}.Param("$x").Dict().AddPairs(pairs...).Build().Build().ToYDB(a)
params := Builder{}.Param("$x").BeginDict().AddPairs(pairs...).EndDict().Build().ToYDB(a)

require.Equal(t, paramsToJSON(
map[string]*Ydb.TypedValue{
Expand Down
4 changes: 2 additions & 2 deletions internal/params/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type (
}
)

func (l *list) AddItem() *listItem {
func (l *list) Add() *listItem {
return &listItem{
parent: l,
}
Expand All @@ -29,7 +29,7 @@ func (l *list) AddItems(items ...value.Value) *list {
return l
}

func (l *list) Build() Builder {
func (l *list) EndList() Builder {
l.parent.params = append(l.parent.params, &Parameter{
parent: l.parent,
name: l.name,
Expand Down
48 changes: 24 additions & 24 deletions internal/params/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestList(t *testing.T) {
}{
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Uint64(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Uint64(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Int64(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Int64(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Uint32(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Uint32(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Int32(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Int32(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Uint16(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Uint16(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Int16(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Int16(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Uint8(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Uint8(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Int8(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Int8(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Bool(true).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Bool(true).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -272,7 +272,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Text("test").Build(),
builder: Builder{}.Param("$x").BeginList().Add().Text("test").EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -300,7 +300,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Bytes([]byte("test")).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Bytes([]byte("test")).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -328,7 +328,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Float(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Float(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Double(123).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Double(123).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -384,7 +384,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Interval(time.Second).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Interval(time.Second).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -412,7 +412,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Datetime(time.Unix(123456789, 456)).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Datetime(time.Unix(123456789, 456)).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -440,7 +440,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Date(time.Unix(123456789, 456)).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Date(time.Unix(123456789, 456)).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -468,7 +468,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Timestamp(time.Unix(123456789, 456)).Build(),
builder: Builder{}.Param("$x").BeginList().Add().Timestamp(time.Unix(123456789, 456)).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -496,7 +496,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().Decimal([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}, 22, 9).Build(), //nolint:lll
builder: Builder{}.Param("$x").BeginList().Add().Decimal([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}, 22, 9).EndList(), //nolint:lll
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -528,7 +528,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().JSON(`{"a": 1,"b": "B"}`).Build(),
builder: Builder{}.Param("$x").BeginList().Add().JSON(`{"a": 1,"b": "B"}`).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -556,7 +556,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().JSONDocument(`{"a": 1,"b": "B"}`).Build(),
builder: Builder{}.Param("$x").BeginList().Add().JSONDocument(`{"a": 1,"b": "B"}`).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -584,7 +584,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().YSON([]byte(`[ 1; 2; 3; 4; 5 ]`)).Build(),
builder: Builder{}.Param("$x").BeginList().Add().YSON([]byte(`[ 1; 2; 3; 4; 5 ]`)).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -612,8 +612,8 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItem().
UUID([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}).Build(),
builder: Builder{}.Param("$x").BeginList().Add().
UUID([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down Expand Up @@ -642,7 +642,7 @@ func TestList(t *testing.T) {
},
{
name: xtest.CurrentFileLine(),
builder: Builder{}.Param("$x").List().AddItems(value.Uint64Value(123), value.Uint64Value(321)).Build(),
builder: Builder{}.Param("$x").BeginList().AddItems(value.Uint64Value(123), value.Uint64Value(321)).EndList(),
params: map[string]*Ydb.TypedValue{
"$x": {
Type: &Ydb.Type{
Expand Down
2 changes: 1 addition & 1 deletion internal/params/optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type (
}
)

func (b *optionalBuilder) Build() Builder {
func (b *optionalBuilder) EndOptional() Builder {
b.opt.parent.params = append(b.opt.parent.params, &Parameter{
parent: b.opt.parent,
name: b.opt.name,
Expand Down
Loading

0 comments on commit fad3140

Please sign in to comment.