Skip to content

Commit b2c2e7c

Browse files
committed
massive refactoring
1 parent 64192e7 commit b2c2e7c

752 files changed

Lines changed: 39367 additions & 68448 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.golangci.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
version: "2"
2+
run:
3+
tests: false
4+
linters:
5+
default: all
6+
disable:
7+
- embeddedstructfieldcheck # too opinionated
8+
- err113 # will fix later
9+
- exhaustruct # I don't declare default fields!
10+
- exhaustive # missing cases in switch are deliberate
11+
- funcorder # my code, my order
12+
- godox # I need TODOs
13+
- godot # too opinionated
14+
- ireturn # I don't care for returning interfaces. It's easier this way
15+
- noinlineerr # my code, my inlining
16+
- nlreturn # too opinionated
17+
- tagalign # too opinionated
18+
- varnamelen # I decide if the variable name is long enough
19+
- wrapcheck # will fix later
20+
- wsl # deprecated
21+
- wsl_v5 # too opinionated
22+
settings:
23+
funlen:
24+
lines: 80
25+
statements: 60
26+
depguard:
27+
rules:
28+
main:
29+
deny:
30+
- pkg: "math/rand$"
31+
desc: use math/rand/v2
32+
- pkg: "github.com/sirupsen/logrus"
33+
desc: not allowed
34+
- pkg: "github.com/pkg/errors"
35+
desc: Should be replaced by standard lib errors package
36+
lll:
37+
line-length: 180
38+
cyclop:
39+
max-complexity: 15
40+
tagliatelle:
41+
# check the struck tag name case
42+
case:
43+
# use the struct field name to check the name of the struct tag
44+
rules:
45+
# any struct tag type can be used.
46+
# support string case: `camel`, `pascal`, `kebab`, `snake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`
47+
json: snake
48+
yaml: snake
49+
use-field-name: true
50+
exclusions:
51+
generated: lax
52+
presets:
53+
- comments
54+
- common-false-positives
55+
- legacy
56+
- std-error-handling
57+
paths:
58+
- third_party$
59+
- builtin$
60+
- examples$
61+
formatters:
62+
enable:
63+
- gci
64+
- gofmt
65+
- gofumpt
66+
- goimports
67+
exclusions:
68+
generated: lax
69+
paths:
70+
- third_party$
71+
- builtin$
72+
- examples$

