Skip to content

Commit 86eeb9a

Browse files
authored
Merge pull request #1575 from ydb-platform/params-from-map
Marked as deprecated `ydb.MustParamsFromMap` + Changed result `ydb.ParamsFromMap` result from tuple <`params.Parameters`, `error`> to `params.Parameters` only
2 parents 761406e + 9dbc127 commit 86eeb9a

32 files changed

+197
-112
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Removed experimental helper `ydb.MustParamsFromMap`
2+
* Changed result of experimental helper `ydb.ParamsFromMap` from tuple <`params.Parameters`, `error`> to `params.Parameters` only
3+
14
## v3.93.1
25
* Published `query.ExecuteOption` as alias to `internal/query/options.Execute`
36

example_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,7 @@ func ExampleOpen_advanced() {
511511
fmt.Printf("connected to %s, database '%s'", db.Endpoint(), db.Name())
512512
}
513513

514-
// func ExampleParamsMap
515-
func ExampleMustParamsFromMap() {
514+
func ExampleParamsFromMap() {
516515
ctx := context.TODO()
517516
db, err := ydb.Open(
518517
ctx,
@@ -532,14 +531,16 @@ func ExampleMustParamsFromMap() {
532531
fmt.Printf("connected to %s, database '%s'", db.Endpoint(), db.Name())
533532

534533
res, err := db.Query().QueryRow(ctx, `
535-
DECLARE $textArg AS Text;
536-
DECLARE $intArg AS Int64;
537-
538-
SELECT $textArg AS TextField, $intArg AS IntField
539-
`, query.WithParameters(ydb.MustParamsFromMap(map[string]any{
540-
"$textArg": "asd",
541-
"$intArg": int64(123),
542-
})))
534+
DECLARE $textArg AS Text;
535+
DECLARE $intArg AS Int64;
536+
537+
SELECT $textArg AS TextField, $intArg AS IntField
538+
`,
539+
query.WithParameters(ydb.ParamsFromMap(map[string]any{
540+
"$textArg": "asd",
541+
"$intArg": int64(123),
542+
})),
543+
)
543544
if err != nil {
544545
fmt.Printf("query failed")
545546
}

internal/bind/params.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func Params(args ...interface{}) ([]*params.Parameter, error) {
204204
}
205205
newParam, err = toYdbParam(x.Name, x.Value)
206206
newParams = append(newParams, newParam)
207-
case *params.Parameters:
207+
case *params.Params:
208208
if len(args) > 1 {
209209
return nil, xerrors.WithStackTrace(errMultipleQueryParameters)
210210
}
@@ -230,7 +230,7 @@ func Params(args ...interface{}) ([]*params.Parameter, error) {
230230
func paramHandleNamedValue(arg driver.NamedValue, paramNumber, argsLen int) ([]*params.Parameter, error) {
231231
if arg.Name == "" {
232232
switch x := arg.Value.(type) {
233-
case *params.Parameters:
233+
case *params.Params:
234234
if argsLen > 1 {
235235
return nil, xerrors.WithStackTrace(errMultipleQueryParameters)
236236
}

internal/params/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package params
22

33
type (
44
Builder struct {
5-
params Parameters
5+
params Params
66
}
77
)
88

9-
func (b Builder) Build() *Parameters {
9+
func (b Builder) Build() *Params {
1010
return &b.params
1111
}
1212

internal/params/builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func TestBuilder(t *testing.T) {
417417
result, ok := xtest.CallMethod(item, tc.method, tc.args...)[0].(Builder)
418418
require.True(t, ok)
419419

420-
params := result.Build().ToYDB(a)
420+
params := result.Build().toYDB(a)
421421

422422
require.Equal(t,
423423
xtest.ToJSON(

internal/params/dict_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func TestDict(t *testing.T) {
440440
d, ok := xtest.CallMethod(addedKey, val.method, val.args...)[0].(*dict)
441441
require.True(t, ok)
442442

443-
params := d.EndDict().Build().ToYDB(a)
443+
params := d.EndDict().Build().toYDB(a)
444444
require.Equal(t, xtest.ToJSON(
445445
map[string]*Ydb.TypedValue{
446446
"$x": {
@@ -482,7 +482,7 @@ func TestDict_AddPairs(t *testing.T) {
482482
},
483483
}
484484

485-
params := Builder{}.Param("$x").BeginDict().AddPairs(pairs...).EndDict().Build().ToYDB(a)
485+
params := Builder{}.Param("$x").BeginDict().AddPairs(pairs...).EndDict().Build().toYDB(a)
486486

487487
require.Equal(t, xtest.ToJSON(
488488
map[string]*Ydb.TypedValue{

internal/params/list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ func TestList(t *testing.T) {
435435
result, ok := xtest.CallMethod(item, tc.method, tc.args...)[0].(*list)
436436
require.True(t, ok)
437437

438-
params := result.EndList().Build().ToYDB(a)
438+
params := result.EndList().Build().toYDB(a)
439439
require.Equal(t, xtest.ToJSON(
440440
map[string]*Ydb.TypedValue{
441441
"$x": {
@@ -462,7 +462,7 @@ func TestList_AddItems(t *testing.T) {
462462
defer a.Free()
463463
params := Builder{}.Param("$x").BeginList().
464464
AddItems(value.Uint64Value(123), value.Uint64Value(321)).
465-
EndList().Build().ToYDB(a)
465+
EndList().Build().toYDB(a)
466466
require.Equal(t, xtest.ToJSON(
467467
map[string]*Ydb.TypedValue{
468468
"$x": {

internal/params/optional_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ func TestOptional(t *testing.T) {
435435
result, ok := xtest.CallMethod(item, tc.method, tc.args...)[0].(*optionalBuilder)
436436
require.True(t, ok)
437437

438-
params := result.EndOptional().Build().ToYDB(a)
438+
params := result.EndOptional().Build().toYDB(a)
439439
require.Equal(t, xtest.ToJSON(
440440
map[string]*Ydb.TypedValue{
441441
"$x": {

internal/params/parameters.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ type (
2323
name string
2424
value value.Value
2525
}
26-
Parameters []*Parameter
26+
Parameters interface {
27+
ToYDB(a *allocator.Allocator) (map[string]*Ydb.TypedValue, error)
28+
}
29+
Params []*Parameter
2730
)
2831

32+
var _ Parameters = (*Params)(nil)
33+
2934
func Named(name string, value value.Value) *Parameter {
3035
return &Parameter{
3136
name: name,
@@ -41,7 +46,7 @@ func (p *Parameter) Value() value.Value {
4146
return p.value
4247
}
4348

44-
func (p *Parameters) String() string {
49+
func (p *Params) String() string {
4550
buffer := xstring.Buffer()
4651
defer buffer.Free()
4752

@@ -62,7 +67,20 @@ func (p *Parameters) String() string {
6267
return buffer.String()
6368
}
6469

65-
func (p *Parameters) ToYDB(a *allocator.Allocator) map[string]*Ydb.TypedValue {
70+
func (p *Params) ToYDB(a *allocator.Allocator) (map[string]*Ydb.TypedValue, error) {
71+
if p == nil {
72+
return nil, nil //nolint:nilnil
73+
}
74+
75+
parameters := make(map[string]*Ydb.TypedValue, len(*p))
76+
for _, param := range *p {
77+
parameters[param.name] = value.ToYDB(param.value, a)
78+
}
79+
80+
return parameters, nil
81+
}
82+
83+
func (p *Params) toYDB(a *allocator.Allocator) map[string]*Ydb.TypedValue {
6684
if p == nil {
6785
return nil
6886
}
@@ -74,7 +92,7 @@ func (p *Parameters) ToYDB(a *allocator.Allocator) map[string]*Ydb.TypedValue {
7492
return parameters
7593
}
7694

77-
func (p *Parameters) Each(it func(name string, v value.Value)) {
95+
func (p *Params) Each(it func(name string, v value.Value)) {
7896
if p == nil {
7997
return
8098
}
@@ -83,15 +101,15 @@ func (p *Parameters) Each(it func(name string, v value.Value)) {
83101
}
84102
}
85103

86-
func (p *Parameters) Count() int {
104+
func (p *Params) Count() int {
87105
if p == nil {
88106
return 0
89107
}
90108

91109
return len(*p)
92110
}
93111

94-
func (p *Parameters) Add(params ...NamedValue) {
112+
func (p *Params) Add(params ...NamedValue) {
95113
for _, param := range params {
96114
*p = append(*p, Named(param.Name(), param.Value()))
97115
}

internal/params/parameters_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestParameter(t *testing.T) {
1818
}
1919

2020
func TestParameters(t *testing.T) {
21-
p := &Parameters{}
21+
p := &Params{}
2222
p.Add(
2323
Named("x", value.TextValue("X")),
2424
Named("y", value.TextValue("Y")),
@@ -39,15 +39,15 @@ func TestParameters(t *testing.T) {
3939
func TestNil(t *testing.T) {
4040
for _, tt := range []struct {
4141
name string
42-
p *Parameters
42+
p *Params
4343
}{
4444
{
4545
name: xtest.CurrentFileLine(),
4646
p: nil,
4747
},
4848
{
4949
name: xtest.CurrentFileLine(),
50-
p: &Parameters{},
50+
p: &Params{},
5151
},
5252
{
5353
name: xtest.CurrentFileLine(),
@@ -64,7 +64,7 @@ func TestNil(t *testing.T) {
6464
require.Empty(t, visited)
6565
a := allocator.New()
6666
defer a.Free()
67-
require.Empty(t, tt.p.ToYDB(a))
67+
require.Empty(t, tt.p.toYDB(a))
6868
})
6969
}
7070
}

0 commit comments

Comments
 (0)