Skip to content

Commit c832905

Browse files
authored
Add golangci-lint (#9)
1 parent b5c2e80 commit c832905

5 files changed

Lines changed: 193 additions & 28 deletions

File tree

.github/workflows/lint.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
defaults:
12+
run:
13+
shell: bash
14+
15+
concurrency:
16+
group: ${{ github.ref_name }}-lint
17+
cancel-in-progress: true
18+
19+
jobs:
20+
lint:
21+
name: Go Lint
22+
runs-on: ubuntu-24.04
23+
steps:
24+
- name: Checkout Repository
25+
uses: actions/checkout@v6
26+
27+
- name: Setup Golang Environment
28+
uses: actions/setup-go@v6
29+
with:
30+
go-version: stable
31+
32+
- name: Lint Go
33+
uses: golangci/golangci-lint-action@v9
34+
35+
actionlint:
36+
name: Actionlint
37+
runs-on: ubuntu-24.04
38+
steps:
39+
- name: Checkout Repository
40+
uses: actions/checkout@v6
41+
42+
- name: Lint Actions
43+
uses: reviewdog/action-actionlint@v1

.golangci.yml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
version: "2"
2+
linters:
3+
default: none
4+
enable:
5+
- asasalint
6+
- asciicheck
7+
- bidichk
8+
- contextcheck
9+
- copyloopvar
10+
- dupword
11+
- durationcheck
12+
- errcheck
13+
- errchkjson
14+
- errname
15+
- errorlint
16+
- fatcontext
17+
- forcetypeassert
18+
- gocheckcompilerdirectives
19+
- gochecksumtype
20+
- gocritic
21+
- godot
22+
- gosec
23+
- gosmopolitan
24+
- govet
25+
- ineffassign
26+
- intrange
27+
- makezero
28+
- misspell
29+
- musttag
30+
- nilerr
31+
- noctx
32+
- nolintlint
33+
- paralleltest
34+
- perfsprint
35+
- prealloc
36+
- predeclared
37+
- reassign
38+
- revive
39+
- staticcheck
40+
- tagalign
41+
- thelper
42+
- tparallel
43+
- unconvert
44+
- unparam
45+
- unused
46+
- usestdlibvars
47+
- wastedassign
48+
- whitespace
49+
settings:
50+
govet:
51+
enable-all: true
52+
misspell:
53+
locale: US
54+
revive:
55+
rules:
56+
- name: blank-imports
57+
- name: context-as-argument
58+
- name: context-keys-type
59+
- name: dot-imports
60+
- name: empty-block
61+
- name: error-naming
62+
- name: error-return
63+
- name: error-strings
64+
- name: errorf
65+
- name: exported
66+
- name: increment-decrement
67+
- name: indent-error-flow
68+
- name: package-comments
69+
- name: range
70+
- name: receiver-naming
71+
- name: redefines-builtin-id
72+
- name: superfluous-else
73+
- name: time-naming
74+
- name: unexported-return
75+
- name: unreachable-code
76+
- name: var-declaration
77+
- name: var-naming
78+
exclusions:
79+
generated: lax
80+
presets:
81+
- comments
82+
- common-false-positives
83+
- legacy
84+
- std-error-handling
85+
paths:
86+
- third_party$
87+
- builtin$
88+
- examples$
89+
issues:
90+
max-issues-per-linter: 0
91+
max-same-issues: 0
92+
formatters:
93+
enable:
94+
- gofmt
95+
- gofumpt
96+
- goimports
97+
exclusions:
98+
generated: lax
99+
paths:
100+
- third_party$
101+
- builtin$
102+
- examples$

log/log.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// DEBUG
1414

15-
// DebugWithContext logs on debug level and trace based on the context span if it exists
15+
// DebugWithContext logs on debug level and trace based on the context span if it exists.
1616
func DebugWithContext(ctx context.Context, log string, fields ...zapcore.Field) {
1717
DebugWithSpan(opentracing.SpanFromContext(ctx), log, fields...)
1818
}
@@ -23,14 +23,14 @@ func DebugWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) {
2323
logSpan(span, log, fields...)
2424
}
2525

26-
// Debug logs on debug level
26+
// Debug logs on debug level.
2727
func Debug(log string, fields ...zapcore.Field) {
2828
zap.L().Debug(log, fields...)
2929
}
3030

3131
// INFO
3232

33-
// InfoWithContext logs on info level and trace based on the context span if it exists
33+
// InfoWithContext logs on info level and trace based on the context span if it exists.
3434
func InfoWithContext(ctx context.Context, log string, fields ...zapcore.Field) {
3535
InfoWithSpan(opentracing.SpanFromContext(ctx), log, fields...)
3636
}
@@ -39,17 +39,16 @@ func InfoWithContext(ctx context.Context, log string, fields ...zapcore.Field) {
3939
func InfoWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) {
4040
Info(log, fields...)
4141
logSpan(span, log, fields...)
42-
4342
}
4443