Makefile

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
1+
define USAGE
2+
3+
"Go blocks" project. A collection of simple but useful Go packages.
4+
5+
Usage: make <target>
6+
7+
some of the <targets> are:
8+
9+
lint - run linters
10+
test - run tests
11+
update-deps - update dependencies
12+
13+
endef
14+
export USAGE
15+
16+
define CAKE
17+
\033[1;31m. . .\033[0m
18+
i i i
19+
%~%~%~%
20+
|||||||
21+
-=========-
22+
endef
23+
export CAKE
24+
25+
help:
26+
@echo "$$USAGE"
27+
128
test:
229
go test -cover ./...
330

431
lint:
532
golangci-lint run
633

7-
.PHONY: test
34+
update-deps:
35+
go get -u ./... && go mod tidy && go mod vendor
36+
37+
cake:
38+
@printf "%b\n" "$$CAKE"
39+
40+
.PHONY: help test lint update-deps cake

apiauth/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package apiauth
22

33
import "net/http"
44

5-
// Authentication provider.
5+
// Auth is authentication provider
66
type Auth interface {
77
Authorized(req *http.Request) error
88
}

conftool/conftool.go

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func parseRead(f io.Reader, decoder unmarshaller, cfg any) error {
7777
func defaultsAndRequired(cfg any) error {
7878
reqs := defsAndReqs(cfg)
7979
if len(reqs) > 0 {
80-
return fmt.Errorf("required config parameters not set: %s", strings.Join(reqs, ", "))
80+
return fmt.Errorf("required config parameters not set: %s", strings.Join(reqs, ", ")) //nolint:err113
8181
}
8282
return nil
8383
}
@@ -87,12 +87,12 @@ func defsAndReqs(cfg any) []string {
8787
val := reflect.ValueOf(cfg).Elem()
8888
typ := val.Type()
8989

90-
for i := 0; i < val.NumField(); i++ {
90+
for i := range val.NumField() {
9191
field := val.Field(i)
9292
fieldType := typ.Field(i)
9393

9494
// Set the default value based on the field kind
95-
if field.Kind() == reflect.Struct {
95+
if field.Kind() == reflect.Struct { //nolint:nestif
9696
// If it's a struct, recurse
9797
reqs = append(reqs, defsAndReqs(field.Addr().Interface())...)
9898
} else if field.CanSet() {
@@ -101,11 +101,7 @@ func defsAndReqs(cfg any) []string {
101101
continue
102102
}
103103
// Check if the field has a `required` tag
104-
isRequired := false
105-
required, ok := fieldType.Tag.Lookup("required")
106-
if ok && required == "true" {
107-
isRequired = true
108-
}
104+
isRequired := isFieldRequired(fieldType)
109105
// Check if the field has a `default` tag
110106
defaultValue, hasDefault := fieldType.Tag.Lookup("default")
111107
if !hasDefault {
@@ -115,41 +111,20 @@ func defsAndReqs(cfg any) []string {
115111
continue
116112
}
117113

118-
switch field.Kind() {
119-
case reflect.String:
120-
field.SetString(defaultValue)
121-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
122-
intValue, err := strconv.ParseInt(defaultValue, 10, 64)
123-
if err != nil {
124-
errSyntax := strconv.ErrSyntax
125-
if errors.Is(err, errSyntax) {
126-
dur, err := time.ParseDuration(defaultValue)
127-
if err == nil {
128-
field.SetInt(int64(dur))
129-
}
130-
}
131-
} else {
132-
field.SetInt(intValue)
133-
}
134-
135-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
136-
if uintValue, err := strconv.ParseUint(defaultValue, 10, 64); err == nil {
137-
field.SetUint(uintValue)
138-
}
139-
case reflect.Float32, reflect.Float64:
140-
if floatValue, err := strconv.ParseFloat(defaultValue, 64); err == nil {
141-
field.SetFloat(floatValue)
142-
}
143-
case reflect.Bool:
144-
if boolValue, err := strconv.ParseBool(defaultValue); err == nil {
145-
field.SetBool(boolValue)
146-
}
147-
}
114+
setField(field, defaultValue)
148115
}
149116
}
150117
return reqs
151118
}
152119

120+
func isFieldRequired(field reflect.StructField) bool {
121+
required, ok := field.Tag.Lookup("required")
122+
if ok && required == "true" {
123+
return true
124+
}
125+
return false
126+
}
127+
153128
func isFieldEmpty(v reflect.Value) bool {
154129
switch v.Kind() {
155130
case reflect.Slice:
@@ -163,3 +138,36 @@ func isFieldEmpty(v reflect.Value) bool {
163138
return v.Interface() == reflect.Zero(v.Type()).Interface()
164139
}
165140
}
141+
142+
func setField(field reflect.Value, defaultValue string) {
143+
switch field.Kind() {
144+
case reflect.String:
145+
field.SetString(defaultValue)
146+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
147+
intValue, err := strconv.ParseInt(defaultValue, 10, 64)
148+
if err != nil {
149+
errSyntax := strconv.ErrSyntax
150+
if errors.Is(err, errSyntax) {
151+
dur, err := time.ParseDuration(defaultValue)
152+
if err == nil {
153+
field.SetInt(int64(dur))
154+
}
155+
}
156+
} else {
157+
field.SetInt(intValue)
158+
}
159+
160+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
161+
if uintValue, err := strconv.ParseUint(defaultValue, 10, 64); err == nil {
162+
field.SetUint(uintValue)
163+
}
164+
case reflect.Float32, reflect.Float64:
165+
if floatValue, err := strconv.ParseFloat(defaultValue, 64); err == nil {
166+
field.SetFloat(floatValue)
167+
}
168+
case reflect.Bool:
169+
if boolValue, err := strconv.ParseBool(defaultValue); err == nil {
170+
field.SetBool(boolValue)
171+
}
172+
}
173+
}

conncount/transport.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ type Transport struct {
2424
type dialer func(ctx context.Context, network string, addr string) (net.Conn, error)
2525

2626
// NewTransport creates Transport with a connection counter.
27-
// prev is a Transport to be wrapped
28-
// callback is a function to be called when connection counter changes
27+
// prev is a Transport to be wrapped.
28+
// callback is a function to be called when connection counter changes.
2929
func NewTransport(logger log.MetaLogger, prev *http.Transport, callback func(int64)) *Transport {
3030
var counter int64
3131
tran := &Transport{Transport: prev, connCounter: &counter}
@@ -54,8 +54,8 @@ func NewTransport(logger log.MetaLogger, prev *http.Transport, callback func(int
5454
return instrumentedConn, nil
5555
}
5656
}
57-
tran.Transport.DialContext = dialWithCounter(prevDialer)
58-
tran.Transport.DialTLSContext = dialWithCounter(prevTLSDialer)
57+
tran.DialContext = dialWithCounter(prevDialer)
58+
tran.DialTLSContext = dialWithCounter(prevTLSDialer)
5959
return tran
6060
}
6161

@@ -64,7 +64,7 @@ func (tran *Transport) getPreviousDialer() func(ctx context.Context, network, ad
6464
return tran.DialContext
6565
}
6666
if tran.Dial != nil {
67-
return func(ctx context.Context, network, addr string) (net.Conn, error) {
67+
return func(_ context.Context, network, addr string) (net.Conn, error) {
6868
return tran.Dial(network, addr) //nolint:wrapcheck
6969
}
7070
}
@@ -77,7 +77,7 @@ func (tran *Transport) getPreviousTLSDialer() func(ctx context.Context, network,
7777
return tran.DialTLSContext
7878
}
7979
if tran.DialTLS != nil {
80-
return func(ctx context.Context, network, addr string) (net.Conn, error) {
80+
return func(_ context.Context, network, addr string) (net.Conn, error) {
8181
return tran.DialTLS(network, addr) //nolint:wrapcheck
8282
}
8383
}

0 commit comments

Comments
 (0)