Skip to content

Support raw protobuf ydb types #1787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/bind/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"encoding/json"
"errors"
"fmt"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"

Check failure on line 9 in internal/bind/params.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/ydb-platform/ydb-go-sdk/v3) (gci)
"net/url"
"reflect"
"sort"
"time"

Check failure on line 13 in internal/bind/params.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 14 in internal/bind/params.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `goimports`-ed with -local github.com/ydb-platform/ydb-go-sdk/v3 (goimports)
"github.com/google/uuid"

Check failure on line 15 in internal/bind/params.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/ydb-platform/ydb-go-sdk/v3) (gci)

"github.com/ydb-platform/ydb-go-sdk/v3/internal/params"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
Expand Down Expand Up @@ -212,6 +213,10 @@
return value.VoidValue(), nil
case value.Value:
return x, nil
case Ydb.TypedValue:
return value.FromProtobuf(&x), nil
case *Ydb.TypedValue:
return value.FromProtobuf(x), nil
case bool:
return value.BoolValue(x), nil
case int:
Expand Down
7 changes: 7 additions & 0 deletions internal/params/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ func (p *Parameter) TzDatetime(v time.Time) Builder {
}
}

// Raw makes value from raw protobuf
// Deprecated: use FromProtobuf instead
func (p *Parameter) Raw(pb *Ydb.TypedValue) Builder {
return p.FromProtobuf(pb)
}

// FromProtobuf makes value from raw protobuf
func (p *Parameter) FromProtobuf(pb *Ydb.TypedValue) Builder {
p.value = value.FromProtobuf(pb)

return Builder{
Expand Down
38 changes: 34 additions & 4 deletions internal/value/cast.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
package value

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

func CastTo(v Value, dst interface{}) error {
if dst == nil {
return errNilDestination
}
if ptr, has := dst.(*Value); has {
*ptr = v

Check failure on line 11 in internal/value/cast.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofmt`-ed with `-s` (gofmt)
switch x := dst.(type) {
case *Value:
*x = v

return nil
}
case **Value:
**x = v

return nil
case *Ydb.Value:
*x = *v.toYDB()

return nil
case **Ydb.Value:
*x = v.toYDB()

return v.castTo(dst)
return nil
case *Ydb.TypedValue:
x.Type = v.Type().ToYDB()
x.Value = v.toYDB()

return nil
case **Ydb.TypedValue:
*x = &Ydb.TypedValue{
Type: v.Type().ToYDB(),
Value: v.toYDB(),
}

return nil
default:
return v.castTo(dst)
}
}
14 changes: 7 additions & 7 deletions params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ func makeParamsUsingParamsBuilder(tb testing.TB) params.Parameters {
Build()
}

func makeParamsUsingRawProtobuf(tb testing.TB) params.Parameters {
func makeParamsFromProtobuf(tb testing.TB) params.Parameters {
return ydb.ParamsBuilder().
Param("$a").Raw(a).
Param("$b").Raw(b).
Param("$c").Raw(c).
Param("$d").Raw(d).
Param("$a").FromProtobuf(a).
Param("$b").FromProtobuf(b).
Param("$c").FromProtobuf(c).
Param("$d").FromProtobuf(d).
Build()
}

Expand Down Expand Up @@ -153,7 +153,7 @@ func TestParams(t *testing.T) {
require.NoError(t, err)
require.Equal(t, fmt.Sprint(exp), fmt.Sprint(pb))
t.Run("Raw", func(t *testing.T) {
params := makeParamsUsingRawProtobuf(t)
params := makeParamsFromProtobuf(t)
pb, err := params.ToYDB()
require.NoError(t, err)
require.Equal(t, fmt.Sprint(exp), fmt.Sprint(pb))
Expand Down Expand Up @@ -184,7 +184,7 @@ func BenchmarkParams(b *testing.B) {
b.Run("RawProtobuf", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
params := makeParamsUsingRawProtobuf(b)
params := makeParamsFromProtobuf(b)
_, _ = params.ToYDB()
}
})
Expand Down
99 changes: 99 additions & 0 deletions tests/integration/param_raw_protobuf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//go:build integration
// +build integration

package integration

import (
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
"github.com/ydb-platform/ydb-go-sdk/v3/query"
"testing"

"github.com/stretchr/testify/require"

"github.com/ydb-platform/ydb-go-sdk/v3"
)

func TestRawProtobuf(t *testing.T) {
raw := &Ydb.TypedValue{
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{
TypeId: Ydb.Type_UINT64,
},
},
Value: &Ydb.Value{
Value: &Ydb.Value_Uint64Value{
Uint64Value: 123,
},
},
}

t.Run("query", func(t *testing.T) {
var (
scope = newScope(t)
db = scope.Driver()
)

row, err := db.Query().QueryRow(scope.Ctx, `
DECLARE $raw AS Uint64;
SELECT $raw;`,
query.WithParameters(
ydb.ParamsBuilder().Param("$raw").FromProtobuf(raw).Build(),
),
)
require.NoError(t, err)

t.Run("*Ydb.TypedValue", func(t *testing.T) {
var act *Ydb.TypedValue
require.NoError(t, row.Scan(&act))
require.Equal(t, raw.String(), act.String())
})
t.Run("**Ydb.TypedValue", func(t *testing.T) {
var act *Ydb.TypedValue
require.NoError(t, row.Scan(&act))
require.Equal(t, raw.String(), act.String())
})
t.Run("*Ydb.Value", func(t *testing.T) {
var act *Ydb.Value
require.NoError(t, row.Scan(&act))
require.Equal(t, raw.Value.String(), act.String())
})
t.Run("**Ydb.Value", func(t *testing.T) {
var act *Ydb.Value
require.NoError(t, row.Scan(&act))
require.Equal(t, raw.Value.String(), act.String())
})
})
t.Run("database/sql", func(t *testing.T) {
var (
scope = newScope(t)
db = scope.SQLDriver(
ydb.WithAutoDeclare(),
ydb.WithPositionalArgs(),
)
)

row := db.QueryRowContext(scope.Ctx, `SELECT ?`, raw)
require.NoError(t, row.Err())

t.Run("*Ydb.TypedValue", func(t *testing.T) {
var act *Ydb.TypedValue
require.NoError(t, row.Scan(&act))
require.Equal(t, raw.String(), act.String())
})
t.Run("**Ydb.TypedValue", func(t *testing.T) {
var act *Ydb.TypedValue
require.NoError(t, row.Scan(&act))
require.Equal(t, raw.String(), act.String())
})
t.Run("*Ydb.Value", func(t *testing.T) {
var act *Ydb.Value
require.NoError(t, row.Scan(&act))
require.Equal(t, raw.Value.String(), act.String())
})
t.Run("**Ydb.Value", func(t *testing.T) {
var act *Ydb.Value
require.NoError(t, row.Scan(&act))
require.Equal(t, raw.Value.String(), act.String())
})
})
}
Loading