Skip to content

Commit

Permalink
Fix add lint check (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee authored Jun 11, 2024
1 parent 16e7890 commit d244a1a
Show file tree
Hide file tree
Showing 25 changed files with 91 additions and 132 deletions.
15 changes: 2 additions & 13 deletions .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.22

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
Expand All @@ -35,18 +35,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18

- name: Cache dependencies
id: cache
uses: actions/cache@v2
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
go-version: 1.22

- name: Collect dependencies
run: |
Expand Down
29 changes: 29 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
linters:
fast: true
disable-all: true
enable:
- bodyclose # checks whether HTTP response body is closed successfully
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
- 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
- dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f())
- goconst # Finds repeated strings that could be replaced by a constant
- 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
- nolintlint # Reports ill-formed or insufficient nolint directives
- rowserrcheck # checks whether Err of rows is checked successfully
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
- unconvert # Remove unnecessary type conversions
- unparam # Reports unused function parameters
# - unused # Checks Go code for unused constants, variables, functions and types

issues:
exclude-dirs:
- internal/example
- cmds
- vendor
exclude-generated: strict
exclude-dirs-use-default: true
exclude-case-sensitive: false
exclude-use-default: true
45 changes: 0 additions & 45 deletions .golangci.yml

This file was deleted.

4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ lint:
protobuf:
protobuild vendor
protobuild gen

lint:
golangci-lint --version
golangci-lint run --timeout 3m --verbose
4 changes: 0 additions & 4 deletions clients/grpcc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ func New(cfg *grpcc_config.Cfg, p Params, middlewares ...lava.Middleware) Client
middlewares: defaultMiddlewares,
}

if cfg.Client.Block {
_ = c.Get().Unwrap()
}