45-
// Info logs on info level
44+
// Info logs on info level.
4645
func Info(log string, fields ...zapcore.Field) {
4746
zap.L().Info(log, fields...)
4847
}
4948

5049
// WARN
5150

52-
// WarnWithContext logs on warn level and trace based on the context span if it exists
51+
// WarnWithContext logs on warn level and trace based on the context span if it exists.
5352
func WarnWithContext(ctx context.Context, log string, fields ...zapcore.Field) {
5453
WarnWithSpan(opentracing.SpanFromContext(ctx), log, fields...)
5554
}
@@ -58,17 +57,16 @@ func WarnWithContext(ctx context.Context, log string, fields ...zapcore.Field) {
5857
func WarnWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) {
5958
Warn(log, fields...)
6059
logSpan(span, log, fields...)
61-
6260
}
6361

64-
// Warn logs on warn level
62+
// Warn logs on warn level.
6563
func Warn(log string, fields ...zapcore.Field) {
6664
zap.L().Warn(log, fields...)
6765
}
6866

6967
// ERROR
7068

71-
// ErrorWithContext logs on error level and trace based on the context span if it exists
69+
// ErrorWithContext logs on error level and trace based on the context span if it exists.
7270
func ErrorWithContext(ctx context.Context, log string, fields ...zapcore.Field) {
7371
ErrorWithSpan(opentracing.SpanFromContext(ctx), log, fields...)
7472
}
@@ -79,14 +77,14 @@ func ErrorWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) {
7977
logSpan(span, log, fields...)
8078
}
8179

82-
// Error logs on error level
80+
// Error logs on error level.
8381
func Error(log string, fields ...zapcore.Field) {
8482
zap.L().Error(log, fields...)
8583
}
8684

8785
// DPANIC
8886

89-
// DPanicWithContext logs on dPanic level and trace based on the context span if it exists
87+
// DPanicWithContext logs on dPanic level and trace based on the context span if it exists.
9088
func DPanicWithContext(ctx context.Context, log string, fields ...zapcore.Field) {
9189
DPanicWithSpan(opentracing.SpanFromContext(ctx), log, fields...)
9290
}
@@ -97,14 +95,14 @@ func DPanicWithSpan(span opentracing.Span, log string, fields ...zapcore.Field)
9795
DPanic(log, fields...)
9896
}
9997

100-
// DPanic logs on dPanic level
98+
// DPanic logs on dPanic level.
10199
func DPanic(log string, fields ...zapcore.Field) {
102100
zap.L().DPanic(log, fields...)
103101
}
104102

105103
// PANIC
106104

107-
// PanicWithContext logs on panic level and trace based on the context span if it exists
105+
// PanicWithContext logs on panic level and trace based on the context span if it exists.
108106
func PanicWithContext(ctx context.Context, log string, fields ...zapcore.Field) {
109107
PanicWithSpan(opentracing.SpanFromContext(ctx), log, fields...)
110108
}
@@ -115,12 +113,12 @@ func PanicWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) {
115113
Panic(log, fields...)
116114
}
117115

118-
// Panic logs on panic level
116+
// Panic logs on panic level.
119117
func Panic(log string, fields ...zapcore.Field) {
120118
zap.L().Panic(log, fields...)
121119
}
122120

123-
// FatalWithContext logs on fatal level and trace based on the context span if it exists
121+
// FatalWithContext logs on fatal level and trace based on the context span if it exists.
124122
func FatalWithContext(ctx context.Context, log string, fields ...zapcore.Field) {
125123
FatalWithSpan(opentracing.SpanFromContext(ctx), log, fields...)
126124
}
@@ -131,14 +129,14 @@ func FatalWithSpan(span opentracing.Span, log string, fields ...zapcore.Field) {
131129
Fatal(log, fields...)
132130
}
133131

134-
// Fatal logs on fatal level
132+
// Fatal logs on fatal level.
135133
func Fatal(log string, fields ...zapcore.Field) {
136134
zap.L().Fatal(log, fields...)
137135
}
138136

