Skip to content

fix(clients): upgrade linter #4851

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

Open
wants to merge 1 commit into
base: feat/go-refacto
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ runs:
if: ${{ inputs.language == 'go' }}
shell: bash
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.60.3
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.1.6
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
go install golang.org/x/tools/cmd/[email protected]

- name: Cache golangci-lint analysis
if: ${{ inputs.language == 'go' }}
Expand Down
134 changes: 74 additions & 60 deletions clients/algoliasearch-client-go/.golangci.yml
Original file line number Diff line number Diff line change
@@ -1,71 +1,85 @@
linters-settings:
govet:
enable-all: true
disable:
- fieldalignment

revive:
rules:
- name: var-naming
disabled: true

version: "2"
run:
concurrency: 2
linters:
enable-all: true

default: all
disable:
- godox
- bodyclose
- contextcheck
- interfacebloat
- gci
- gosmopolitan
- wsl
- varnamelen
- nlreturn
- canonicalheader
- containedctx
- copyloopvar
- cyclop
- depguard
- dupl
- dupword
- err113
- gochecknoglobals
- exhaustruct
- exhaustive
- depguard
- lll
- exhaustruct
- forbidigo
- gochecknoinits
- cyclop
- errorlint
- gomnd
- tagliatelle
- nilnil
- stylecheck
- musttag
- errchkjson
- nonamedreturns
- inamedparam
- ineffassign
- dupword
- nestif
- goconst
- funlen
- dupl
- unparam
- gochecknoglobals
- gocognit
- forcetypeassert
- wastedassign
- goconst
- gocyclo
- maintidx
- copyloopvar
- inamedparam
- intrange
- canonicalheader
- lll
- maintidx
- mnd
- nestif
- nilnil
- nonamedreturns
- perfsprint
- containedctx

# Deprecated
- execinquery
- exportloopref

issues:
exclude-generated: disable

