Skip to content

Commit

Permalink
fix: Autofix and enable lints
Browse files Browse the repository at this point in the history
* Fix the lints, which auto fixable with golangci-lint run --fix
* Enable security (gosec) lint
* Enable performance (prealloc) lint
* Ensure license headers
  • Loading branch information
efirs committed Oct 3, 2022
1 parent 765d8d8 commit 2afe027
Show file tree
Hide file tree
Showing 116 changed files with 879 additions and 538 deletions.
122 changes: 122 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
linters:
enable:
- asasalint
- asciicheck
- bidichk
- deadcode
- decorder
- depguard
- dogsled
- errcheck
- errname
- execinquery
- exportloopref
- forbidigo
- gci
- godot
- gofmt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosimple
- govet
- grouper
- importas
- ineffassign
- misspell
- nolintlint
- promlinter
- staticcheck
- tenv
- tparallel
- typecheck
- unconvert
- unused
- usestdlibvars
- varcheck
- whitespace
- gofumpt
- gosec
- prealloc
- errchkjson
- gocritic
disable:
# Work on fixing this
- maintidx
- unparam
- nakedret
- makezero
- gocyclo
- durationcheck
- bodyclose
- gocognit
- funlen
- goconst
- nestif
- noctx
- dupl
- containedctx
- cyclop
- errorlint
- forcetypeassert
- nilerr
- nosprintfhostport
- exhaustive
- thelper
- nonamedreturns
- predeclared
- nilnil
- godox
- goerr113
- lll
- nlreturn
- nosnakecase
- revive
- stylecheck
- wsl

# Not working with generics
# enable in the future
- contextcheck
- rowserrcheck
- sqlclosecheck
- structcheck
- wastedassign

# These are two strict or deprecated
- testpackage
- paralleltest
- interfacer
- ifshort
- ireturn
- wrapcheck
- varnamelen
- gomnd
- tagliatelle
- gochecknoglobals
- exhaustivestruct
- exhaustruct
- golint
- maligned
- gochecknoinits
- scopelint
linters-settings:
goheader:
template: |-
Copyright {{ YEAR }} Tigris Data, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
lll:
line-length: 120
14 changes: 14 additions & 0 deletions api/server/v1/collation.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2022 Tigris Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

const CollationKey string = "collation"
Expand Down
36 changes: 17 additions & 19 deletions api/server/v1/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,31 @@ type TigrisError struct {
Details []proto.Message `json:"details,omitempty"`
}

// Error to return the underlying error message
// Error to return the underlying error message.
func (e *TigrisError) Error() string {
return e.Message
}

// WithDetails a helper method for adding details to the TigrisError
// WithDetails a helper method for adding details to the TigrisError.
func (e *TigrisError) WithDetails(details ...proto.Message) *TigrisError {
e.Details = append(e.Details, details...)
return e
}

// WithRetry attached retry information to the error
// WithRetry attached retry information to the error.
func (e *TigrisError) WithRetry(d time.Duration) *TigrisError {
if d != 0 {
e.Details = append(e.Details, &errdetails.RetryInfo{RetryDelay: durationpb.New(d)})
}
return e
}

