Skip to content

Commit

Permalink
- Update linters
Browse files Browse the repository at this point in the history
- Use GOB as default codec for nil message

Signed-off-by: Valery Piashchynski <[email protected]>
  • Loading branch information
rustatian committed Jun 9, 2021
1 parent 22ef1a5 commit 65898f6
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 76 deletions.
144 changes: 92 additions & 52 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,57 +1,97 @@
# Documentation: <https://github.com/golangci/golangci-lint#config-file>

run:
timeout: 1m
skip-dirs:
- bench
linters:
- .github
- .git
skip-files:
- pkg/frame/frame_test.go
- pkg/pipe/pipe_test.go
- pkg/rpc/client_server.test.go
modules-download-mode: readonly
allow-parallel-runners: true

output:
format: colored-line-number # colored-line-number|line-number|json|tab|checkstyle|code-climate

linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0.1
gocyclo:
min-complexity: 15
godot:
scope: declarations
capital: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 3
misspell:
locale: US
lll:
line-length: 120
prealloc:
simple: true
range-loops: true
for-loops: true
nolintlint:
allow-leading-space: false
require-specific: true

linters: # All available linters list: <https://golangci-lint.run/usage/linters/>
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- exhaustive
# - funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
# - gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
# - lll
- misspell
- nakedret
# - noctx
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
# - unparam
- unused
- varcheck
- whitespace
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
- bodyclose # Checks whether HTTP response body is closed successfully
- deadcode # Finds unused code
- depguard # Go linter that checks if package imports are in a list of acceptable packages
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
- dupl # Tool for code clone detection
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
- errorlint # find code that will cause problems with the error wrapping scheme introduced in Go 1.13
- exhaustive # check exhaustiveness of enum switch statements
- exportloopref # checks for pointers to enclosing loop variables
- funlen # Tool for detection of long functions
- gochecknoglobals # Checks that no globals are present in Go code
- gochecknoinits # Checks that no init functions are present in Go code
- gocognit # Computes and checks the cognitive complexity of functions
- goconst # Finds repeated strings that could be replaced by a constant
- gocritic # The most opinionated Go source code linter
- gocyclo # Computes and checks the cyclomatic complexity of functions
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports
- goprintffuncname # Checks that printf-like functions are named with `f` at the end
- gosec # Inspects source code for security problems
- gosimple # Linter for Go source code that specializes in simplifying a code
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
- ineffassign # Detects when assignments to existing variables are not used
- misspell # Finds commonly misspelled English words in comments
- nakedret # Finds naked returns in functions greater than a specified function length
- nestif # Reports deeply nested if statements
- noctx # finds sending http request without context.Context
- nolintlint # Reports ill-formed or insufficient nolint directives
- prealloc # Finds slice declarations that could potentially be preallocated
- rowserrcheck # Checks whether Err of rows is checked successfully
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
- structcheck # Finds unused struct fields
- stylecheck # Stylecheck is a replacement for golint
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
- unconvert # Remove unnecessary type conversions
- unparam # Reports unused function parameters
- unused # Checks Go code for unused constants, variables, functions and types
- varcheck # Finds unused global variables and constants
- whitespace # Tool for detection of leading and trailing whitespace

# don't enable:
# - asciicheck
# - gochecknoglobals
# - gocognit
# - godot
# - godox
# - goerr113
# - maligned
# - nestif
# - prealloc
# - testpackage
# - wsl
issues:
exclude-rules:
- path: pkg/frame/frame_test.go
linters:
- dupl
- funlen
- scopelint
- gocognit
2 changes: 1 addition & 1 deletion internal/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ReceiveFrame(relay io.Reader, fr *frame.Frame) error {
// we should read the options
optsLen := (header.ReadHL() - 3) * frame.WORD
opts := make([]byte, optsLen)
_, err := io.ReadFull(relay, opts)
_, err = io.ReadFull(relay, opts)
if err != nil {
return errors.E(op, err)
}
Expand Down
38 changes: 20 additions & 18 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,26 @@ func (c *ClientCodec) WriteRequest(r *rpc.Request,

// writeServiceMethod to the buffer
buf.WriteString(r.ServiceMethod)

// check if message is PROTO
if m, ok := body.(proto.Message); ok {
fr.WriteFlags(frame.CODEC_PROTO)
b, err := proto.Marshal(m)
if err != nil {
return errors.E(op, err)
}
buf.Write(b)
} else {
// use fallback as gob
fr.WriteFlags(frame.CODEC_GOB)

enc := gob.NewEncoder(buf)
// write data to the gob
err := enc.Encode(body)
if err != nil {
return errors.E(op, err)
// use fallback as gob
fr.WriteFlags(frame.CODEC_GOB)

if body != nil {
switch m := body.(type) {
// check if message is PROTO
case proto.Message:
fr.WriteFlags(frame.CODEC_PROTO)
b, err := proto.Marshal(m)
if err != nil {
return errors.E(op, err)
}
buf.Write(b)
default:
enc := gob.NewEncoder(buf)
// write data to the gob
err := enc.Encode(body)
if err != nil {
return errors.E(op, err)
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/rpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import (
"net/rpc"
"sync"

j "github.com/json-iterator/go"
"github.com/spiral/errors"
"github.com/spiral/goridge/v3/pkg/frame"
"github.com/spiral/goridge/v3/pkg/relay"
"github.com/spiral/goridge/v3/pkg/socket"
"go.uber.org/multierr"
)

var json = j.ConfigCompatibleWithStandardLibrary

// Codec represent net/rpc bridge over Goridge socket relay.
type Codec struct {
relay relay.Relay
Expand All @@ -38,7 +35,7 @@ func NewCodecWithRelay(relay relay.Relay) *Codec {
}

// WriteResponse marshals response, byte slice or error to remote party.
func (c *Codec) WriteResponse(r *rpc.Response, body interface{}) error {
func (c *Codec) WriteResponse(r *rpc.Response, body interface{}) error { //nolint:funlen
const op = errors.Op("codec: write response")
fr := frame.NewFrame()

Expand Down
1 change: 1 addition & 0 deletions pkg/rpc/decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/gob"

json "github.com/json-iterator/go"
"github.com/spiral/errors"
"github.com/spiral/goridge/v3/pkg/frame"
"github.com/vmihailenco/msgpack"
Expand Down
1 change: 1 addition & 0 deletions pkg/rpc/encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/gob"
"io"

json "github.com/json-iterator/go"
"github.com/spiral/errors"
"github.com/vmihailenco/msgpack"
"google.golang.org/protobuf/proto"
Expand Down
2 changes: 1 addition & 1 deletion pkg/shared_memory/posix/posix_shm.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (s *SharedMemorySegment) writeBuffer(src []byte, dst []byte) {
copy(dst, src)
}

// Clear by behaviour is similar to the std::memset(..., 0, ...)
// Clear by behavior is similar to the std::memset(..., 0, ...)
func (s *SharedMemorySegment) Clear() {
for i := 0; i < len(s.data); i++ {
s.data[i] = 0
Expand Down

0 comments on commit 65898f6

Please sign in to comment.