Skip to content

Commit

Permalink
Merge pull request #1126 from brojeg/master
Browse files Browse the repository at this point in the history
Enable funlen linter
  • Loading branch information
asmyasnikov authored Mar 27, 2024
2 parents e66f15b + b837471 commit a0abd92
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 70 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ issues:
- predeclared
- path: _test\.go
linters:
- funlen
- unused
- unparam
- gocritic
Expand Down
63 changes: 31 additions & 32 deletions internal/bind/numeric_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"unicode/utf8"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/params"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
Expand All @@ -16,9 +17,7 @@ func (m NumericArgs) blockID() blockID {
return blockYQL
}

func (m NumericArgs) RewriteQuery(sql string, args ...interface{}) (
yql string, newArgs []interface{}, err error,
) {
func (m NumericArgs) RewriteQuery(sql string, args ...interface{}) (yql string, newArgs []interface{}, err error) {
l := &sqlLexer{
src: sql,
stateFn: numericArgsStateFn,
Expand All @@ -29,14 +28,18 @@ func (m NumericArgs) RewriteQuery(sql string, args ...interface{}) (
l.stateFn = l.stateFn(l)
}

var (
buffer = xstring.Buffer()
param table.ParameterOption
)
buffer := xstring.Buffer()
defer buffer.Free()

if len(args) > 0 {
newArgs = make([]interface{}, len(args))
parameters, err := parsePositionalParameters(args)
if err != nil {
return "", nil, err
}
newArgs = make([]interface{}, len(parameters))
for i, param := range parameters {
newArgs[i] = param
}
}

for _, p := range l.parts {
Expand All @@ -49,38 +52,20 @@ func (m NumericArgs) RewriteQuery(sql string, args ...interface{}) (
}
if int(p) > len(args) {
return "", nil, xerrors.WithStackTrace(
fmt.Errorf("%w: $p%d, len(args) = %d", ErrInconsistentArgs, p, len(args)),
fmt.Errorf("%w: $%d, len(args) = %d", ErrInconsistentArgs, p, len(args)),
)
}
paramName := "$p" + strconv.Itoa(int(p-1)) //nolint:goconst
if newArgs[p-1] == nil {
param, err = toYdbParam(paramName, args[p-1])
if err != nil {
return "", nil, xerrors.WithStackTrace(err)
}
newArgs[p-1] = param
buffer.WriteString(param.Name())
} else {
buffer.WriteString(newArgs[p-1].(table.ParameterOption).Name())
}
}
}

for i, p := range newArgs {
if p == nil {
return "", nil, xerrors.WithStackTrace(
fmt.Errorf("%w: $p%d, len(args) = %d", ErrInconsistentArgs, i+1, len(args)),
)
paramIndex := int(p - 1)
buffer.WriteString(newArgs[paramIndex].(table.ParameterOption).Name())
}
}

yql = buffer.String()
if len(newArgs) > 0 {
const prefix = "-- origin query with numeric args replacement\n"

return prefix + buffer.String(), newArgs, nil
yql = "-- origin query with numeric args replacement\n" + yql
}

return buffer.String(), newArgs, nil
return yql, newArgs, nil
}

func numericArgsStateFn(l *sqlLexer) stateFn {
Expand Down Expand Up @@ -130,6 +115,20 @@ func numericArgsStateFn(l *sqlLexer) stateFn {
}
}

func parsePositionalParameters(args []interface{}) ([]*params.Parameter, error) {
newArgs := make([]*params.Parameter, len(args))
for i, arg := range args {
paramName := fmt.Sprintf("$p%d", i)
param, err := toYdbParam(paramName, arg)
if err != nil {
return nil, err
}
newArgs[i] = param
}

return newArgs, nil
}

func numericArgState(l *sqlLexer) stateFn {
numbers := ""
defer func() {
Expand Down
84 changes: 46 additions & 38 deletions internal/bind/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
errMultipleQueryParameters = errors.New("only one query arg *table.QueryParameters allowed")
)

//nolint:gocyclo
//nolint:gocyclo,funlen
func toValue(v interface{}) (_ types.Value, err error) {
if valuer, ok := v.(driver.Valuer); ok {
v, err = valuer.Value()
Expand Down Expand Up @@ -174,62 +174,70 @@ func toYdbParam(name string, value interface{}) (*params.Parameter, error) {
return params.Named(name, v), nil
}

func Params(args ...interface{}) (parameters []*params.Parameter, _ error) {
parameters = make([]*params.Parameter, 0, len(args))
func Params(args ...interface{}) ([]*params.Parameter, error) {
parameters := make([]*params.Parameter, 0, len(args))
for i, arg := range args {
var newParam *params.Parameter
var newParams []*params.Parameter
var err error
switch x := arg.(type) {
case driver.NamedValue:
if x.Name == "" {
switch xx := x.Value.(type) {
case *params.Parameters:
if len(args) > 1 {
return nil, xerrors.WithStackTrace(errMultipleQueryParameters)
}
parameters = *xx
case *params.Parameter:
parameters = append(parameters, xx)
default:
x.Name = fmt.Sprintf("$p%d", i)
param, err := toYdbParam(x.Name, x.Value)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
parameters = append(parameters, param)
}
} else {
param, err := toYdbParam(x.Name, x.Value)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
parameters = append(parameters, param)
}
newParams, err = paramHandleNamedValue(x, i, len(args))
case sql.NamedArg:
if x.Name == "" {
return nil, xerrors.WithStackTrace(errUnnamedParam)
}
param, err := toYdbParam(x.Name, x.Value)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
parameters = append(parameters, param)
newParam, err = toYdbParam(x.Name, x.Value)
newParams = append(newParams, newParam)
case *params.Parameters:
if len(args) > 1 {
return nil, xerrors.WithStackTrace(errMultipleQueryParameters)
}
parameters = *x
case *params.Parameter:
parameters = append(parameters, x)
newParams = append(newParams, x)
default:
param, err := toYdbParam(fmt.Sprintf("$p%d", i), x)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
parameters = append(parameters, param)
newParam, err = toYdbParam(fmt.Sprintf("$p%d", i), x)
newParams = append(newParams, newParam)
}
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
parameters = append(parameters, newParams...)
}
sort.Slice(parameters, func(i, j int) bool {
return parameters[i].Name() < parameters[j].Name()
})

return parameters, nil
}

func paramHandleNamedValue(arg driver.NamedValue, paramNumber, argsLen int) ([]*params.Parameter, error) {
if arg.Name == "" {
switch x := arg.Value.(type) {
case *params.Parameters:
if argsLen > 1 {
return nil, xerrors.WithStackTrace(errMultipleQueryParameters)
}

return *x, nil
case *params.Parameter:
return []*params.Parameter{x}, nil
default:
arg.Name = fmt.Sprintf("$p%d", paramNumber)
param, err := toYdbParam(arg.Name, arg.Value)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}

return []*params.Parameter{param}, nil
}
} else {
param, err := toYdbParam(arg.Name, arg.Value)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}

return []*params.Parameter{param}, nil
}
}
1 change: 1 addition & 0 deletions internal/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ func (c *conn) Invoke(
return err
}

//nolint:funlen
func (c *conn) NewStream(
ctx context.Context,
desc *grpc.StreamDesc,
Expand Down

0 comments on commit a0abd92

Please sign in to comment.