Skip to content
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

Enable funlen linter (part 4) #1224

Merged
merged 2 commits into from
May 15, 2024
Merged
Changes from 1 commit
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
104 changes: 46 additions & 58 deletions internal/cmd/gtrace/writer.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tool gtrace no need to change because it is an ad-hoc tool

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the breaking action was broken with your PR - because you change a code of gtrace tool

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the review. I've rolled back [writer.go] and added //nolint:funlen where needed.

Original file line number Diff line number Diff line change
@@ -372,6 +372,7 @@ func (w *Writer) composeHook(hook Hook, t1, t2, dst string) {
w.line(`}`)
}

//nolint:funlen
func (w *Writer) composeHookCall(fn *Func, h1, h2 string) {
w.newScope(func() {
w.capture(h1, h2)
@@ -385,70 +386,57 @@ func (w *Writer) composeHookCall(fn *Func, h1, h2 string) {
w.funcResults(fn)
w.line(` {`)
w.line(`if options.panicCallback != nil {`)
w.addPanicCallback()
w.line("}")
if fn.HasResult() {
rs := w.declareResults(fn)
w.addHookCalls(fn, h1, h2, args, rs)
w.addReturnStatement(fn, rs)
} else {
w.addHookCalls(fn, h1, h2, args, nil)
}
w.line(`}`)
})
})
}

func (w *Writer) addPanicCallback() {
w.block(func() {
w.line("defer func() {")
w.block(func() {
w.line("if e := recover(); e != nil {")
w.block(func() {
w.line(`options.panicCallback(e)`)
w.line("defer func() {")
w.block(func() {
w.line("if e := recover(); e != nil {")
w.block(func() {
w.line(`options.panicCallback(e)`)
})
w.line("}")
})
w.line("}()")
})
w.line("}")
})
w.line("}()")
})
}

func (w *Writer) declareResults(fn *Func) []string {
r1 := w.declare("r")
r2 := w.declare("r")
rs := []string{r1, r2}
w.code("var " + r1 + ", " + r2 + " ")
w.funcResults(fn)
_ = w.bw.WriteByte('\n')
w.atEOL = true

return rs
}

func (w *Writer) addHookCalls(fn *Func, h1, h2 string, args, rs []string) {
for i, h := range []string{h1, h2} {
w.line("if " + h + " != nil {")
w.block(func() {
var (
r1 string
r2 string
rs []string
)
if fn.HasResult() {
r1 = w.declare("r")
r2 = w.declare("r")
rs = []string{r1, r2}
w.code("var " + r1 + ", " + r2 + " ")
w.funcResults(fn)
_ = w.bw.WriteByte('\n')
w.atEOL = true
}
for i, h := range []string{h1, h2} {
w.line("if " + h + " != nil {")
w.block(func() {
if fn.HasResult() {
w.code(rs[i], ` = `) //nolint:scopelint
}
w.code(h) //nolint:scopelint
w.call(args)
})
w.line("}")
}
if fn.HasResult() {
w.code(rs[i], ` = `) //nolint:scopelint
w.code(`return `)
switch x := fn.Result[0].(type) {
case *Func:
w.composeHookCall(x, r1, r2)
case *Trace:
w.line(r1, `.Compose(`, r2, `)`)
default:
panic("unknown result type")
}
}
w.code(h) //nolint:scopelint
w.call(args)
})
w.line("}")
}
}

func (w *Writer) addReturnStatement(fn *Func, rs []string) {
w.code(`return `)
switch x := fn.Result[0].(type) {
case *Func:
w.composeHookCall(x, rs[0], rs[1])
case *Trace:
w.line(rs[0], `.Compose(`, rs[1], `)`)
default:
panic("unknown result type")
}
w.line(`}`)
})
}

func (w *Writer) options(trace *Trace) {
4 changes: 1 addition & 3 deletions internal/credentials/static.go
Original file line number Diff line number Diff line change
@@ -87,9 +87,7 @@ func (c *Static) Token(ctx context.Context) (token string, err error) {
fmt.Errorf("dial failed: %w", err),
)
}
defer func() {
_ = cc.Close()
}()
defer cc.Close()

client := Ydb_Auth_V1.NewAuthServiceClient(cc)

2 changes: 1 addition & 1 deletion internal/value/nullable.go
Original file line number Diff line number Diff line change
@@ -299,7 +299,7 @@ func NullableDyNumberValue(v *string) Value {
// Nullable makes optional value from nullable type
// Warning: type interface will be replaced in the future with typed parameters pattern from go1.18
//
//nolint:gocyclo
//nolint:gocyclo, funlen
func Nullable(t types.Type, v interface{}) Value {
switch t {
case types.Bool:
3 changes: 3 additions & 0 deletions internal/value/value.go
Original file line number Diff line number Diff line change
@@ -78,6 +78,7 @@ func nullValueFromYDB(x *Ydb.Value, t types.Type) (_ Value, ok bool) {
}
}

//nolint:funlen
func primitiveValueFromYDB(t types.Primitive, v *Ydb.Value) (Value, error) {
switch t {
case types.Bool:
@@ -167,6 +168,7 @@ func primitiveValueFromYDB(t types.Primitive, v *Ydb.Value) (Value, error) {
}
}

//nolint:funlen
func fromYDB(t *Ydb.Type, v *Ydb.Value) (Value, error) {
tt := types.TypeFromYDB(t)

@@ -2307,6 +2309,7 @@ func YSONValue(v []byte) ysonValue {
return v
}

//nolint:funlen
func zeroPrimitiveValue(t types.Primitive) Value {
switch t {
case types.Bool:
2 changes: 2 additions & 0 deletions retry/sql.go
Original file line number Diff line number Diff line change
@@ -130,6 +130,8 @@ func WithTxOptions(txOptions *sql.TxOptions) txOptionsOption {
}

// DoTx is a retryer of database/sql transactions with fallbacks on errors
//
//nolint:funlen
func DoTx(ctx context.Context, db *sql.DB, op func(context.Context, *sql.Tx) error, opts ...doTxOption) error {
var (
options = doTxOptions{