139137
func logSpan(span opentracing.Span, log string, fields ...zapcore.Field) {
140138
if span != nil {
141-
opentracingFields := make([]opentracinglog.Field, len(fields)+1)
139+
opentracingFields := make([]opentracinglog.Field, 0, len(fields)+1)
142140
if log != "" {
143141
opentracingFields = append(opentracingFields, opentracinglog.String("event", log))
144142
}

utils/fields.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,38 @@ func ZapFieldsToOpentracing(zapFields ...zapcore.Field) []opentracinglog.Field {
2525
// input Zap field.
2626
func ZapFieldToOpentracing(zapField zapcore.Field) opentracinglog.Field {
2727
switch zapField.Type {
28-
2928
case zapcore.BoolType:
3029
val := false
3130
if zapField.Integer >= 1 {
3231
val = true
3332
}
3433
return opentracinglog.Bool(zapField.Key, val)
3534
case zapcore.Float32Type:
36-
return opentracinglog.Float32(zapField.Key, math.Float32frombits(uint32(zapField.Integer)))
35+
return opentracinglog.Float32(zapField.Key, math.Float32frombits(uint32(zapField.Integer))) //nolint:gosec // G115: intentional bit-pattern conversion matching zap's encoding
3736
case zapcore.Float64Type:
38-
return opentracinglog.Float64(zapField.Key, math.Float64frombits(uint64(zapField.Integer)))
37+
return opentracinglog.Float64(zapField.Key, math.Float64frombits(uint64(zapField.Integer))) //nolint:gosec // G115: intentional bit-pattern conversion matching zap's encoding
3938
case zapcore.Int64Type:
40-
return opentracinglog.Int64(zapField.Key, int64(zapField.Integer))
39+
return opentracinglog.Int64(zapField.Key, zapField.Integer)
4140
case zapcore.Int32Type:
42-
return opentracinglog.Int32(zapField.Key, int32(zapField.Integer))
41+
return opentracinglog.Int32(zapField.Key, int32(zapField.Integer)) //nolint:gosec // G115: value was originally int32, stored as int64 by zap
4342
case zapcore.StringType:
4443
return opentracinglog.String(zapField.Key, zapField.String)
4544
case zapcore.StringerType:
46-
return opentracinglog.String(zapField.Key, zapField.Interface.(fmt.Stringer).String())
45+
if s, ok := zapField.Interface.(fmt.Stringer); ok {
46+
return opentracinglog.String(zapField.Key, s.String())
47+
}
48+
return opentracinglog.Object(zapField.Key, zapField.Interface)
4749
case zapcore.Uint64Type:
48-
return opentracinglog.Uint64(zapField.Key, uint64(zapField.Integer))
50+
return opentracinglog.Uint64(zapField.Key, uint64(zapField.Integer)) //nolint:gosec // G115: value was originally uint64, stored as int64 by zap
4951
case zapcore.Uint32Type:
50-
return opentracinglog.Uint32(zapField.Key, uint32(zapField.Integer))
52+
return opentracinglog.Uint32(zapField.Key, uint32(zapField.Integer)) //nolint:gosec // G115: value was originally uint32, stored as int64 by zap
5153
case zapcore.DurationType:
5254
return opentracinglog.String(zapField.Key, time.Duration(zapField.Integer).String())
5355
case zapcore.ErrorType:
54-
return opentracinglog.Error(zapField.Interface.(error))
56+
if err, ok := zapField.Interface.(error); ok {
57+
return opentracinglog.Error(err)
58+
}
59+
return opentracinglog.Object(zapField.Key, zapField.Interface)
5560
default:
5661
return opentracinglog.Object(zapField.Key, zapField.Interface)
5762
}

utils/fields_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (s stringer) String() string {
1818
}
1919

2020
func TestFieldsConversion(t *testing.T) {
21-
21+
t.Parallel()
2222
TestData := []struct {
2323
ZapField zapcore.Field
2424
OpenTracingField opentracinglog.Field
@@ -83,6 +83,24 @@ func TestFieldsConversion(t *testing.T) {
8383
zap.Bool("namespace", true),
8484
opentracinglog.Bool("namespace", true),
8585
},
86+
// StringerType: Interface payload is not a fmt.Stringer – should fall back to Object.
87+
{
88+
zapcore.Field{Key: "stringer", Type: zapcore.StringerType, Interface: 42},
89+
opentracinglog.Object("stringer", 42),
90+
},
91+
{
92+
zapcore.Field{Key: "stringer", Type: zapcore.StringerType, Interface: nil},
93+
opentracinglog.Object("stringer", nil),
94+
},
95+
// ErrorType: Interface payload is not an error – should fall back to Object.
96+
{
97+
zapcore.Field{Key: "err", Type: zapcore.ErrorType, Interface: "not an error"},
98+
opentracinglog.Object("err", "not an error"),
99+
},
100+
{
101+
zapcore.Field{Key: "err", Type: zapcore.ErrorType, Interface: nil},
102+
opentracinglog.Object("err", nil),
103+
},
86104
}
87105

88106
for _, data := range TestData {
@@ -97,5 +115,4 @@ func TestFieldsConversion(t *testing.T) {
97115
t.Errorf("Expected same value. Got %s but expected %s", result.Value(), data.OpenTracingField.Value())
98116
}
99117
}
100-
101118
}

0 commit comments

Comments
 (0)