run:
concurrency: 2
timeout: 10m
- recvcheck
- tagliatelle
- varnamelen
settings:
govet:
enable-all: true
disable:
- fieldalignment
revive:
rules:
- name: var-naming
disabled: true
staticcheck:
checks: ["all", "-ST1005", "-ST1016"]
exclusions:
generated: disable
presets:
- comments
- std-error-handling
# old client is not linted
paths-except:
- algolia/abtesting
- algolia/analytics
- algolia/composition
- algolia/ingestion
- algolia/insights
- algolia/monitoring
- algolia/personalization
- algolia/query-suggestions
- algolia/recommend
- algolia/search
formatters:
enable:
- gofmt
- gofumpt
- goimports
- golines
settings:
golines:
max-len: 150
exclusions:
generated: disable
# old client is not formatted
paths:
- algolia/abtesting
- algolia/analytics
- algolia/composition
- algolia/ingestion
- algolia/insights
- algolia/monitoring
- algolia/personalization
- algolia/query-suggestions
- algolia/recommend
- algolia/search
4 changes: 4 additions & 0 deletions clients/algoliasearch-client-go/algolia/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func Display(input any) {
if !debug {
return
}

start := time.Now()

var msg string
switch v := input.(type) {
case *http.Request:
Expand All @@ -40,6 +42,7 @@ func Display(input any) {
default:
msg = fmt.Sprintf("do not know how to display %#v", v)
}

Println(msg)
fmt.Printf("took %s\n", time.Since(start))
}
Expand All @@ -52,6 +55,7 @@ func Printf(format string, a ...any) {
if !debug {
return
}

msg := fmt.Sprintf(format, a...)
fmt.Printf("> ALGOLIA DEBUG: %s", msg)
}
9 changes: 9 additions & 0 deletions clients/algoliasearch-client-go/algolia/debug/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ func copyReadCloser(r io.ReadCloser) (io.ReadCloser, string) {
if r == nil {
return nil, ""
}

data, err := io.ReadAll(r)
_ = r.Close()

if err != nil {
return nil, ""
}

return io.NopCloser(bytes.NewReader(data)), string(data)
}

Expand All @@ -29,10 +32,12 @@ func decodeGzipContent(in string) (string, error) {
if err != nil {
return in, fmt.Errorf("cannot open content with gzip.Reader: %w", err)
}

out, err := io.ReadAll(gr)
if err != nil {
return in, fmt.Errorf("cannot read content from gzip.Reader: %w", err)
}

return string(out), nil
}

Expand All @@ -58,10 +63,12 @@ func extractBody(body io.ReadCloser, c compression.Compression) (io.ReadCloser,

func prettyPrintJSON(input string) string {
var b bytes.Buffer

err := json.Indent(&b, []byte(input), "\t", " ")
if err != nil {
return input
}

return strings.TrimSuffix(b.String(), "\n")
}

Expand All @@ -88,8 +95,10 @@ func debugRequest(req *http.Request) string {
if strings.Contains(strings.ToLower(k), "algolia") {
str = strings.Repeat("*", len(str))
}

msg += fmt.Sprintf("\theader=%s:%q\n", k, str)
}

msg += fmt.Sprintf("\tbody=\n\t%s\n", prettyPrintJSON(body))

return msg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ func (e *NoMoreHostToTryError) IntermediateNetworkErrors() []error {

func (e *NoMoreHostToTryError) Error() string {
if len(e.intermediateNetworkErrors) > 0 {
return fmt.Errorf("all hosts have been contacted unsuccessfully, it can either be a server or a network error or wrong appID/key credentials were used. %w", errors.Join(e.intermediateNetworkErrors...)).Error()
return fmt.Errorf("all hosts have been contacted unsuccessfully, it can either be a server or a network error or wrong appID/key credentials were used. %w", errors.Join(e.intermediateNetworkErrors...)).
Error()
}

return "all hosts have been contacted unsuccessfully, it can either be a server or a network error or wrong appID/key credentials were used. You can use 'ExposeIntermediateNetworkErrors: true' in the config to investigate."
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Configuration struct {
AppID string
ApiKey string
ApiKey string //nolint:staticcheck

Hosts []StatefulHost
DefaultHeader map[string]string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transport

import (
"context"
"errors"
"net"
"strings"
"sync"
Expand Down Expand Up @@ -99,16 +100,19 @@ func (s *RetryStrategy) Decide(h Host, code int, err error) Outcome {

if err == nil && is2xx(code) {
s.markUp(h)

return Success
}

if isTimeoutError(err) {
s.markTimeout(h)

return Retry
}

if !(isZero(code) || is4xx(code) || is2xx(code)) || isNetworkError(err) {
if (!isZero(code) && !is4xx(code) && !is2xx(code)) || isNetworkError(err) {
s.markDown(h)

return Retry
}

Expand All @@ -119,6 +123,7 @@ func (s *RetryStrategy) markUp(host Host) {
for _, h := range s.hosts {
if h.host == host.host {
h.markUp()

return
}
}
Expand All @@ -128,6 +133,7 @@ func (s *RetryStrategy) markTimeout(host Host) {
for _, h := range s.hosts {
if h.host == host.host {
h.markTimeout()

return
}
}
Expand All @@ -137,6 +143,7 @@ func (s *RetryStrategy) markDown(host Host) {
for _, h := range s.hosts {
if h.host == host.host {
h.markDown()

return
}
}
Expand All @@ -146,7 +153,9 @@ func isNetworkError(err error) bool {
if err == nil {
return false
}
_, ok := err.(net.Error)

var netErr net.Error
ok := errors.As(err, &netErr)
// We need to ensure that the error is a net.Error but not a
// context.DeadlineExceeded error (which is actually a net.Error), because
// we do not want to consider context.DeadlineExceeded as an error.
Expand All @@ -157,6 +166,7 @@ func isTimeoutError(err error) bool {
if err == nil {
return false
}

return strings.Contains(err.Error(), context.DeadlineExceeded.Error())
}

Expand Down
15 changes: 13 additions & 2 deletions clients/algoliasearch-client-go/algolia/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,36 @@ func (t *Transport) Request(ctx context.Context, req *http.Request, k call.Kind,
// already cancelled.
if ctx.Err() != nil {
cancel()

return res, nil, err
}

switch t.retryStrategy.Decide(h, code, err) {
case Success, Failure:
body, errBody := io.ReadAll(res.Body)
errClose := res.Body.Close()

cancel()

res.Body = io.NopCloser(bytes.NewBuffer(body))
if errBody != nil {
return res, nil, fmt.Errorf("cannot read body: %v", errBody)
return res, nil, fmt.Errorf("cannot read body: %w", errBody)
}

if errClose != nil {
return res, nil, fmt.Errorf("cannot close response's body: %v", errClose)
return res, nil, fmt.Errorf("cannot close response's body: %w", errClose)
}

return res, body, err
default:
if err != nil {
intermediateNetworkErrors = append(intermediateNetworkErrors, err)
}

if res != nil && res.Body != nil {
if err = res.Body.Close(); err != nil {
cancel()

return res, nil, fmt.Errorf("cannot close response's body before retry: %w", err)
}
}
Expand All @@ -142,7 +149,9 @@ func (t *Transport) request(req *http.Request, host Host, timeout time.Duration,

if err != nil {
msg := fmt.Sprintf("cannot perform request:\n\terror=%v\n\tmethod=%s\n\turl=%s", err, req.Method, req.URL)

var nerr net.Error

if errors.As(err, &nerr) {
// Because net.Error and error have different meanings for the
// retry strategy, we cannot simply return a new error, which
Expand All @@ -154,6 +163,7 @@ func (t *Transport) request(req *http.Request, host Host, timeout time.Duration,
} else {
err = errors.New(msg)
}

return nil, err
}

Expand All @@ -164,5 +174,6 @@ func shouldCompress(c compression.Compression, method string, body any) bool {
isValidMethod := method == http.MethodPut || method == http.MethodPost
isCompressionEnabled := c != compression.NONE
isBodyNonEmpty := body != nil

return isCompressionEnabled && isValidMethod && isBodyNonEmpty
}
Loading
Loading