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

fix(387): add more useful linter #394

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,51 @@ run:
linters:
enable:
- goimports
- goconst
- misspell
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
# - revive
- asasalint
- asciicheck
- bodyclose
# - contextcheck
- errname
- errorlint
- exportloopref
# - gocritic
# - godot
# - gomoddirectives
# - gosec
- nakedret
# - nilerr
# - nilnil
# - noctx
- nolintlint
# - prealloc
# - predeclared
- promlinter
# - reassign
- rowserrcheck
- sqlclosecheck
- tenv
# - testableexamples
# - thelper
- tparallel
- unconvert
# - unparam
- usestdlibvars
- wastedassign

linters-settings:
goimports:
local-prefixes: github.com/streamnative/oxia
goconst:
ignore-tests: true

issues:
fix: true
14 changes: 7 additions & 7 deletions cmd/client/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ func TestClientCmd(t *testing.T) {
"\\{\"error\":\"binary flag was set but value is not valid base64\"\\}",
"^$",
},
{"put-no-value", []string{"put", "-k", "k-put-np"}, "", put.ErrorExpectedKeyValueInconsistent,
{"put-no-value", []string{"put", "-k", "k-put-np"}, "", put.ErrExpectedKeyValueInconsistent,
".*",
"Error: inconsistent flags; key and value flags must be in pairs",
},
{"put-no-key", []string{"put", "-v", "k-put-np"}, "", put.ErrorExpectedKeyValueInconsistent,
{"put-no-key", []string{"put", "-v", "k-put-np"}, "", put.ErrExpectedKeyValueInconsistent,
".*",
"Error: inconsistent flags; key and value flags must be in pairs",
},
{"put-stdin", []string{"put"}, "{\"key\":\"3a\",\"value\":\"aGVsbG8y\",\"binary\":true}\n{\"key\":\"3x\",\"value\":\"aGVsbG8y\"}", nil,
"\\{\"version\":\\{\"version_id\":7,\"created_timestamp\":\\d+,\"modified_timestamp\":\\d+\\,\"modifications_count\":0}\\}\\n{\"version\":\\{\"version_id\":8,\"created_timestamp\":\\d+,\"modified_timestamp\":\\d+\\,\"modifications_count\":0}\\}",
"^$",
},
{"put-bad-binary-use", []string{"put", "-b"}, "", put.ErrorIncorrectBinaryFlagUse,
{"put-bad-binary-use", []string{"put", "-b"}, "", put.ErrIncorrectBinaryFlagUse,
".*",
"Error: binary flag was set when config is being sourced from stdin",
},
Expand All @@ -113,7 +113,7 @@ func TestClientCmd(t *testing.T) {
"\\{\"binary\":true,\"value\":\"aGVsbG8y\",\"version\":\\{\"version_id\":6,\"created_timestamp\":\\d+,\"modified_timestamp\":\\d+\\,\"modifications_count\":0}\\}\\n{\"binary\":false,\"value\":\"c\",\"version\":\\{\"version_id\":4,\"created_timestamp\":\\d+,\"modified_timestamp\":\\d+\\,\"modifications_count\":0}\\}",
"^$",
},
{"get-bad-binary-use", []string{"get", "-b"}, "", get.ErrorIncorrectBinaryFlagUse,
{"get-bad-binary-use", []string{"get", "-b"}, "", get.ErrIncorrectBinaryFlagUse,
".*",
"Error: binary flag was set when config is being sourced from stdin",
},
Expand All @@ -125,11 +125,11 @@ func TestClientCmd(t *testing.T) {
"\\{\"keys\":\\[\"k-put\",\"k-put-binary-ok\"\\]\\}",
"^$",
},
{"list-no-minimum", []string{"list", "--key-max", "XXY"}, "", list.ErrorExpectedRangeInconsistent,
{"list-no-minimum", []string{"list", "--key-max", "XXY"}, "", list.ErrExpectedRangeInconsistent,
".*",
"Error: inconsistent flags; min and max flags must be in pairs",
},
{"list-no-maximum", []string{"list", "--key-min", "XXX"}, "", list.ErrorExpectedRangeInconsistent,
{"list-no-maximum", []string{"list", "--key-min", "XXX"}, "", list.ErrExpectedRangeInconsistent,
".*",
"Error: inconsistent flags; min and max flags must be in pairs",
},
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestClientCmd(t *testing.T) {
"\\{\\}",
"^$",
},
{"delete-range-with-expected", []string{"delete", "--key-min", "q", "--key-max", "s", "-e", "0"}, "", delete.ErrorExpectedVersionInconsistent,
{"delete-range-with-expected", []string{"delete", "--key-min", "q", "--key-max", "s", "-e", "0"}, "", delete.ErrExpectedVersionInconsistent,
".*",
"Error: inconsistent flags; zero or all keys must have an expected version",
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/client/delete/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
var (
Config = flags{}

ErrorExpectedVersionInconsistent = errors.New("inconsistent flags; zero or all keys must have an expected version")
ErrorExpectedRangeInconsistent = errors.New("inconsistent flags; min and max flags must be in pairs")
ErrExpectedVersionInconsistent = errors.New("inconsistent flags; zero or all keys must have an expected version")
ErrExpectedRangeInconsistent = errors.New("inconsistent flags; min and max flags must be in pairs")
)

type flags struct {
Expand Down Expand Up @@ -72,10 +72,10 @@ func exec(cmd *cobra.Command, args []string) error {

func _exec(flags flags, in io.Reader, queue common.QueryQueue) error {
if len(flags.keyMinimums) != len(flags.keyMaximums) {
return ErrorExpectedRangeInconsistent
return ErrExpectedRangeInconsistent
}
if len(flags.keys) != len(flags.expectedVersions) && len(flags.expectedVersions) > 0 {
return ErrorExpectedVersionInconsistent
return ErrExpectedVersionInconsistent
}
if len(flags.keyMinimums) > 0 || len(flags.keys) > 0 {
for i, n := range flags.keyMinimums {
Expand Down
8 changes: 4 additions & 4 deletions cmd/client/delete/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,23 @@ func Test_exec(t *testing.T) {
flags{
expectedVersions: []int64{1},
},
ErrorExpectedVersionInconsistent,
ErrExpectedVersionInconsistent,
nil},
{"missing-version",
"",
flags{
keys: []string{"x", "y"},
expectedVersions: []int64{1},
},
ErrorExpectedVersionInconsistent,
ErrExpectedVersionInconsistent,
nil},
{"range-no-max",
"",
flags{
keyMinimums: []string{"a", "x"},
keyMaximums: []string{"y"},
},
ErrorExpectedRangeInconsistent,
ErrExpectedRangeInconsistent,
nil,
},
{"range",
Expand All @@ -160,7 +160,7 @@ func Test_exec(t *testing.T) {
keyMinimums: []string{"a"},
keyMaximums: []string{"b", "y"},
},
ErrorExpectedRangeInconsistent,
ErrExpectedRangeInconsistent,
nil,
},
{"stdin",
Expand Down
4 changes: 2 additions & 2 deletions cmd/client/get/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
var (
Config = flags{}

ErrorIncorrectBinaryFlagUse = errors.New("binary flag was set when config is being sourced from stdin")
ErrIncorrectBinaryFlagUse = errors.New("binary flag was set when config is being sourced from stdin")
)

type flags struct {
Expand Down Expand Up @@ -82,7 +82,7 @@ func _exec(flags flags, in io.Reader, queue common.QueryQueue) error {
}
} else {
if flags.binaryValues {
return ErrorIncorrectBinaryFlagUse
return ErrIncorrectBinaryFlagUse
}
common.ReadStdin(in, Query{}, queue)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/client/list/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
var (
Config = flags{}

ErrorExpectedRangeInconsistent = errors.New("inconsistent flags; min and max flags must be in pairs")
ErrExpectedRangeInconsistent = errors.New("inconsistent flags; min and max flags must be in pairs")
)

type flags struct {
Expand Down Expand Up @@ -66,7 +66,7 @@ func exec(cmd *cobra.Command, args []string) error {

func _exec(flags flags, in io.Reader, queue common.QueryQueue) error {
if len(flags.keyMinimums) != len(flags.keyMaximums) {
return ErrorExpectedRangeInconsistent
return ErrExpectedRangeInconsistent
}
if len(flags.keyMinimums) > 0 {
for i, n := range flags.keyMinimums {
Expand Down
4 changes: 2 additions & 2 deletions cmd/client/list/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ func Test_exec(t *testing.T) {
flags{
keyMaximums: []string{"b"},
},
ErrorExpectedRangeInconsistent,
ErrExpectedRangeInconsistent,
nil},
{"range-no-max",
"",
flags{
keyMaximums: []string{"b"},
},
ErrorExpectedRangeInconsistent,
ErrExpectedRangeInconsistent,
nil},
} {
t.Run(test.name, func(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions cmd/client/put/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
var (
Config = flags{}

ErrorExpectedKeyValueInconsistent = errors.New("inconsistent flags; key and value flags must be in pairs")
ErrorExpectedVersionInconsistent = errors.New("inconsistent flags; zero or all keys must have an expected version")
ErrorBase64ValueInvalid = errors.New("binary flag was set but value is not valid base64")
ErrorIncorrectBinaryFlagUse = errors.New("binary flag was set when config is being sourced from stdin")
ErrExpectedKeyValueInconsistent = errors.New("inconsistent flags; key and value flags must be in pairs")
ErrExpectedVersionInconsistent = errors.New("inconsistent flags; zero or all keys must have an expected version")
ErrBase64ValueInvalid = errors.New("binary flag was set but value is not valid base64")
ErrIncorrectBinaryFlagUse = errors.New("binary flag was set when config is being sourced from stdin")
)

type flags struct {
Expand Down Expand Up @@ -78,10 +78,10 @@ func exec(cmd *cobra.Command, args []string) error {

func _exec(flags flags, in io.Reader, queue common.QueryQueue) error {
if len(flags.keys) != len(flags.values) && (len(flags.values) > 0 || len(flags.keys) > 0) {
return ErrorExpectedKeyValueInconsistent
return ErrExpectedKeyValueInconsistent
}
if (len(flags.expectedVersions) > 0) && len(flags.keys) != len(flags.expectedVersions) {
return ErrorExpectedVersionInconsistent
return ErrExpectedVersionInconsistent
}
if len(flags.keys) > 0 {
for i, k := range flags.keys {
Expand All @@ -97,7 +97,7 @@ func _exec(flags flags, in io.Reader, queue common.QueryQueue) error {
}
} else {
if flags.binaryValues {
return ErrorIncorrectBinaryFlagUse
return ErrIncorrectBinaryFlagUse
}
common.ReadStdin(in, Query{}, queue)
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func convertValue(binary bool, value string) ([]byte, error) {
decoded := make([]byte, int64(float64(len(value))*0.8))
_, err := base64.StdEncoding.Decode(decoded, []byte(value))
if err != nil {
return nil, ErrorBase64ValueInvalid
return nil, ErrBase64ValueInvalid
}
return decoded, nil
} else {
Expand Down
10 changes: 5 additions & 5 deletions cmd/client/put/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ func Test_exec(t *testing.T) {
flags{
values: []string{"y"},
},
ErrorExpectedKeyValueInconsistent,
ErrExpectedKeyValueInconsistent,
nil},
{"entry-no-value",
"",
flags{
keys: []string{"x"},
},
ErrorExpectedKeyValueInconsistent,
ErrExpectedKeyValueInconsistent,
nil},
{"entry-missing-version",
"",
Expand All @@ -104,7 +104,7 @@ func Test_exec(t *testing.T) {
values: []string{"a", "b"},
expectedVersions: []int64{1},
},
ErrorExpectedVersionInconsistent,
ErrExpectedVersionInconsistent,
nil},
{"entry-binary",
"",
Expand Down Expand Up @@ -194,7 +194,7 @@ func Test_exec(t *testing.T) {
flags{
binaryValues: true,
},
ErrorIncorrectBinaryFlagUse,
ErrIncorrectBinaryFlagUse,
nil},
} {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -312,7 +312,7 @@ func TestConvertValue(t *testing.T) {
name: "invalid-binary",
value: "hello",
binary: true,
expectedErr: ErrorBase64ValueInvalid,
expectedErr: ErrBase64ValueInvalid,
},
} {
t.Run(test.name, func(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions common/batch/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"time"
)

var ErrorShuttingDown = errors.New("shutting down")
var ErrShuttingDown = errors.New("shutting down")

type Batcher interface {
io.Closer
Expand All @@ -46,7 +46,7 @@ func (b *batcherImpl) Close() error {

func (b *batcherImpl) Add(call any) {
if b.closed.Load() {
b.failCall(call, ErrorShuttingDown)
b.failCall(call, ErrShuttingDown)
} else {
b.callC <- call
}
Expand Down Expand Up @@ -103,13 +103,13 @@ func (b *batcherImpl) Run() {
case <-b.closeC:
if batch != nil {
timer.Stop()
batch.Fail(ErrorShuttingDown)
batch.Fail(ErrShuttingDown)
batch = nil
}
for {
select {
case call := <-b.callC:
b.failCall(call, ErrorShuttingDown)
b.failCall(call, ErrShuttingDown)
default:
return
}
Expand Down
4 changes: 2 additions & 2 deletions common/batch/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (b *testBatch) Complete() {

func (b *testBatch) Fail(err error) {
b.result <- err
//closeC(b.result)
// closeC(b.result)
}

func TestBatcher(t *testing.T) {
Expand All @@ -65,7 +65,7 @@ func TestBatcher(t *testing.T) {
}{
{"complete on maxRequestsPerBatch", 1 * time.Second, 1, false, nil},
{"complete on linger", 1 * time.Millisecond, 2, false, nil},
{"fail on close", 1 * time.Second, 2, true, ErrorShuttingDown},
{"fail on close", 1 * time.Second, 2, true, ErrShuttingDown},
} {
t.Run(item.name, func(t *testing.T) {
testBatch := newTestBatch()
Expand Down
6 changes: 3 additions & 3 deletions common/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import (
const DefaultLogLevel = slog.LevelInfo

var (
// LogLevel Used for flags
// LogLevel Used for flags.
LogLevel slog.Level
// LogJson Used for flags
// LogJson Used for flags.
LogJson bool
)

// ParseLogLevel will convert the slog level configuration to slog.Level values
// ParseLogLevel will convert the slog level configuration to slog.Level values.
func ParseLogLevel(levelStr string) (slog.Level, error) {
switch {
case strings.EqualFold(levelStr, slog.LevelDebug.String()):
Expand Down
2 changes: 1 addition & 1 deletion common/memoize.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type memoize[T any] struct {
}

// Memoize is used to cache the result of the invocation of a function
// for a certain amount of time
// for a certain amount of time.
func Memoize[T any](provider func() T, cacheTime time.Duration) func() T {
m := memoize[T]{
provider: provider,
Expand Down
Loading
Loading