vars.RegisterValue(fmt.Sprintf("%s-grpc-client-config", cfg.Service.Name), cfg)
return c
}
Expand Down
6 changes: 0 additions & 6 deletions clients/grpcc/grpcc_config/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ type GrpcClientCfg struct {
MaxHeaderListSize uint32 `json:"max_header_list_size"`
DisableHealthCheck bool `json:"disable_health_check"`
Insecure bool `json:"insecure"`
Block bool `json:"block"`
IdleNum uint32 `json:"idle_num"`
WriteBuffer int `json:"write_buffer"`
ReadBuffer int `json:"read_buffer"`
Expand All @@ -105,10 +104,6 @@ func (t GrpcClientCfg) ToOpts() []grpc.DialOption {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

if t.Block {
opts = append(opts, grpc.WithBlock())
}

if !t.Proxy {
opts = append(opts, grpc.WithNoProxy())
}
Expand Down Expand Up @@ -171,7 +166,6 @@ func (t GrpcClientCfg) ToOpts() []grpc.DialOption {
}

opts = append(opts, grpc.WithDefaultCallOptions(cos...))
opts = append(opts, grpc.FailOnNonTempDialError(true))
opts = append(opts, grpc.WithKeepaliveParams(t.ClientParameters.toClientParameters()))
opts = append(opts, grpc.WithConnectParams(t.ConnectParams.toConnectParams()))
return opts
Expand Down
7 changes: 1 addition & 6 deletions clients/grpcc/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package grpcc

import (
"context"
"fmt"

"github.com/pubgo/funk/errors"
Expand Down Expand Up @@ -33,18 +32,14 @@ func buildTarget(cfg *grpcc_config.ServiceCfg) string {
}

func createConn(cfg *grpcc_config.Cfg, log log.Logger, mm []lava.Middleware) (grpc.ClientConnInterface, error) {
// 创建grpc client
ctx, cancel := context.WithTimeout(context.Background(), cfg.Client.DialTimeout)
defer cancel()

addr := buildTarget(cfg.Service)

ee := log.Info().
Str(logkey.Service, cfg.Service.Name).
Str("addr", addr)
ee.Msg("grpc client init")

conn, err := grpc.DialContext(ctx, addr, append(
conn, err := grpc.NewClient(addr, append(
append(cfg.Client.ToOpts(), grpc.WithResolvers(cfg.Resolvers...)),
grpc.WithChainUnaryInterceptor(unaryInterceptor(mm)),
grpc.WithChainStreamInterceptor(streamInterceptor(mm)))...)
Expand Down
2 changes: 1 addition & 1 deletion clients/resty/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Client struct {
func (c *Client) Do(ctx context.Context, req *Request) (r result.Result[*fasthttp.Response]) {
defer recovery.Result(&r)

reqErr := doRequest(ctx, c, req)
reqErr := doRequest(c, req)
if reqErr.IsErr() {
return r.WithErr(reqErr.Err())
}
Expand Down
2 changes: 1 addition & 1 deletion clients/resty/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (req *Request) SetBody(body any) *Request {
}

func (req *Request) SetQuery(query map[string]string) *Request {
if query == nil || len(query) == 0 {
if len(query) == 0 {
return req
}

Expand Down
33 changes: 10 additions & 23 deletions clients/resty/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func getBodyReader(rawBody interface{}) ([]byte, error) {
switch body := rawBody.(type) {
case nil:
return nil, nil
case *bytes.Buffer:
return body.Bytes(), nil
case []byte:
return body, nil
case string:
return convert.StoB(body), nil
case *bytes.Buffer:
return body.Bytes(), nil

// We prioritize *bytes.Reader here because we don't really want to
// deal with it seeking so want it to match here instead of the
Expand All @@ -92,6 +92,9 @@ func getBodyReader(rawBody interface{}) ([]byte, error) {
}
return buf, nil

case url.Values:
return convert.StoB(body.Encode()), nil

// Read all in so we can reset
case io.Reader:
buf, err := io.ReadAll(body)
Expand All @@ -100,9 +103,6 @@ func getBodyReader(rawBody interface{}) ([]byte, error) {
}
return buf, nil

case url.Values:
return convert.StoB(body.Encode()), nil

case json.Marshaler:
return body.MarshalJSON()

Expand All @@ -129,10 +129,8 @@ func IsRedirect(statusCode int) bool {

func handleHeader(c *Client, req *Request) {
header := c.cfg.DefaultHeader
if header != nil {
for k, v := range header {
req.header.Add(k, v)
}
for k, v := range header {
req.header.Add(k, v)
}
}

Expand Down Expand Up @@ -190,11 +188,7 @@ func handleContentType(c *Client, req *Request) (string, error) {
}

// doRequest data:[bytes|string|map|struct]
func doRequest(ctx context.Context, c *Client, req *Request) (rsp result.Result[*fasthttp.Request]) {
if ctx == nil {
ctx = context.Background()
}

func doRequest(c *Client, req *Request) (rsp result.Result[*fasthttp.Request]) {
r := fasthttp.AcquireRequest()

ct, err := handleContentType(c, req)
Expand Down Expand Up @@ -288,7 +282,7 @@ func toString(v any) string {
case int32:
return strconv.FormatInt(int64(t), 10)
case int64:
return strconv.FormatInt(int64(t), 10)
return strconv.FormatInt(t, 10)
case uint:
return strconv.FormatUint(uint64(t), 10)
case uint8:
Expand All @@ -298,7 +292,7 @@ func toString(v any) string {
case uint32:
return strconv.FormatUint(uint64(t), 10)
case uint64:
return strconv.FormatUint(uint64(t), 10)
return strconv.FormatUint(t, 10)
default:
return fmt.Sprintf("%v", t)
}
Expand Down Expand Up @@ -370,13 +364,6 @@ func valueOrDefault(value, def string) string {
// the Request.
var errMissingHost = errors.New("http: Request.Write on Request with no Host or URL set")

func closeRequestBody(r *http.Request) error {
if r.Body == nil {
return nil
}
return r.Body.Close()
}

// Headers that Request.Write handles itself and should be skipped.
var reqWriteExcludeHeader = map[string]bool{
"Host": true, // not in Header map anyway
Expand Down
10 changes: 8 additions & 2 deletions core/debug/debug/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/pubgo/funk/errors"
"github.com/pubgo/funk/log"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/funk/result"
"github.com/pubgo/funk/running"
"github.com/pubgo/funk/strutil"
"github.com/pubgo/lava/core/debug"
Expand Down Expand Up @@ -52,8 +53,13 @@ func init() {
if host != "localhost" && host != "127.0.0.1" {
if token != passwd {
err := errors.New("token 不存在或者密码不对")
c.WriteString(err.Error())
c.SendStatus(http.StatusInternalServerError)
if ret := result.Of(c.WriteString(err.Error())); ret.IsErr() {
return errors.WrapCaller(ret.Err())
}

if err := c.SendStatus(http.StatusInternalServerError); err != nil {
return errors.WrapCaller(err)
}
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/metrics/aaa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package metrics

import (
"github.com/pubgo/funk/log"
"github.com/uber-go/tally/v4"
tally "github.com/uber-go/tally/v4"
)

type (
Expand Down
4 changes: 3 additions & 1 deletion core/metrics/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/uber-go/tally/v4"
)

var metricKey = xid.New().String()
type ctxKet string

var metricKey = ctxKet(xid.New().String())

func CreateCtx(parent context.Context, scope tally.Scope) context.Context {
return context.WithValue(parent, metricKey, scope)
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ require (
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/invopop/yaml v0.3.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.3 // indirect
github.com/jhump/protoreflect v1.11.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand Down
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfG
github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY=
github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI=
github.com/jackc/pgconn v1.11.0/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI=
github.com/jackc/pgconn v1.13.0 h1:3L1XMNV2Zvca/8BYhzcRFS70Lr0WlDg16Di6SFGAbys=
github.com/jackc/pgconn v1.13.0/go.mod h1:AnowpAqO4CMIIJNZl2VJp+KrkAZciAkhEl0W0JIobpI=
github.com/jackc/pgconn v1.14.3 h1:bVoTr12EGANZz66nZPkMInAV/KHD2TxH9npjXXgiB3w=
github.com/jackc/pgconn v1.14.3/go.mod h1:RZbme4uasqzybK2RK5c65VsHxoyaml09lx3tXOcO/VM=
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
Expand All @@ -268,8 +269,9 @@ github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:
github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.2.0/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.3.1 h1:nwj7qwf0S+Q7ISFfBndqeLwSwxs+4DPsbRFjECT1Y4Y=
github.com/jackc/pgproto3/v2 v2.3.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.3.3 h1:1HLSx5H+tXR9pW3in3zaztoEwQYRC9SQaYUHjTSUOag=
github.com/jackc/pgproto3/v2 v2.3.3/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
Expand All @@ -278,15 +280,17 @@ github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCM
github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw=
github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM=
github.com/jackc/pgtype v1.10.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
github.com/jackc/pgtype v1.12.0 h1:Dlq8Qvcch7kiehm8wPGIW0W3KsCCHJnRacKW0UM8n5w=
github.com/jackc/pgtype v1.12.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
github.com/jackc/pgtype v1.14.0 h1:y+xUdabmyMkJLyApYuPj38mW+aAIqCe5uuBB51rH3Vw=
github.com/jackc/pgtype v1.14.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y=
github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM=
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs=
github.com/jackc/pgx/v4 v4.15.0/go.mod h1:D/zyOyXiaM1TmVWnOM18p0xdDtdakRBa0RsVGI3U3bw=
github.com/jackc/pgx/v4 v4.17.2 h1:0Ut0rpeKwvIVbMQ1KbMBU4h6wxehBI535LK6Flheh8E=
github.com/jackc/pgx/v4 v4.17.2/go.mod h1:lcxIZN44yMIrWI78a5CpucdD14hX0SBDbNRvjDBItsw=
github.com/jackc/pgx/v4 v4.18.3 h1:dE2/TrEsGX3RBprb3qryqSV9Y60iZN1C6i8IrmW9/BA=
github.com/jackc/pgx/v4 v4.18.3/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw=
github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8=
github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
Expand Down
Loading

0 comments on commit d244a1a

Please sign in to comment.