Skip to content

enabled nonamedreturns linter #1033

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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ linters:
- ireturn
- maintidx
- maligned
- nonamedreturns
- paralleltest
- protogetter
- scopelint
Expand Down
5 changes: 3 additions & 2 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var (
}
)

func defaultGrpcOptions(t *trace.Driver, secure bool, tlsConfig *tls.Config) (opts []grpc.DialOption) {
func defaultGrpcOptions(t *trace.Driver, secure bool, tlsConfig *tls.Config) []grpc.DialOption {
var opts []grpc.DialOption
Copy link
Member

Choose a reason for hiding this comment

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

You can create a slice with predefined capacity
Because next line will re-allocate internal slice capacity

Copy link
Member

@asmyasnikov asmyasnikov Feb 20, 2024

Choose a reason for hiding this comment

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

Alternatively you can make slice from static
Such as

opts := []grpc.DialOption{
		grpc.WithKeepaliveParams(
			DefaultGrpcConnectionPolicy,
		),
		// use round robin balancing policy for fastest dialing
		grpc.WithDefaultServiceConfig(`{
			"loadBalancingPolicy": "round_robin"
		}`),
		// limit size of outgoing and incoming packages
		grpc.WithDefaultCallOptions(
			grpc.MaxCallRecvMsgSize(DefaultGRPCMsgSize),
			grpc.MaxCallSendMsgSize(DefaultGRPCMsgSize),
		),
		// use proxy-resolvers
		// 1) for interpret schemas `ydb`, `grpc` and `grpcs` in node URLs as for dns resolver
		// 2) for observe resolving events
		grpc.WithResolvers(
			xresolver.New("", t),
			xresolver.New("ydb", t),
			xresolver.New("grpc", t),
			xresolver.New("grpcs", t),
		),
}

But on next step (if secure) append will re-allocate slice to new size

Copy link
Author

Choose a reason for hiding this comment

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

I fixed this by adding the make with fixed capacity.

opts = append(opts,
// keep-aliving all connections
grpc.WithKeepaliveParams(
Expand Down Expand Up @@ -84,7 +85,7 @@ func defaultTLSConfig() *tls.Config {
}
}

func defaultConfig() (c *Config) {
func defaultConfig() *Config {
return &Config{
credentials: credentials.NewAnonymousCredentials(
credentials.WithSourceInfo(stack.Record(0)),
Expand Down
41 changes: 27 additions & 14 deletions examples/basic/database_sql/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func episodeData(seriesID, seasonID, episodeID, title string, date time.Time) ty
)
}

func getData() (series, seasons, episodes []types.Value) {
func getData() ([]types.Value, []types.Value, []types.Value) {
Copy link
Member

Choose a reason for hiding this comment

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

Read comment for getDataForITCrowd

Copy link
Author

Choose a reason for hiding this comment

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

OK. I returned previous declaration.
I simple followed by linter's comment.

var (
series = make([]types.Value, 0)
seasons = make([]types.Value, 0)
episodes = make([]types.Value, 0)
)
for seriesID, fill := range map[string]func(seriesID string) (
seriesData types.Value, seasons []types.Value, episodes []types.Value,
){
Expand All @@ -61,15 +66,19 @@ func getData() (series, seasons, episodes []types.Value) {
episodes = append(episodes, episodesData...)
}

return
return series, seasons, episodes
}

func getDataForITCrowd(seriesID string) (series types.Value, seasons, episodes []types.Value) {
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 this changes not required and harmful
Because if I call getDataForITCrowd - I want to know about each of three results.
Old variant helps to understand. New variant cannot helps

Copy link
Author

Choose a reason for hiding this comment

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

OK. I returned previous declaration.
I simple followed by linter's comment.

series = seriesData(
seriesID, date("2006-02-03"), "IT Crowd", ""+
"The IT Crowd is a British sitcom produced by Channel 4, written by Graham Linehan, produced by "+
"Ash Atalla and starring Chris O'Dowd, Richard Ayoade, Katherine Parkinson, and Matt Berry.",
"", // NULL comment.
func getDataForITCrowd(seriesID string) (types.Value, []types.Value, []types.Value) {
var (
series = seriesData(
seriesID, date("2006-02-03"), "IT Crowd", ""+
"The IT Crowd is a British sitcom produced by Channel 4, written by Graham Linehan, produced by "+
"Ash Atalla and starring Chris O'Dowd, Richard Ayoade, Katherine Parkinson, and Matt Berry.",
"", // NULL comment.
)
seasons = make([]types.Value, 0)
episodes = make([]types.Value, 0)
)
for _, season := range []struct { //nolint:gocritic
title string
Expand Down Expand Up @@ -120,12 +129,16 @@ func getDataForITCrowd(seriesID string) (series types.Value, seasons, episodes [
return series, seasons, episodes
}

func getDataForSiliconValley(seriesID string) (series types.Value, seasons, episodes []types.Value) {
Copy link
Member

Choose a reason for hiding this comment

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

Read previous comment

Copy link
Author

Choose a reason for hiding this comment

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

OK. I returned previous declaration.
I simple followed by linter's comment.

series = seriesData(
seriesID, date("2014-04-06"), "Silicon Valley", ""+
"Silicon Valley is an American comedy television series created by Mike Judge, John Altschuler and "+
"Dave Krinsky. The series focuses on five young men who founded a startup company in Silicon Valley.",
"Some comment here",
func getDataForSiliconValley(seriesID string) (types.Value, []types.Value, []types.Value) {
var (
series = seriesData(
seriesID, date("2014-04-06"), "Silicon Valley", ""+
"Silicon Valley is an American comedy television series created by Mike Judge, John Altschuler and "+
"Dave Krinsky. The series focuses on five young men who founded a startup company in Silicon Valley.",
"Some comment here",
)
seasons = make([]types.Value, 0)
episodes = make([]types.Value, 0)
)
for _, season := range []struct { //nolint:gocritic
title string
Expand Down
36 changes: 18 additions & 18 deletions examples/basic/database_sql/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
)

func selectDefault(ctx context.Context, db *sql.DB) (err error) {
func selectDefault(ctx context.Context, db *sql.DB) error {
// explain of query
err = retry.Do(ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
err := retry.Do(ctx, db, func(ctx context.Context, cc *sql.Conn) error {
row := cc.QueryRowContext(ydb.WithQueryMode(ctx, ydb.ExplainQueryMode),
`SELECT series_id, title, release_date FROM series;`,
)
var (
ast string
plan string
)
if err = row.Scan(&ast, &plan); err != nil {
if err := row.Scan(&ast, &plan); err != nil {
return err
}
log.Printf("AST = %s\n\nPlan = %s", ast, plan)
Expand All @@ -39,10 +39,10 @@ func selectDefault(ctx context.Context, db *sql.DB) (err error) {
err = retry.Do(
ydb.WithTxControl(ctx, table.OnlineReadOnlyTxControl()),
db,
func(ctx context.Context, cc *sql.Conn) (err error) {
rows, err := cc.QueryContext(ctx, `SELECT series_id, title, release_date FROM series;`)
if err != nil {
return err
func(ctx context.Context, cc *sql.Conn) error {
rows, errIn := cc.QueryContext(ctx, `SELECT series_id, title, release_date FROM series;`)
if errIn != nil {
return errIn
}
defer func() {
_ = rows.Close()
Expand Down Expand Up @@ -72,12 +72,12 @@ func selectDefault(ctx context.Context, db *sql.DB) (err error) {
return nil
}

func selectScan(ctx context.Context, db *sql.DB) (err error) {
func selectScan(ctx context.Context, db *sql.DB) error {
// scan query
err = retry.Do(
err := retry.Do(
ydb.WithTxControl(ctx, table.StaleReadOnlyTxControl()),
db,
func(ctx context.Context, cc *sql.Conn) (err error) {
func(ctx context.Context, cc *sql.Conn) error {
var (
id string
seriesIDs []types.Value
Expand All @@ -92,11 +92,11 @@ func selectScan(ctx context.Context, db *sql.DB) (err error) {
table.ValueParam("$seriesTitle", types.TextValue("%IT Crowd%")),
),
)
if err = row.Scan(&id); err != nil {
if err := row.Scan(&id); err != nil {
return err
}
seriesIDs = append(seriesIDs, types.BytesValueFromString(id))
if err = row.Err(); err != nil {
if err := row.Err(); err != nil {
return err
}

Expand Down Expand Up @@ -167,15 +167,15 @@ func selectScan(ctx context.Context, db *sql.DB) (err error) {
return nil
}

func fillTablesWithData(ctx context.Context, db *sql.DB) (err error) {
func fillTablesWithData(ctx context.Context, db *sql.DB) error {
series, seasonsData, episodesData := getData()
args := []interface{}{
sql.Named("seriesData", types.ListValue(series...)),
sql.Named("seasonsData", types.ListValue(seasonsData...)),
sql.Named("episodesData", types.ListValue(episodesData...)),
}
err = retry.DoTx(ctx, db, func(ctx context.Context, tx *sql.Tx) error {
if _, err = tx.ExecContext(ctx, `
err := retry.DoTx(ctx, db, func(ctx context.Context, tx *sql.Tx) error {
if _, err := tx.ExecContext(ctx, `
REPLACE INTO series
SELECT
series_id,
Expand Down Expand Up @@ -216,9 +216,9 @@ func fillTablesWithData(ctx context.Context, db *sql.DB) (err error) {
return nil
}

func prepareSchema(ctx context.Context, db *sql.DB) (err error) {
err = retry.Do(ctx, db, func(ctx context.Context, cc *sql.Conn) error {
err = dropTableIfExists(ctx, cc, "series")
func prepareSchema(ctx context.Context, db *sql.DB) error {
err := retry.Do(ctx, db, func(ctx context.Context, cc *sql.Conn) error {
err := dropTableIfExists(ctx, cc, "series")
if err != nil {
_, _ = fmt.Fprintf(os.Stdout, "warn: drop series table failed: %v\n", err)
}
Expand Down
12 changes: 6 additions & 6 deletions examples/basic/gorm/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Series struct {
Seasons []Season
}

func (s *Series) BeforeCreate(_ *gorm.DB) (err error) {
func (s *Series) BeforeCreate(_ *gorm.DB) error {
id, err := uuid.NewUUID()
if err != nil {
return err
Expand All @@ -27,7 +27,7 @@ func (s *Series) BeforeCreate(_ *gorm.DB) (err error) {
s.Seasons[i].SeriesID = s.ID
}

return
return err
}

type Season struct {
Expand All @@ -40,7 +40,7 @@ type Season struct {
Episodes []Episode
}

func (s *Season) BeforeCreate(_ *gorm.DB) (err error) {
func (s *Season) BeforeCreate(_ *gorm.DB) error {
id, err := uuid.NewUUID()
if err != nil {
return err
Expand All @@ -50,7 +50,7 @@ func (s *Season) BeforeCreate(_ *gorm.DB) (err error) {
s.Episodes[i].SeasonID = s.ID
}

return
return err
}

type Episode struct {
Expand All @@ -60,12 +60,12 @@ type Episode struct {
AirDate time.Time `gorm:"column:air_date"`
}

func (e *Episode) BeforeCreate(_ *gorm.DB) (err error) {
func (e *Episode) BeforeCreate(_ *gorm.DB) error {
id, err := uuid.NewUUID()
if err != nil {
return err
}
e.ID = id.String()

return
return err
}
50 changes: 30 additions & 20 deletions examples/basic/native/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,21 @@ SELECT
FROM AS_TABLE($episodesData);
`))

func readTable(ctx context.Context, c table.Client, path string) (err error) {
var res result.StreamResult
func readTable(ctx context.Context, c table.Client, path string) error {
var (
res result.StreamResult
err error
)
err = c.Do(ctx,
func(ctx context.Context, s table.Session) (err error) {
func(ctx context.Context, s table.Session) error {
res, err = s.StreamReadTable(ctx, path,
options.ReadOrdered(),
options.ReadColumn("series_id"),
options.ReadColumn("title"),
options.ReadColumn("release_date"),
)

return
return err
},
)
if err != nil {
Expand Down Expand Up @@ -138,13 +141,16 @@ func readTable(ctx context.Context, c table.Client, path string) (err error) {
return res.Err()
}

func describeTableOptions(ctx context.Context, c table.Client) (err error) {
var desc options.TableOptionsDescription
func describeTableOptions(ctx context.Context, c table.Client) error {
var (
desc options.TableOptionsDescription
err error
)
err = c.Do(ctx,
func(ctx context.Context, s table.Session) (err error) {
func(ctx context.Context, s table.Session) error {
desc, err = s.DescribeTableOptions(ctx)

return
return err
},
)
if err != nil {
Expand Down Expand Up @@ -191,7 +197,7 @@ func describeTableOptions(ctx context.Context, c table.Client) (err error) {
return nil
}

func selectSimple(ctx context.Context, c table.Client, prefix string) (err error) {
func selectSimple(ctx context.Context, c table.Client, prefix string) error {
query := render(
template.Must(template.New("").Parse(`
PRAGMA TablePathPrefix("{{ .TablePathPrefix }}");
Expand Down Expand Up @@ -220,17 +226,20 @@ func selectSimple(ctx context.Context, c table.Client, prefix string) (err error
),
table.CommitTx(),
)
var res result.Result
var (
res result.Result
err error
)
err = c.Do(ctx,
func(ctx context.Context, s table.Session) (err error) {
func(ctx context.Context, s table.Session) error {
_, res, err = s.Execute(ctx, readTx, query,
table.NewQueryParameters(
table.ValueParam("$seriesID", types.Uint64Value(1)),
),
options.WithCollectStatsModeBasic(),
)

return
return err
},
)
if err != nil {
Expand Down Expand Up @@ -266,7 +275,7 @@ func selectSimple(ctx context.Context, c table.Client, prefix string) (err error
return res.Err()
}

func scanQuerySelect(ctx context.Context, c table.Client, prefix string) (err error) {
func scanQuerySelect(ctx context.Context, c table.Client, prefix string) error {
query := render(
template.Must(template.New("").Parse(`
PRAGMA TablePathPrefix("{{ .TablePathPrefix }}");
Expand Down Expand Up @@ -327,15 +336,16 @@ func scanQuerySelect(ctx context.Context, c table.Client, prefix string) (err er
)
}

func fillTablesWithData(ctx context.Context, c table.Client, prefix string) (err error) {
func fillTablesWithData(ctx context.Context, c table.Client, prefix string) error {
var err error
writeTx := table.TxControl(
table.BeginTx(
table.WithSerializableReadWrite(),
),
table.CommitTx(),
)
err = c.Do(ctx,
func(ctx context.Context, s table.Session) (err error) {
func(ctx context.Context, s table.Session) error {
_, _, err = s.Execute(ctx, writeTx, render(fill, templateConfig{
TablePathPrefix: prefix,
}), table.NewQueryParameters(
Expand All @@ -351,8 +361,8 @@ func fillTablesWithData(ctx context.Context, c table.Client, prefix string) (err
return err
}

func createTables(ctx context.Context, c table.Client, prefix string) (err error) {
err = c.Do(ctx,
func createTables(ctx context.Context, c table.Client, prefix string) error {
err := c.Do(ctx,
func(ctx context.Context, s table.Session) error {
return s.CreateTable(ctx, path.Join(prefix, "series"),
options.WithColumn("series_id", types.Optional(types.TypeUint64)),
Expand Down Expand Up @@ -403,8 +413,8 @@ func createTables(ctx context.Context, c table.Client, prefix string) (err error
return nil
}

func describeTable(ctx context.Context, c table.Client, path string) (err error) {
err = c.Do(ctx,
func describeTable(ctx context.Context, c table.Client, path string) error {
err := c.Do(ctx,
func(ctx context.Context, s table.Session) error {
desc, err := s.DescribeTable(ctx, path)
if err != nil {
Expand All @@ -419,7 +429,7 @@ func describeTable(ctx context.Context, c table.Client, path string) (err error)
},
)

return
return err
}

func render(t *template.Template, data interface{}) string {
Expand Down
Loading