Skip to content

Commit

Permalink
refactor: optimise with golangci-lint (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbinz authored Aug 11, 2024
1 parent ae83735 commit 02363ea
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 15 deletions.
44 changes: 44 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ linters:
# Checks whether HTTP response body is closed successfully.
- bodyclose

# Checks whether net/http.Header uses canonical header.
- canonicalheader

# Detects structs that contain a context.Context field.
- containedctx

# Checks whether functions use a non-inherited context.
- contextcheck

# Detects places where loop variables are copied.
- copyloopvar

# Checks function and package cyclomatic complexity.
- cyclop

# A Go linter that checks package imports are in a list of acceptable packages.
- depguard

# Check for two durations multiplied together.
- durationcheck

Expand All @@ -54,6 +63,9 @@ linters:
# Checks for pointers to enclosing loop variables.
- exportloopref

# Detects nested contexts in loops.
- fatcontext

# Finds forced type assertions.
- forcetypeassert

Expand All @@ -63,6 +75,9 @@ linters:
# Checks that go compiler directive comments (//go:) are valid.
- gocheckcompilerdirectives

# Run exhaustiveness checks on Go "sum types".
- gochecksumtype

# Finds repeated strings that could be replaced by a constant.
- goconst

Expand Down Expand Up @@ -97,9 +112,15 @@ linters:
# An analyzer to analyze expression groups.
- grouper

# Reports interfaces with unnamed method parameters.
- inamedparam

# Detects when assignments to existing variables are not used.
- ineffassign

# Find places where for loops could make use of an integer range.
- intrange

# Finds slice declarations with non-zero initial length.
- makezero

Expand Down Expand Up @@ -136,6 +157,9 @@ linters:
# Detects missing usage of t.Parallel() method in your Go test.
- paralleltest

# Checks that fmt.Sprintf can be replaced with a faster alternative.
- perfsprint

# Finds slice declarations that could potentially be pre-allocated.
- prealloc

Expand All @@ -145,6 +169,9 @@ linters:
# Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
- revive

# Ensure consistent code style when using log/slog.
- sloglint

# It's a set of rules from staticcheck. It's not the same thing as the
# staticcheck binary. The author of staticcheck doesn't support or
# approve the use of staticcheck as a library inside golangci-lint.
Expand Down Expand Up @@ -221,6 +248,23 @@ linters-settings:
package-average: 0.0
skip-tests: false

depguard:
rules:
main:
allow:
- $gostd
- github.com/coder/websocket
- github.com/fxamacker/cbor/v2
deny:
- pkg: github.com/pkg/errors
desc: "Use errors (std) instead"
- pkg: golang.org/x/net/context
desc: "Use context (std) instead"
- pkg: "github.com/stretchr/testify"
desc: "Use gotest.tools/v3 instead"
- pkg: "nhooyr.io/websocket"
desc: "Moved to github.com/coder/websocket"

errcheck:
check-type-assertions: true
check-blank: false # TODO: activate?
Expand Down
18 changes: 9 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ const (
versionPrefix = "surrealdb-"
)

var (
regexName = regexp.MustCompile("^[A-Za-z0-9_]+$")

ErrInvalidNamespaceName = errors.New("invalid namespace name")
ErrInvalidDatabaseName = errors.New("invalid database name")

ErrContextNil = errors.New("context is nil")
)

type Client struct {
*options

Expand Down Expand Up @@ -221,15 +230,6 @@ func (c *Client) checkWebsocketConn(err error) {
}
}

var (
regexName = regexp.MustCompile("^[A-Za-z0-9_]+$")

ErrInvalidNamespaceName = fmt.Errorf("invalid namespace name")
ErrInvalidDatabaseName = fmt.Errorf("invalid database name")

ErrContextNil = errors.New("context is nil")
)

func (c *Client) init(ctx context.Context, conf Config) error {
if !regexName.MatchString(conf.Namespace) {
return ErrInvalidNamespaceName
Expand Down
2 changes: 1 addition & 1 deletion methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (c *Client) Live(ctx context.Context, query string, vars map[string]any) (<
}

for newKey := range params {
if _, err := c.Query(killCtx, fmt.Sprintf("REMOVE PARAM $%s", newKey), nil); err != nil {
if _, err := c.Query(killCtx, fmt.Sprintf("REMOVE PARAM $%s;", newKey), nil); err != nil {
c.logger.ErrorContext(killCtx, "Could not remove param.", "key", newKey, "error", err)
}
}
Expand Down
28 changes: 23 additions & 5 deletions stores.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package sdbc

import (
"bytes"
"crypto/rand"
"fmt"
"sync"

"github.com/google/uuid"
)

//
Expand Down Expand Up @@ -53,12 +53,12 @@ type requests struct {
}

func (r *requests) prepare() (string, <-chan *output) {
key := uuid.New()
key := newRequestKey() // TODO: generate multiple keys beforehand and reuse by using sync.Pool?
ch := make(chan *output)

r.store.Store(key.String(), ch)
r.store.Store(key, ch)

return key.String(), ch
return key, ch
}

func (r *requests) get(key string) (chan<- *output, bool) {
Expand Down Expand Up @@ -157,3 +157,21 @@ func (l *liveQueries) reset() {
return true
})
}

//
// -- HELPER
//

const (
requestKeyLength = 16
)

func newRequestKey() string {
key := make([]byte, requestKeyLength)

if _, err := rand.Read(key); err != nil {
return "" // TODO: error?
}

return fmt.Sprintf("%X-%X-%X-%X-%X", key[0:4], key[4:6], key[6:8], key[8:10], key[10:])
}

0 comments on commit 02363ea

Please sign in to comment.