Skip to content

enable gocognit linter #1037

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 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e39fb38
refactor internal/stack/record and upload with off gocognit linter
grishagavrin Feb 8, 2024
a33374f
refactor sugar/path and upload with off gocognit linter
grishagavrin Feb 8, 2024
90dd596
refactor sugar/path.removeWithPrefix and upload with off gocognit linter
grishagavrin Feb 8, 2024
3047f3f
refactor sugar/path.removeEntry and upload with off gocognit linter
grishagavrin Feb 8, 2024
f203aa4
refactor sugar/path.removeEntry and upload with off gocognit linter w…
grishagavrin Feb 8, 2024
b5dcd3c
refactor internal/xsql/dsn and upload with off gocognit linter
grishagavrin Feb 8, 2024
efc29c9
refactor internal/backoff/backoff_test and upload with off gocognit l…
grishagavrin Feb 8, 2024
c6ef237
refactor internal/decimal/decimal and upload with off gocognit linter
grishagavrin Feb 8, 2024
9d5e827
refactor internal/table/client and upload with off gocognit linter
grishagavrin Feb 8, 2024
3343652
refactor internal/table/retry_test and upload with off gocognit linter
grishagavrin Feb 8, 2024
2ba5042
refactor metrics/sql and upload with off gocognit linter
grishagavrin Feb 8, 2024
3d56192
refactor metrics/driver and upload with off gocognit linter
grishagavrin Feb 8, 2024
7386724
refactor log/sql and upload with off gocognit linter
grishagavrin Feb 8, 2024
50d2eae
refactor internal/table/scanner/scanner_test.go and upload with off g…
grishagavrin Feb 9, 2024
1edb132
refactor log/topic and upload with off gocognit linter
grishagavrin Feb 9, 2024
5ce51c4
refactor internal/table/scanner/scanner and upload with off gocognit …
grishagavrin Feb 9, 2024
a2bf458
refactor log/driver and upload with off gocognit linter
grishagavrin Feb 9, 2024
14394d5
refactor internal/topic/topicwriterinternal/queue_test and upload wit…
grishagavrin Feb 9, 2024
c97da45
refactor retry/retry and upload with off gocognit linter
grishagavrin Feb 9, 2024
4765323
refactor internal/cmd/gtrace/main and upload with off gocognit linter
grishagavrin Feb 9, 2024
a3735b0
undo because check-codegen is falling
grishagavrin Feb 9, 2024
e06bc0e
refactor internal/bind/params and upload with off gocognit linter
grishagavrin Feb 10, 2024
12265dc
refactor internal/cmd/gtrace/main manually checks tests and upload wi…
grishagavrin Feb 10, 2024
0ed37d8
refactor internal/cmd/gtrace/main.iterateOverWriter manually checks t…
grishagavrin Feb 10, 2024
3108b41
refactor internal/cmd/gtrace/main.findGtraceGen manually checks tests…
grishagavrin Feb 10, 2024
e7b2d6c
refactor internal/cmd/gtrace/main.findCommentsWithGtrace manually che…
grishagavrin Feb 10, 2024
ec004e4
refactor internal/cmd/gtrace/main.findCommentsWithGtrace manually che…
grishagavrin Feb 10, 2024
84d7f17
refactor internal/cmd/gtrace/main.findCommentsWithGtrace checks on no…
grishagavrin Feb 10, 2024
c7cb479
refactor log/table manually checks tests and upload with off gocognit…
grishagavrin Feb 10, 2024
3053edb
refactor log/table manually checks tests and upload with off gocognit…
grishagavrin Feb 10, 2024
0b86a1c
refactor log/table manually checks tests and upload with off gocognit…
grishagavrin Feb 10, 2024
e81deb1
refactor log/table manually checks tests and upload with off gocognit…
grishagavrin Feb 10, 2024
bd278ba
refactor log/table manually checks tests and upload with off gocognit…
grishagavrin Feb 10, 2024
57a84ee
gocognit linter on
grishagavrin Feb 10, 2024
d81f128
Merge branch 'master' into master
asmyasnikov Mar 12, 2024
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 @@ -230,7 +230,6 @@ linters:
- forcetypeassert
- funlen
- gochecknoglobals
- gocognit
- godot
- goerr113
- golint
Expand Down
39 changes: 22 additions & 17 deletions internal/backoff/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/stretchr/testify/require"
)

type exp struct {
eq time.Duration
gte time.Duration
lte time.Duration
}

func TestDelays(t *testing.T) {
duration := func(s string) (d time.Duration) {
d, err := time.ParseDuration(s)
Expand Down Expand Up @@ -42,11 +48,6 @@ func TestDelays(t *testing.T) {
}

func TestLogBackoff(t *testing.T) {
type exp struct {
eq time.Duration
gte time.Duration
lte time.Duration
}
for _, tt := range []struct {
backoff Backoff
exp []exp
Expand Down Expand Up @@ -145,24 +146,28 @@ func TestLogBackoff(t *testing.T) {

continue
}
if gte := exp.gte; act <= gte {
t.Errorf(
"unexpected Backoff delay: %s; want >= %s",
act, gte,
)
}
if lte := exp.lte; act >= lte {
t.Errorf(
"unexpected Backoff delay: %s; want <= %s",
act, lte,
)
}
checkExpWithAct(t, exp, act)
}
}
})
}
}

func checkExpWithAct(t *testing.T, exp exp, act time.Duration) {
if gte := exp.gte; act <= gte {
t.Errorf(
"unexpected Backoff delay: %s; want >= %s",
act, gte,
)
}
if lte := exp.lte; act >= lte {
t.Errorf(
"unexpected Backoff delay: %s; want <= %s",
act, lte,
)
}
}