// RetryDelay retrieves retry delay if it's attached to the error
// RetryDelay retrieves retry delay if it's attached to the error.
func (e *TigrisError) RetryDelay() time.Duration {
var dur time.Duration

for _, d := range e.Details {
switch t := d.(type) {
case *errdetails.RetryInfo:
if t, ok := d.(*errdetails.RetryInfo); ok {
dur = t.RetryDelay.AsDuration()
}
}
Expand All @@ -115,7 +114,7 @@ func (e *TigrisError) RetryDelay() time.Duration {
}

// ToGRPCCode converts Tigris error code to GRPC code
// Extended codes converted to 'Unknown' GRPC code
// Extended codes converted to 'Unknown' GRPC code.
func ToGRPCCode(code Code) codes.Code {
if code <= Code_UNAUTHENTICATED {
return codes.Code(code)
Expand All @@ -125,12 +124,12 @@ func ToGRPCCode(code Code) codes.Code {
return codes.Unknown
}

// ToTigrisCode converts GRPC code to Tigris code
// ToTigrisCode converts GRPC code to Tigris code.
func ToTigrisCode(code codes.Code) Code {
return Code(code)
}

// CodeFromString parses Tigris error code from its string representation
// CodeFromString parses Tigris error code from its string representation.
func CodeFromString(c string) Code {
code, ok := Code_value[c]
if !ok {
Expand All @@ -140,7 +139,7 @@ func CodeFromString(c string) Code {
return Code(code)
}

// CodeToString convert Tigris error code into string representation
// CodeToString convert Tigris error code into string representation.
func CodeToString(c Code) string {
r, ok := Code_name[int32(c)]
if !ok {
Expand Down Expand Up @@ -180,7 +179,7 @@ func FromHttpCode(httpCode int) Code {
}

// ToHTTPCode converts Tigris' code to HTTP status code
// Used to customize HTTP codes returned by GRPC-gateway
// Used to customize HTTP codes returned by GRPC-gateway.
func ToHTTPCode(code Code) int {
switch code {
case Code_OK:
Expand Down Expand Up @@ -228,17 +227,16 @@ func ToHTTPCode(code Code) int {
return 500
}

// As is used by runtime.DefaultHTTPErrorHandler to override HTTP status code
// As is used by runtime.DefaultHTTPErrorHandler to override HTTP status code.
func (e *TigrisError) As(i any) bool {
switch t := i.(type) {
case **runtime.HTTPStatusError:
if t, ok := i.(**runtime.HTTPStatusError); ok {
*t = &runtime.HTTPStatusError{HTTPStatus: ToHTTPCode(e.Code), Err: e}
return true
}
return false
}

// GRPCStatus converts the TigrisError and return status.Status. This is used to return grpc status to the grpc clients
// GRPCStatus converts the TigrisError and return status.Status. This is used to return grpc status to the grpc clients.
func (e *TigrisError) GRPCStatus() *status.Status {
st, _ := status.New(ToGRPCCode(e.Code), e.Message).
WithDetails(&errdetails.ErrorInfo{Reason: CodeToString(e.Code)})
Expand All @@ -250,7 +248,7 @@ func (e *TigrisError) GRPCStatus() *status.Status {
return st
}

// MarshalStatus marshal status object
// MarshalStatus marshal status object.
func MarshalStatus(status *spb.Status) ([]byte, error) {
resp := struct {
Error ErrorDetails `json:"error"`
Expand Down Expand Up @@ -305,7 +303,7 @@ func FromErrorDetails(e *ErrorDetails) *TigrisError {
return te.WithRetry(time.Duration(e.Retry.Delay) * time.Millisecond)
}

// UnmarshalStatus reconstruct TigrisError from HTTP error JSON body
// UnmarshalStatus reconstruct TigrisError from HTTP error JSON body.
func UnmarshalStatus(b []byte) *TigrisError {
resp := struct {
Error ErrorDetails `json:"error"`
Expand All @@ -318,7 +316,7 @@ func UnmarshalStatus(b []byte) *TigrisError {
return FromErrorDetails(&resp.Error)
}

// FromStatusError parses GRPC status from error into TigrisError
// FromStatusError parses GRPC status from error into TigrisError.
func FromStatusError(err error) *TigrisError {
st := status.Convert(err)
code := ToTigrisCode(st.Code())
Expand All @@ -336,7 +334,7 @@ func FromStatusError(err error) *TigrisError {
return &TigrisError{Code: code, Message: st.Message(), Details: details}
}

// Errorf constructs TigrisError
// Errorf constructs TigrisError.
func Errorf(c Code, format string, a ...interface{}) *TigrisError {
if c == Code_OK {
return nil
Expand Down
14 changes: 14 additions & 0 deletions api/server/v1/header.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2022 Tigris Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
Expand Down
Loading

0 comments on commit 2afe027

Please sign in to comment.