From 736fb38e793a95f21e14846718590446733e9b85 Mon Sep 17 00:00:00 2001 From: Kagaya Date: Thu, 9 Sep 2021 20:45:08 +0800 Subject: [PATCH] fix: examples lint error (#1454) * fix example lint error * use latest version lint * ignore zap Sync error --- .github/workflows/lint.yml | 3 +-- Makefile | 5 ++-- examples/benchmark/main.go | 20 ++++++++-------- examples/blog/internal/biz/article.go | 5 ++-- examples/blog/internal/data/article.go | 4 ++-- examples/blog/internal/data/data.go | 1 + .../blog/internal/data/ent/schema/article.go | 3 ++- .../blog/internal/data/ent/schema/comment.go | 3 ++- examples/blog/internal/data/ent/schema/tag.go | 3 ++- examples/blog/internal/data/redis.go | 1 + examples/blog/internal/server/grpc.go | 2 +- examples/blog/internal/server/http.go | 2 +- examples/blog/internal/service/article.go | 4 ++-- examples/config/apollo/main.go | 14 ++++++----- examples/errors/client/main.go | 4 +++- examples/errors/server/main.go | 2 +- examples/event/kafka/kafka.go | 11 +++++---- examples/event/memory/memory.go | 16 ++++++++----- examples/event/memory/memory_test.go | 4 ++-- examples/event/receiver/main.go | 7 +++--- examples/event/sender/main.go | 1 + examples/header/server/main.go | 2 +- examples/helloworld/client/main.go | 2 +- examples/helloworld/server/main.go | 2 +- examples/http/echo/main.go | 3 ++- examples/http/errors/main.go | 2 +- examples/http/upload/main.go | 2 +- examples/i18n/internal/biz/greeter.go | 1 + examples/i18n/internal/data/data.go | 5 ++-- examples/i18n/internal/data/greeter.go | 1 + .../pkg/middleware/localize/localize.go | 1 + examples/i18n/internal/server/grpc.go | 2 +- examples/i18n/internal/server/http.go | 2 +- examples/i18n/internal/service/greeter.go | 3 ++- examples/log/logrus_test.go | 24 ++++++++++++++++--- examples/log/zap_test.go | 3 ++- examples/metadata/server/main.go | 2 +- examples/metrics/main.go | 2 +- examples/registry/consul/client/main.go | 1 - examples/registry/registry_test.go | 5 ++-- examples/stream/client/main.go | 1 - examples/stream/service/hello.go | 2 ++ examples/swagger/server/main.go | 2 +- examples/tls/server/main.go | 2 +- examples/tls/tls_test.go | 5 ++-- examples/traces/app/message/main.go | 2 +- examples/traces/app/user/main.go | 2 +- examples/validate/main.go | 4 ++-- hack/.lintcheck_failures | 1 - hack/tools.sh | 2 +- transport/http/server_test.go | 2 +- 51 files changed, 123 insertions(+), 82 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 70c19cc78..a5c3a0b29 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -31,7 +31,6 @@ jobs: - name: Lint uses: golangci/golangci-lint-action@v2 with: - version: v1.42 + version: latest working-directory: ${{ matrix.workdir }} - skip-go-installation: true skip-pkg-cache: true diff --git a/Makefile b/Makefile index 0b11ede69..842c95625 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,6 @@ BIN := "" TOOLS_SHELL="./hack/tools.sh" # golangci-lint LINTER := bin/golangci-lint -$(LINTER): - curl -L https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.42.0 # check GOBIN ifneq ($(GOBIN),) @@ -22,6 +20,9 @@ else endif endif +$(LINTER): + curl -SL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s latest + all: @cd cmd/kratos && go build && cd - &> /dev/null @cd cmd/protoc-gen-go-errors && go build && cd - &> /dev/null diff --git a/examples/benchmark/main.go b/examples/benchmark/main.go index fa3ae4f56..08021c774 100644 --- a/examples/benchmark/main.go +++ b/examples/benchmark/main.go @@ -36,17 +36,17 @@ func main() { if argsLen > 1 { webFramework = args[1] } - if argsLen > 2 { + if argsLen > 2 { //nolint:gomnd sleepTime, _ = strconv.Atoi(args[2]) if sleepTime == -1 { cpuBound = true sleepTime = 0 } } - if argsLen > 3 { + if argsLen > 3 { //nolint:gomnd port, _ = strconv.Atoi(args[3]) } - if argsLen > 4 { + if argsLen > 4 { //nolint:gomnd samplingPoint, _ = strconv.Atoi(args[4]) } sleepTimeDuration = time.Duration(sleepTime) * time.Millisecond @@ -108,14 +108,14 @@ func kratosHandle(w http2.ResponseWriter, q *http2.Request) { runtime.Gosched() } } - w.Write(message) + _, _ = w.Write(message) } func GinServer() { gin.SetMode(gin.ReleaseMode) mux := gin.New() mux.GET("/", ginHandler) - mux.Run(":" + strconv.Itoa(port)) + _ = mux.Run(":" + strconv.Itoa(port)) } func ginHandler(c *gin.Context) { @@ -128,13 +128,13 @@ func ginHandler(c *gin.Context) { runtime.Gosched() } } - c.Writer.Write(message) + _, _ = c.Writer.Write(message) } func EchoServer() { e := echo.New() e.GET("/", echoHandler) - e.Start(":" + strconv.Itoa(port)) + _ = e.Start(":" + strconv.Itoa(port)) } func echoHandler(c echo.Context) error { @@ -147,7 +147,7 @@ func echoHandler(c echo.Context) error { runtime.Gosched() } } - c.Response().Write(message) + _, _ = c.Response().Write(message) return nil } @@ -155,7 +155,7 @@ func MuxServer() { r := mux.NewRouter() r.HandleFunc("/", muxHandle) http2.Handle("/", r) - http2.ListenAndServe(":"+strconv.Itoa(port), r) + _ = http2.ListenAndServe(":"+strconv.Itoa(port), r) } func muxHandle(w http2.ResponseWriter, q *http2.Request) { @@ -168,7 +168,7 @@ func muxHandle(w http2.ResponseWriter, q *http2.Request) { runtime.Gosched() } } - w.Write(message) + _, _ = w.Write(message) } func pow(targetBits int) { diff --git a/examples/blog/internal/biz/article.go b/examples/blog/internal/biz/article.go index 291d2e252..bf5a6bc57 100644 --- a/examples/blog/internal/biz/article.go +++ b/examples/blog/internal/biz/article.go @@ -2,12 +2,13 @@ package biz import ( "context" - "github.com/go-kratos/kratos/v2/log" "time" + + "github.com/go-kratos/kratos/v2/log" ) type Article struct { - Id int64 + ID int64 Title string Content string CreatedAt time.Time diff --git a/examples/blog/internal/data/article.go b/examples/blog/internal/data/article.go index d951746af..c13cdf879 100644 --- a/examples/blog/internal/data/article.go +++ b/examples/blog/internal/data/article.go @@ -29,7 +29,7 @@ func (ar *articleRepo) ListArticle(ctx context.Context) ([]*biz.Article, error) rv := make([]*biz.Article, 0) for _, p := range ps { rv = append(rv, &biz.Article{ - Id: p.ID, + ID: p.ID, Title: p.Title, Content: p.Content, CreatedAt: p.CreatedAt, @@ -45,7 +45,7 @@ func (ar *articleRepo) GetArticle(ctx context.Context, id int64) (*biz.Article, return nil, err } return &biz.Article{ - Id: p.ID, + ID: p.ID, Title: p.Title, Content: p.Content, CreatedAt: p.CreatedAt, diff --git a/examples/blog/internal/data/data.go b/examples/blog/internal/data/data.go index 0bbaa829c..dc92829ed 100644 --- a/examples/blog/internal/data/data.go +++ b/examples/blog/internal/data/data.go @@ -16,6 +16,7 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" + // init mysql driver _ "github.com/go-sql-driver/mysql" ) diff --git a/examples/blog/internal/data/ent/schema/article.go b/examples/blog/internal/data/ent/schema/article.go index b43ca34f1..795785265 100644 --- a/examples/blog/internal/data/ent/schema/article.go +++ b/examples/blog/internal/data/ent/schema/article.go @@ -1,11 +1,12 @@ package schema import ( + "time" + "entgo.io/ent" "entgo.io/ent/dialect" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" - "time" ) // Article holds the schema definition for the Article entity. diff --git a/examples/blog/internal/data/ent/schema/comment.go b/examples/blog/internal/data/ent/schema/comment.go index 0f5e2c024..37eafb7bc 100644 --- a/examples/blog/internal/data/ent/schema/comment.go +++ b/examples/blog/internal/data/ent/schema/comment.go @@ -1,11 +1,12 @@ package schema import ( + "time" + "entgo.io/ent" "entgo.io/ent/dialect" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" - "time" ) // Comment holds the schema definition for the Comment entity. diff --git a/examples/blog/internal/data/ent/schema/tag.go b/examples/blog/internal/data/ent/schema/tag.go index 229ae6d9d..3db2cb6bc 100644 --- a/examples/blog/internal/data/ent/schema/tag.go +++ b/examples/blog/internal/data/ent/schema/tag.go @@ -1,11 +1,12 @@ package schema import ( + "time" + "entgo.io/ent" "entgo.io/ent/dialect" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" - "time" ) // Tag holds the schema definition for the Tag entity. diff --git a/examples/blog/internal/data/redis.go b/examples/blog/internal/data/redis.go index 088290e2f..3bbffa9a6 100644 --- a/examples/blog/internal/data/redis.go +++ b/examples/blog/internal/data/redis.go @@ -3,6 +3,7 @@ package data import ( "context" "fmt" + "github.com/go-redis/redis/v8" ) diff --git a/examples/blog/internal/server/grpc.go b/examples/blog/internal/server/grpc.go index 6b14380dc..4bf89fab7 100644 --- a/examples/blog/internal/server/grpc.go +++ b/examples/blog/internal/server/grpc.go @@ -14,7 +14,7 @@ import ( // NewGRPCServer new a gRPC server. func NewGRPCServer(c *conf.Server, logger log.Logger, blog *service.BlogService) *grpc.Server { - var opts = []grpc.ServerOption{ + opts := []grpc.ServerOption{ grpc.Middleware( recovery.Recovery(), tracing.Server(), diff --git a/examples/blog/internal/server/http.go b/examples/blog/internal/server/http.go index 340713fd1..f17a882ce 100644 --- a/examples/blog/internal/server/http.go +++ b/examples/blog/internal/server/http.go @@ -14,7 +14,7 @@ import ( // NewHTTPServer new a HTTP server. func NewHTTPServer(c *conf.Server, logger log.Logger, blog *service.BlogService) *http.Server { - var opts = []http.ServerOption{ + opts := []http.ServerOption{ http.Middleware( recovery.Recovery(), tracing.Server(), diff --git a/examples/blog/internal/service/article.go b/examples/blog/internal/service/article.go index e72a7d189..fd16fd070 100644 --- a/examples/blog/internal/service/article.go +++ b/examples/blog/internal/service/article.go @@ -50,7 +50,7 @@ func (s *BlogService) GetArticle(ctx context.Context, req *pb.GetArticleRequest) if err != nil { return nil, err } - return &pb.GetArticleReply{Article: &pb.Article{Id: p.Id, Title: p.Title, Content: p.Content, Like: p.Like}}, nil + return &pb.GetArticleReply{Article: &pb.Article{Id: p.ID, Title: p.Title, Content: p.Content, Like: p.Like}}, nil } func (s *BlogService) ListArticle(ctx context.Context, req *pb.ListArticleRequest) (*pb.ListArticleReply, error) { @@ -58,7 +58,7 @@ func (s *BlogService) ListArticle(ctx context.Context, req *pb.ListArticleReques reply := &pb.ListArticleReply{} for _, p := range ps { reply.Results = append(reply.Results, &pb.Article{ - Id: p.Id, + Id: p.ID, Title: p.Title, Content: p.Content, }) diff --git a/examples/config/apollo/main.go b/examples/config/apollo/main.go index 23d0c7dbd..1e756aef8 100644 --- a/examples/config/apollo/main.go +++ b/examples/config/apollo/main.go @@ -28,6 +28,8 @@ func main() { if err != nil { panic(err) } + log.Printf("service: %s", name) + // Defines the config JSON Field var v struct { Name string `json:"name"` @@ -35,7 +37,7 @@ func main() { } // Unmarshal the config to struct - if err := c.Scan(&v); err != nil { + if err = c.Scan(&v); err != nil { panic(err) } log.Printf("config: %+v", v) @@ -48,12 +50,12 @@ func main() { log.Printf("service: %s", name) // watch key - if err := c.Watch("name", func(key string, value config.Value) { - name, err := value.String() - if err != nil { - panic(err) + if err = c.Watch("name", func(key string, value config.Value) { + n, e := value.String() + if e != nil { + panic(e) } - log.Printf("config changed: %s = %s\n", key, name) + log.Printf("config changed: %s = %s\n", key, n) }); err != nil { panic(err) } diff --git a/examples/errors/client/main.go b/examples/errors/client/main.go index b9b9807ec..54df1de6a 100644 --- a/examples/errors/client/main.go +++ b/examples/errors/client/main.go @@ -4,6 +4,8 @@ import ( "context" "log" + stdhttp "net/http" + "github.com/go-kratos/kratos/examples/errors/api" pb "github.com/go-kratos/kratos/examples/helloworld/helloworld" "github.com/go-kratos/kratos/v2/errors" @@ -27,7 +29,7 @@ func callHTTP() { client := pb.NewGreeterHTTPClient(conn) reply, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "empty"}) if err != nil { - if errors.Code(err) == 500 { + if errors.Code(err) == stdhttp.StatusInternalServerError { log.Println(err) } if api.IsUserNotFound(err) { diff --git a/examples/errors/server/main.go b/examples/errors/server/main.go index 45f7ea6a3..089af6c0d 100644 --- a/examples/errors/server/main.go +++ b/examples/errors/server/main.go @@ -18,7 +18,7 @@ var ( // Name is the name of the compiled software. Name = "errors" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" ) // server is used to implement helloworld.GreeterServer. diff --git a/examples/event/kafka/kafka.go b/examples/event/kafka/kafka.go index e4a3b6fec..dd4c05007 100644 --- a/examples/event/kafka/kafka.go +++ b/examples/event/kafka/kafka.go @@ -2,14 +2,17 @@ package kafka import ( "context" + "log" + "github.com/go-kratos/kratos/examples/event/event" "github.com/segmentio/kafka-go" - "log" ) -var _ event.Sender = (*kafkaSender)(nil) -var _ event.Receiver = (*kafkaReceiver)(nil) -var _ event.Event = (*Message)(nil) +var ( + _ event.Sender = (*kafkaSender)(nil) + _ event.Receiver = (*kafkaReceiver)(nil) + _ event.Event = (*Message)(nil) +) type Message struct { key string diff --git a/examples/event/memory/memory.go b/examples/event/memory/memory.go index 1b3b08005..67fe471a2 100644 --- a/examples/event/memory/memory.go +++ b/examples/event/memory/memory.go @@ -2,14 +2,17 @@ package memory import ( "context" - "github.com/go-kratos/kratos/examples/event/event" "log" "sync" + + "github.com/go-kratos/kratos/examples/event/event" ) -var _ event.Sender = (*memorySender)(nil) -var _ event.Receiver = (*memoryReceiver)(nil) -var _ event.Event = (*Message)(nil) +var ( + _ event.Sender = (*memorySender)(nil) + _ event.Receiver = (*memoryReceiver)(nil) + _ event.Event = (*Message)(nil) +) var ( chanMap = struct { @@ -47,6 +50,7 @@ func (m *memorySender) Send(ctx context.Context, msg event.Event) error { } return nil } + func (m *memorySender) Close() error { return nil } @@ -74,9 +78,9 @@ func (m *memoryReceiver) Close() error { func NewMemory(topic string) (event.Sender, event.Receiver) { chanMap.RLock() if _, ok := chanMap.cm[topic]; !ok { - //chanMap.Lock() + // chanMap.Lock() chanMap.cm[topic] = make(chan *Message, ChanSize) - //chanMap.Unlock() + // chanMap.Unlock() } defer chanMap.RUnlock() diff --git a/examples/event/memory/memory_test.go b/examples/event/memory/memory_test.go index 3d6ec2cd3..813bcf86a 100644 --- a/examples/event/memory/memory_test.go +++ b/examples/event/memory/memory_test.go @@ -3,9 +3,10 @@ package memory import ( "context" "fmt" - "github.com/go-kratos/kratos/examples/event/event" "testing" "time" + + "github.com/go-kratos/kratos/examples/event/event" ) func TestSendAndReceive(t *testing.T) { @@ -14,7 +15,6 @@ func TestSendAndReceive(t *testing.T) { t.Log(fmt.Sprintf("key:%s, value:%s\n", event.Key(), event.Value())) return nil }) - if err != nil { t.Error(err) } diff --git a/examples/event/receiver/main.go b/examples/event/receiver/main.go index af39c3457..0497a6269 100644 --- a/examples/event/receiver/main.go +++ b/examples/event/receiver/main.go @@ -19,10 +19,9 @@ func main() { panic(err) } receive(receiver) - select { - case <-sigs: - _ = receiver.Close() - } + + <-sigs + _ = receiver.Close() } func receive(receiver event.Receiver) { diff --git a/examples/event/sender/main.go b/examples/event/sender/main.go index 4443306af..3cd6ecfae 100644 --- a/examples/event/sender/main.go +++ b/examples/event/sender/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "github.com/go-kratos/kratos/examples/event/event" "github.com/go-kratos/kratos/examples/event/kafka" ) diff --git a/examples/header/server/main.go b/examples/header/server/main.go index 5b2db1363..e15c5857a 100644 --- a/examples/header/server/main.go +++ b/examples/header/server/main.go @@ -17,7 +17,7 @@ var ( // Name is the name of the compiled software. Name = "helloworld" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" ) // server is used to implement helloworld.GreeterServer. diff --git a/examples/helloworld/client/main.go b/examples/helloworld/client/main.go index 1be1c779b..a371acb31 100644 --- a/examples/helloworld/client/main.go +++ b/examples/helloworld/client/main.go @@ -36,7 +36,7 @@ func callHTTP() { log.Printf("[http] SayHello %s\n", reply.Message) // returns error - reply, err = client.SayHello(context.Background(), &pb.HelloRequest{Name: "error"}) + _, err = client.SayHello(context.Background(), &pb.HelloRequest{Name: "error"}) if err != nil { log.Printf("[http] SayHello error: %v\n", err) } diff --git a/examples/helloworld/server/main.go b/examples/helloworld/server/main.go index f60cdfcb5..33a876b73 100644 --- a/examples/helloworld/server/main.go +++ b/examples/helloworld/server/main.go @@ -18,7 +18,7 @@ var ( // Name is the name of the compiled software. Name = "helloworld" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" ) // server is used to implement helloworld.GreeterServer. diff --git a/examples/http/echo/main.go b/examples/http/echo/main.go index ed3599120..ebc8117b7 100644 --- a/examples/http/echo/main.go +++ b/examples/http/echo/main.go @@ -1,10 +1,11 @@ package main import ( + "log" + "github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2/transport/http" "github.com/labstack/echo/v4" - "log" ) func main() { diff --git a/examples/http/errors/main.go b/examples/http/errors/main.go index 335bc7d91..09afb89fb 100644 --- a/examples/http/errors/main.go +++ b/examples/http/errors/main.go @@ -41,7 +41,7 @@ func errorEncoder(w stdhttp.ResponseWriter, r *stdhttp.Request, err error) { } w.Header().Set("Content-Type", "application/"+codec.Name()) w.WriteHeader(se.Code) - w.Write(body) + _, _ = w.Write(body) } func main() { diff --git a/examples/http/upload/main.go b/examples/http/upload/main.go index 794c96917..db97ba37c 100644 --- a/examples/http/upload/main.go +++ b/examples/http/upload/main.go @@ -19,7 +19,7 @@ func uploadFile(ctx http.Context) error { } defer file.Close() - f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666) + f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0o666) if err != nil { return err } diff --git a/examples/i18n/internal/biz/greeter.go b/examples/i18n/internal/biz/greeter.go index e82021393..8b35979ed 100644 --- a/examples/i18n/internal/biz/greeter.go +++ b/examples/i18n/internal/biz/greeter.go @@ -2,6 +2,7 @@ package biz import ( "context" + "github.com/go-kratos/kratos/v2/log" ) diff --git a/examples/i18n/internal/data/data.go b/examples/i18n/internal/data/data.go index d45fc5daa..269e9f18d 100644 --- a/examples/i18n/internal/data/data.go +++ b/examples/i18n/internal/data/data.go @@ -10,14 +10,13 @@ import ( var ProviderSet = wire.NewSet(NewData, NewGreeterRepo) // Data . -type Data struct { - // TODO wrapped database client +type Data struct { // TODO wrapped database client } // NewData . func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) { cleanup := func() { - logger.Log(log.LevelInfo, "closing the data resources") + _ = logger.Log(log.LevelInfo, "closing the data resources") } return &Data{}, cleanup, nil } diff --git a/examples/i18n/internal/data/greeter.go b/examples/i18n/internal/data/greeter.go index 2fd947ef9..20002cb99 100644 --- a/examples/i18n/internal/data/greeter.go +++ b/examples/i18n/internal/data/greeter.go @@ -2,6 +2,7 @@ package data import ( "context" + "github.com/go-kratos/kratos/examples/i18n/internal/biz" "github.com/go-kratos/kratos/v2/log" ) diff --git a/examples/i18n/internal/pkg/middleware/localize/localize.go b/examples/i18n/internal/pkg/middleware/localize/localize.go index c2d611bc3..d865a647c 100644 --- a/examples/i18n/internal/pkg/middleware/localize/localize.go +++ b/examples/i18n/internal/pkg/middleware/localize/localize.go @@ -2,6 +2,7 @@ package localize import ( "context" + "github.com/BurntSushi/toml" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language" diff --git a/examples/i18n/internal/server/grpc.go b/examples/i18n/internal/server/grpc.go index a8da23aec..2858baece 100644 --- a/examples/i18n/internal/server/grpc.go +++ b/examples/i18n/internal/server/grpc.go @@ -15,7 +15,7 @@ import ( // NewGRPCServer new a gRPC server. func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *grpc.Server { - var opts = []grpc.ServerOption{ + opts := []grpc.ServerOption{ grpc.Middleware( recovery.Recovery(), tracing.Server(), diff --git a/examples/i18n/internal/server/http.go b/examples/i18n/internal/server/http.go index 28aaa5680..cb94fdc37 100644 --- a/examples/i18n/internal/server/http.go +++ b/examples/i18n/internal/server/http.go @@ -16,7 +16,7 @@ import ( // NewHTTPServer new a HTTP server. func NewHTTPServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *http.Server { - var opts = []http.ServerOption{ + opts := []http.ServerOption{ http.Middleware( recovery.Recovery(), tracing.Server(), diff --git a/examples/i18n/internal/service/greeter.go b/examples/i18n/internal/service/greeter.go index 030e4b52d..8ddf181da 100644 --- a/examples/i18n/internal/service/greeter.go +++ b/examples/i18n/internal/service/greeter.go @@ -2,10 +2,11 @@ package service import ( "context" + "github.com/go-kratos/kratos/examples/i18n/internal/pkg/middleware/localize" "github.com/nicksnyder/go-i18n/v2/i18n" - "github.com/go-kratos/kratos/examples/i18n/api/helloworld/v1" + v1 "github.com/go-kratos/kratos/examples/i18n/api/helloworld/v1" "github.com/go-kratos/kratos/examples/i18n/internal/biz" "github.com/go-kratos/kratos/v2/log" ) diff --git a/examples/log/logrus_test.go b/examples/log/logrus_test.go index c8e204e0a..62f1cb481 100644 --- a/examples/log/logrus_test.go +++ b/examples/log/logrus_test.go @@ -18,9 +18,27 @@ func TestLoggerLog(t *testing.T) { kvs []interface{} want string }{ - "json format": {level: logrus.InfoLevel, formatter: &logrus.JSONFormatter{}, logLevel: log.LevelInfo, kvs: []interface{}{"case", "json format", "msg", "1"}, want: `{"case":"json format","level":"info","msg":"1"`}, - "level unmatch": {level: logrus.InfoLevel, formatter: &logrus.JSONFormatter{}, logLevel: log.LevelDebug, kvs: []interface{}{"case", "level unmatch", "msg", "1"}, want: ""}, - "no tags": {level: logrus.InfoLevel, formatter: &logrus.JSONFormatter{}, logLevel: log.LevelInfo, kvs: []interface{}{"msg", "1"}, want: `{"level":"info","msg":"1"`}, + "json format": { + level: logrus.InfoLevel, + formatter: &logrus.JSONFormatter{}, + logLevel: log.LevelInfo, + kvs: []interface{}{"case", "json format", "msg", "1"}, + want: `{"case":"json format","level":"info","msg":"1"`, + }, + "level unmatch": { + level: logrus.InfoLevel, + formatter: &logrus.JSONFormatter{}, + logLevel: log.LevelDebug, + kvs: []interface{}{"case", "level unmatch", "msg", "1"}, + want: "", + }, + "no tags": { + level: logrus.InfoLevel, + formatter: &logrus.JSONFormatter{}, + logLevel: log.LevelInfo, + kvs: []interface{}{"msg", "1"}, + want: `{"level":"info","msg":"1"`, + }, } for name, test := range tests { diff --git a/examples/log/zap_test.go b/examples/log/zap_test.go index 5cfb444c0..d41de9aba 100644 --- a/examples/log/zap_test.go +++ b/examples/log/zap_test.go @@ -35,5 +35,6 @@ func TestZapLogger(t *testing.T) { zlog.Infow("name", "kratos", "from", "opensource") zlog.Infow("name", "kratos", "from") - defer logger.Sync() + // zap stdout/stderr Sync bugs in OSX, see https://github.com/uber-go/zap/issues/370 + _ = logger.Sync() } diff --git a/examples/metadata/server/main.go b/examples/metadata/server/main.go index e8ef1daa5..3c92f8bad 100644 --- a/examples/metadata/server/main.go +++ b/examples/metadata/server/main.go @@ -18,7 +18,7 @@ var ( // Name is the name of the compiled software. Name = "helloworld" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" ) // server is used to implement helloworld.GreeterServer. diff --git a/examples/metrics/main.go b/examples/metrics/main.go index 1d3824c4f..c7a84d80c 100644 --- a/examples/metrics/main.go +++ b/examples/metrics/main.go @@ -21,7 +21,7 @@ var ( // Name is the name of the compiled software. Name = "metrics" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" _metricSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: "server", diff --git a/examples/registry/consul/client/main.go b/examples/registry/consul/client/main.go index 10ceaee04..772cd03a8 100644 --- a/examples/registry/consul/client/main.go +++ b/examples/registry/consul/client/main.go @@ -65,5 +65,4 @@ func callHTTP(cli *api.Client) { log.Fatal(err) } log.Printf("[http] SayHello %s\n", reply.Message) - } diff --git a/examples/registry/registry_test.go b/examples/registry/registry_test.go index 56c64d856..0094d52ba 100644 --- a/examples/registry/registry_test.go +++ b/examples/registry/registry_test.go @@ -9,6 +9,7 @@ import ( "github.com/go-kratos/kratos/examples/helloworld/helloworld" pb "github.com/go-kratos/kratos/examples/helloworld/helloworld" + "github.com/stretchr/testify/assert" consulregistry "github.com/go-kratos/kratos/contrib/registry/consul/v2" etcdregistry "github.com/go-kratos/kratos/contrib/registry/etcd/v2" @@ -105,7 +106,7 @@ func TestETCD(t *testing.T) { } callHTTP(t, r) callGRPC(t, r) - srv.Stop() + assert.NoError(t, srv.Stop()) } func TestConsul(t *testing.T) { @@ -120,5 +121,5 @@ func TestConsul(t *testing.T) { } callHTTP(t, r) callGRPC(t, r) - srv.Stop() + assert.NoError(t, srv.Stop()) } diff --git a/examples/stream/client/main.go b/examples/stream/client/main.go index 5086ae7e8..20a78c9c3 100644 --- a/examples/stream/client/main.go +++ b/examples/stream/client/main.go @@ -74,7 +74,6 @@ func uploadLog(client hello.HelloClient) { time.Sleep(time.Millisecond * 50) number++ } - return } func chat(client hello.HelloClient) { diff --git a/examples/stream/service/hello.go b/examples/stream/service/hello.go index b0b531aef..cf95a2699 100644 --- a/examples/stream/service/hello.go +++ b/examples/stream/service/hello.go @@ -28,6 +28,7 @@ func (s *HelloService) GetNumber(req *pb.GetNumberRequest, conn pb.Hello_GetNumb time.Sleep(time.Second) } } + func (s *HelloService) UploadLog(conn pb.Hello_UploadLogServer) error { for { req, err := conn.Recv() @@ -40,6 +41,7 @@ func (s *HelloService) UploadLog(conn pb.Hello_UploadLogServer) error { fmt.Println(req.Log) } } + func (s *HelloService) Chat(conn pb.Hello_ChatServer) error { for { req, err := conn.Recv() diff --git a/examples/swagger/server/main.go b/examples/swagger/server/main.go index 9c187b895..fad644452 100644 --- a/examples/swagger/server/main.go +++ b/examples/swagger/server/main.go @@ -19,7 +19,7 @@ var ( // Name is the name of the compiled software. Name = "helloworld" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" ) // server is used to implement helloworld.GreeterServer. diff --git a/examples/tls/server/main.go b/examples/tls/server/main.go index e954938b1..b6e9cba53 100644 --- a/examples/tls/server/main.go +++ b/examples/tls/server/main.go @@ -17,7 +17,7 @@ var ( // Name is the name of the compiled software. Name = "helloworld" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" ) // server is used to implement helloworld.GreeterServer. diff --git a/examples/tls/tls_test.go b/examples/tls/tls_test.go index 166f727ba..27c961bf9 100644 --- a/examples/tls/tls_test.go +++ b/examples/tls/tls_test.go @@ -16,6 +16,7 @@ import ( "github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/transport/grpc" "github.com/go-kratos/kratos/v2/transport/http" + "github.com/stretchr/testify/assert" etcd "go.etcd.io/etcd/client/v3" ) @@ -127,6 +128,6 @@ func TestETCD(t *testing.T) { } callHTTP(t, r, tlsConf) callGRPC(t, r, tlsConf) - srv.Stop() - srvTLS.Stop() + assert.NoError(t, srv.Stop()) + assert.NoError(t, srvTLS.Stop()) } diff --git a/examples/traces/app/message/main.go b/examples/traces/app/message/main.go index 7f59c69b9..7eb888475 100644 --- a/examples/traces/app/message/main.go +++ b/examples/traces/app/message/main.go @@ -25,7 +25,7 @@ var ( // Name is the name of the compiled software. Name = "message" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" ) // server is used to implement helloworld.GreeterServer. diff --git a/examples/traces/app/user/main.go b/examples/traces/app/user/main.go index ad672a1e5..b0ce5d8c0 100644 --- a/examples/traces/app/user/main.go +++ b/examples/traces/app/user/main.go @@ -29,7 +29,7 @@ var ( // Name is the name of the compiled software. Name = "user" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" ) // server is used to implement helloworld.GreeterServer. diff --git a/examples/validate/main.go b/examples/validate/main.go index bd42edfbe..58904e8f1 100644 --- a/examples/validate/main.go +++ b/examples/validate/main.go @@ -4,7 +4,7 @@ import ( "context" "log" - "github.com/go-kratos/kratos/examples/validate/api" + v1 "github.com/go-kratos/kratos/examples/validate/api" "github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2/middleware/validate" "github.com/go-kratos/kratos/v2/transport/grpc" @@ -16,7 +16,7 @@ var ( // Name is the name of the compiled software. Name = "errors" // Version is the version of the compiled software. - Version = "v1.0.0" + // Version = "v1.0.0" ) type server struct { diff --git a/hack/.lintcheck_failures b/hack/.lintcheck_failures index be34df5bc..65c33a88f 100644 --- a/hack/.lintcheck_failures +++ b/hack/.lintcheck_failures @@ -1,4 +1,3 @@ -./examples ./contrib/metrics/datadog ./contrib/config/nacos ./contrib/config/apollo diff --git a/hack/tools.sh b/hack/tools.sh index 12a0e9b87..0fd8ad8ea 100755 --- a/hack/tools.sh +++ b/hack/tools.sh @@ -100,7 +100,7 @@ function tidy() { } function help() { - echo "use: lint, test, fix, tidy" + echo "use: lint, test, test_coverage, fix, tidy" } case $1 in diff --git a/transport/http/server_test.go b/transport/http/server_test.go index 1d84d8eb1..f32a459d4 100644 --- a/transport/http/server_test.go +++ b/transport/http/server_test.go @@ -45,7 +45,7 @@ func TestServer(t *testing.T) { }() time.Sleep(time.Second) testClient(t, srv) - _ = srv.Stop(ctx) + assert.NoError(t, srv.Stop(ctx)) } func testClient(t *testing.T, srv *Server) {