func TestFastSlowDelaysWithoutJitter(t *testing.T) {
for _, tt := range []struct {
name string
Expand Down
64 changes: 41 additions & 23 deletions internal/bind/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,30 +179,11 @@ func Params(args ...interface{}) (parameters []*params.Parameter, _ error) {
for i, arg := range args {
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)
driverNamedParams, err := checkDriverNamedValue(i, params, x, args)
if err != nil {
return nil, err
}
params = driverNamedParams
case sql.NamedArg:
if x.Name == "" {
return nil, xerrors.WithStackTrace(errUnnamedParam)
Expand Down Expand Up @@ -233,3 +214,40 @@ func Params(args ...interface{}) (parameters []*params.Parameter, _ error) {

return parameters, nil
}

// checkDriverNamedValue checks the driver.NamedValue and adds it to the params slice.
func checkDriverNamedValue(
Copy link
Member

Choose a reason for hiding this comment

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

Need to test for newest func

Copy link
Member

Choose a reason for hiding this comment

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

appendParams or tryAppendParams?

i int,
params []table.ParameterOption,
x driver.NamedValue,
args []interface{},
) ([]table.ParameterOption, error) {
if x.Name == "" {
switch xx := x.Value.(type) {
case *table.QueryParameters:
if len(args) > 1 {
return nil, xerrors.WithStackTrace(errMultipleQueryParameters)
}
xx.Each(func(name string, v types.Value) {
params = append(params, table.ValueParam(name, v))
})
case table.ParameterOption:
params = append(params, xx)
default:
x.Name = fmt.Sprintf("$p%d", i)
param, err := toYdbParam(x.Name, x.Value)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
params = append(params, param)
}
} else {
param, err := toYdbParam(x.Name, x.Value)
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
params = append(params, param)
}

return params, nil
}
127 changes: 75 additions & 52 deletions internal/cmd/gtrace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ func main() {
if err != nil {
panic(fmt.Sprintf("type error: %v", err))
}
items := findGtraceGen(astFiles, pkgFiles, srcFilePath)
p := Package{
Package: pkg,
BuildConstraints: buildConstraints,
}
traces := make(map[string]*Trace)
for _, item := range items {
t := &Trace{
Name: item.Ident.Name,
}
p.Traces = append(p.Traces, t)
traces[item.Ident.Name] = t
}
extractNameAndDetails(items, &p, info, traces)
checkErrsInWriters(writers, p)

log.Println("OK")
}

// findGtraceGen iterates over the astFiles and pkgFiles to find elements marked with a comment containing "gtrace:gen"
// and collects them into GenItems.
//
//nolint:gocognit
func findGtraceGen(astFiles []*ast.File, pkgFiles []*os.File, srcFilePath string) []*GenItem {
var items []*GenItem
for i, astFile := range astFiles {
if pkgFiles[i].Name() != srcFilePath {
Expand All @@ -151,66 +175,72 @@ func main() {
depth int
item *GenItem
)
ast.Inspect(astFile, func(n ast.Node) (next bool) {
if n == nil {
item = nil
depth--
ast.Inspect(astFile,
func(n ast.Node) (next bool) {
if n == nil {
item = nil
depth--

return true
}
defer func() {
if next {
depth++
return true
}
}()
defer func() {
if next {
depth++
}
}()

switch v := n.(type) {
case *ast.FuncDecl, *ast.ValueSpec:
return false
switch v := n.(type) {
case *ast.FuncDecl, *ast.ValueSpec:
return false

case *ast.Ident:
if item != nil {
item.Ident = v
}
case *ast.Ident:
if item != nil {
item.Ident = v
}

return false
return false

case *ast.CommentGroup:
for _, c := range v.List {
if strings.Contains(strings.TrimPrefix(c.Text, "//"), "gtrace:gen") {
if item == nil {
item = &GenItem{}
case *ast.CommentGroup:
for _, c := range v.List {
if strings.Contains(strings.TrimPrefix(c.Text, "//"), "gtrace:gen") {
if item == nil {
item = &GenItem{}
}
}
}
}

return false
return false

case *ast.StructType:
if item != nil {
item.StructType = v
items = append(items, item)
item = nil
}
case *ast.StructType:
if item != nil {
item.StructType = v
items = append(items, item)
item = nil
}

return false
}
return false
}

return true
})
}
p := Package{
Package: pkg,
BuildConstraints: buildConstraints,
return true
},
)
}
traces := make(map[string]*Trace)
for _, item := range items {
t := &Trace{
Name: item.Ident.Name,

return items
}

// checkErrsInWriters iterate over each Writer in the writers slice and checks errors
func checkErrsInWriters(writers []*Writer, p Package) {
for _, w := range writers {
if err := w.Write(p); err != nil {
panic(err)
}
p.Traces = append(p.Traces, t)
traces[item.Ident.Name] = t
}
}

// extractNameAndDetails extracts the name and details of functions from the given items, and populates the traces
// in the package with hooks based on the extracted functions.
func extractNameAndDetails(items []*GenItem, p *Package, info *types.Info, traces map[string]*Trace) {
for i, item := range items {
t := p.Traces[i]
for _, field := range item.StructType.Fields.List {
Expand All @@ -237,13 +267,6 @@ func main() {
})
}
}
for _, w := range writers {
if err := w.Write(p); err != nil {
panic(err)
}
}

log.Println("OK")
}

func buildFunc(info *types.Info, traces map[string]*Trace, fn *ast.FuncType) (ret *Func, err error) {
Expand Down
Loading