fix: examples lint error (#1454)

* fix example lint error

* use latest version lint

* ignore zap Sync error
pull/1456/head
Kagaya 3 years ago committed by GitHub
parent b38c1145cf
commit 736fb38e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .github/workflows/lint.yml
  2. 5
      Makefile
  3. 20
      examples/benchmark/main.go
  4. 5
      examples/blog/internal/biz/article.go
  5. 4
      examples/blog/internal/data/article.go
  6. 1
      examples/blog/internal/data/data.go
  7. 3
      examples/blog/internal/data/ent/schema/article.go
  8. 3
      examples/blog/internal/data/ent/schema/comment.go
  9. 3
      examples/blog/internal/data/ent/schema/tag.go
  10. 1
      examples/blog/internal/data/redis.go
  11. 2
      examples/blog/internal/server/grpc.go
  12. 2
      examples/blog/internal/server/http.go
  13. 4
      examples/blog/internal/service/article.go
  14. 14
      examples/config/apollo/main.go
  15. 4
      examples/errors/client/main.go
  16. 2
      examples/errors/server/main.go
  17. 11
      examples/event/kafka/kafka.go
  18. 16
      examples/event/memory/memory.go
  19. 4
      examples/event/memory/memory_test.go
  20. 7
      examples/event/receiver/main.go
  21. 1
      examples/event/sender/main.go
  22. 2
      examples/header/server/main.go
  23. 2
      examples/helloworld/client/main.go
  24. 2
      examples/helloworld/server/main.go
  25. 3
      examples/http/echo/main.go
  26. 2
      examples/http/errors/main.go
  27. 2
      examples/http/upload/main.go
  28. 1
      examples/i18n/internal/biz/greeter.go
  29. 5
      examples/i18n/internal/data/data.go
  30. 1
      examples/i18n/internal/data/greeter.go
  31. 1
      examples/i18n/internal/pkg/middleware/localize/localize.go
  32. 2
      examples/i18n/internal/server/grpc.go
  33. 2
      examples/i18n/internal/server/http.go
  34. 3
      examples/i18n/internal/service/greeter.go
  35. 24
      examples/log/logrus_test.go
  36. 3
      examples/log/zap_test.go
  37. 2
      examples/metadata/server/main.go
  38. 2
      examples/metrics/main.go
  39. 1
      examples/registry/consul/client/main.go
  40. 5
      examples/registry/registry_test.go
  41. 1
      examples/stream/client/main.go
  42. 2
      examples/stream/service/hello.go
  43. 2
      examples/swagger/server/main.go
  44. 2
      examples/tls/server/main.go
  45. 5
      examples/tls/tls_test.go
  46. 2
      examples/traces/app/message/main.go
  47. 2
      examples/traces/app/user/main.go
  48. 4
      examples/validate/main.go
  49. 1
      hack/.lintcheck_failures
  50. 2
      hack/tools.sh
  51. 2
      transport/http/server_test.go

@ -31,7 +31,6 @@ jobs:
- name: Lint - name: Lint
uses: golangci/golangci-lint-action@v2 uses: golangci/golangci-lint-action@v2
with: with:
version: v1.42 version: latest
working-directory: ${{ matrix.workdir }} working-directory: ${{ matrix.workdir }}
skip-go-installation: true
skip-pkg-cache: true skip-pkg-cache: true

@ -9,8 +9,6 @@ BIN := ""
TOOLS_SHELL="./hack/tools.sh" TOOLS_SHELL="./hack/tools.sh"
# golangci-lint # golangci-lint
LINTER := bin/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 # check GOBIN
ifneq ($(GOBIN),) ifneq ($(GOBIN),)
@ -22,6 +20,9 @@ else
endif endif
endif endif
$(LINTER):
curl -SL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s latest
all: all:
@cd cmd/kratos && go build && cd - &> /dev/null @cd cmd/kratos && go build && cd - &> /dev/null
@cd cmd/protoc-gen-go-errors && go build && cd - &> /dev/null @cd cmd/protoc-gen-go-errors && go build && cd - &> /dev/null

@ -36,17 +36,17 @@ func main() {
if argsLen > 1 { if argsLen > 1 {
webFramework = args[1] webFramework = args[1]
} }
if argsLen > 2 { if argsLen > 2 { //nolint:gomnd
sleepTime, _ = strconv.Atoi(args[2]) sleepTime, _ = strconv.Atoi(args[2])
if sleepTime == -1 { if sleepTime == -1 {
cpuBound = true cpuBound = true
sleepTime = 0 sleepTime = 0
} }
} }
if argsLen > 3 { if argsLen > 3 { //nolint:gomnd
port, _ = strconv.Atoi(args[3]) port, _ = strconv.Atoi(args[3])
} }
if argsLen > 4 { if argsLen > 4 { //nolint:gomnd
samplingPoint, _ = strconv.Atoi(args[4]) samplingPoint, _ = strconv.Atoi(args[4])
} }
sleepTimeDuration = time.Duration(sleepTime) * time.Millisecond sleepTimeDuration = time.Duration(sleepTime) * time.Millisecond
@ -108,14 +108,14 @@ func kratosHandle(w http2.ResponseWriter, q *http2.Request) {
runtime.Gosched() runtime.Gosched()
} }
} }
w.Write(message) _, _ = w.Write(message)
} }
func GinServer() { func GinServer() {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
mux := gin.New() mux := gin.New()
mux.GET("/", ginHandler) mux.GET("/", ginHandler)
mux.Run(":" + strconv.Itoa(port)) _ = mux.Run(":" + strconv.Itoa(port))
} }
func ginHandler(c *gin.Context) { func ginHandler(c *gin.Context) {
@ -128,13 +128,13 @@ func ginHandler(c *gin.Context) {
runtime.Gosched() runtime.Gosched()
} }
} }
c.Writer.Write(message) _, _ = c.Writer.Write(message)
} }
func EchoServer() { func EchoServer() {
e := echo.New() e := echo.New()
e.GET("/", echoHandler) e.GET("/", echoHandler)
e.Start(":" + strconv.Itoa(port)) _ = e.Start(":" + strconv.Itoa(port))
} }
func echoHandler(c echo.Context) error { func echoHandler(c echo.Context) error {
@ -147,7 +147,7 @@ func echoHandler(c echo.Context) error {
runtime.Gosched() runtime.Gosched()
} }
} }
c.Response().Write(message) _, _ = c.Response().Write(message)
return nil return nil
} }
@ -155,7 +155,7 @@ func MuxServer() {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", muxHandle) r.HandleFunc("/", muxHandle)
http2.Handle("/", r) http2.Handle("/", r)
http2.ListenAndServe(":"+strconv.Itoa(port), r) _ = http2.ListenAndServe(":"+strconv.Itoa(port), r)
} }
func muxHandle(w http2.ResponseWriter, q *http2.Request) { func muxHandle(w http2.ResponseWriter, q *http2.Request) {
@ -168,7 +168,7 @@ func muxHandle(w http2.ResponseWriter, q *http2.Request) {
runtime.Gosched() runtime.Gosched()
} }
} }
w.Write(message) _, _ = w.Write(message)
} }
func pow(targetBits int) { func pow(targetBits int) {

@ -2,12 +2,13 @@ package biz
import ( import (
"context" "context"
"github.com/go-kratos/kratos/v2/log"
"time" "time"
"github.com/go-kratos/kratos/v2/log"
) )
type Article struct { type Article struct {
Id int64 ID int64
Title string Title string
Content string Content string
CreatedAt time.Time CreatedAt time.Time

@ -29,7 +29,7 @@ func (ar *articleRepo) ListArticle(ctx context.Context) ([]*biz.Article, error)
rv := make([]*biz.Article, 0) rv := make([]*biz.Article, 0)
for _, p := range ps { for _, p := range ps {
rv = append(rv, &biz.Article{ rv = append(rv, &biz.Article{
Id: p.ID, ID: p.ID,
Title: p.Title, Title: p.Title,
Content: p.Content, Content: p.Content,
CreatedAt: p.CreatedAt, CreatedAt: p.CreatedAt,
@ -45,7 +45,7 @@ func (ar *articleRepo) GetArticle(ctx context.Context, id int64) (*biz.Article,
return nil, err return nil, err
} }
return &biz.Article{ return &biz.Article{
Id: p.ID, ID: p.ID,
Title: p.Title, Title: p.Title,
Content: p.Content, Content: p.Content,
CreatedAt: p.CreatedAt, CreatedAt: p.CreatedAt,

@ -16,6 +16,7 @@ import (
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
// init mysql driver // init mysql driver
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )

@ -1,11 +1,12 @@
package schema package schema
import ( import (
"time"
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect" "entgo.io/ent/dialect"
"entgo.io/ent/schema/edge" "entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"time"
) )
// Article holds the schema definition for the Article entity. // Article holds the schema definition for the Article entity.

@ -1,11 +1,12 @@
package schema package schema
import ( import (
"time"
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect" "entgo.io/ent/dialect"
"entgo.io/ent/schema/edge" "entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"time"
) )
// Comment holds the schema definition for the Comment entity. // Comment holds the schema definition for the Comment entity.

@ -1,11 +1,12 @@
package schema package schema
import ( import (
"time"
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect" "entgo.io/ent/dialect"
"entgo.io/ent/schema/edge" "entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"time"
) )
// Tag holds the schema definition for the Tag entity. // Tag holds the schema definition for the Tag entity.

@ -3,6 +3,7 @@ package data
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )

@ -14,7 +14,7 @@ import (
// NewGRPCServer new a gRPC server. // NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Server, logger log.Logger, blog *service.BlogService) *grpc.Server { func NewGRPCServer(c *conf.Server, logger log.Logger, blog *service.BlogService) *grpc.Server {
var opts = []grpc.ServerOption{ opts := []grpc.ServerOption{
grpc.Middleware( grpc.Middleware(
recovery.Recovery(), recovery.Recovery(),
tracing.Server(), tracing.Server(),

@ -14,7 +14,7 @@ import (
// NewHTTPServer new a HTTP server. // NewHTTPServer new a HTTP server.
func NewHTTPServer(c *conf.Server, logger log.Logger, blog *service.BlogService) *http.Server { func NewHTTPServer(c *conf.Server, logger log.Logger, blog *service.BlogService) *http.Server {
var opts = []http.ServerOption{ opts := []http.ServerOption{
http.Middleware( http.Middleware(
recovery.Recovery(), recovery.Recovery(),
tracing.Server(), tracing.Server(),

@ -50,7 +50,7 @@ func (s *BlogService) GetArticle(ctx context.Context, req *pb.GetArticleRequest)
if err != nil { if err != nil {
return nil, err 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) { 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{} reply := &pb.ListArticleReply{}
for _, p := range ps { for _, p := range ps {
reply.Results = append(reply.Results, &pb.Article{ reply.Results = append(reply.Results, &pb.Article{
Id: p.Id, Id: p.ID,
Title: p.Title, Title: p.Title,
Content: p.Content, Content: p.Content,
}) })

@ -28,6 +28,8 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
log.Printf("service: %s", name)
// Defines the config JSON Field // Defines the config JSON Field
var v struct { var v struct {
Name string `json:"name"` Name string `json:"name"`
@ -35,7 +37,7 @@ func main() {
} }
// Unmarshal the config to struct // Unmarshal the config to struct
if err := c.Scan(&v); err != nil { if err = c.Scan(&v); err != nil {
panic(err) panic(err)
} }
log.Printf("config: %+v", v) log.Printf("config: %+v", v)
@ -48,12 +50,12 @@ func main() {
log.Printf("service: %s", name) log.Printf("service: %s", name)
// watch key // watch key
if err := c.Watch("name", func(key string, value config.Value) { if err = c.Watch("name", func(key string, value config.Value) {
name, err := value.String() n, e := value.String()
if err != nil { if e != nil {
panic(err) panic(e)
} }
log.Printf("config changed: %s = %s\n", key, name) log.Printf("config changed: %s = %s\n", key, n)
}); err != nil { }); err != nil {
panic(err) panic(err)
} }

@ -4,6 +4,8 @@ import (
"context" "context"
"log" "log"
stdhttp "net/http"
"github.com/go-kratos/kratos/examples/errors/api" "github.com/go-kratos/kratos/examples/errors/api"
pb "github.com/go-kratos/kratos/examples/helloworld/helloworld" pb "github.com/go-kratos/kratos/examples/helloworld/helloworld"
"github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/errors"
@ -27,7 +29,7 @@ func callHTTP() {
client := pb.NewGreeterHTTPClient(conn) client := pb.NewGreeterHTTPClient(conn)
reply, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "empty"}) reply, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "empty"})
if err != nil { if err != nil {
if errors.Code(err) == 500 { if errors.Code(err) == stdhttp.StatusInternalServerError {
log.Println(err) log.Println(err)
} }
if api.IsUserNotFound(err) { if api.IsUserNotFound(err) {

@ -18,7 +18,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "errors" Name = "errors"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
) )
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.

@ -2,14 +2,17 @@ package kafka
import ( import (
"context" "context"
"log"
"github.com/go-kratos/kratos/examples/event/event" "github.com/go-kratos/kratos/examples/event/event"
"github.com/segmentio/kafka-go" "github.com/segmentio/kafka-go"
"log"
) )
var _ event.Sender = (*kafkaSender)(nil) var (
var _ event.Receiver = (*kafkaReceiver)(nil) _ event.Sender = (*kafkaSender)(nil)
var _ event.Event = (*Message)(nil) _ event.Receiver = (*kafkaReceiver)(nil)
_ event.Event = (*Message)(nil)
)
type Message struct { type Message struct {
key string key string

@ -2,14 +2,17 @@ package memory
import ( import (
"context" "context"
"github.com/go-kratos/kratos/examples/event/event"
"log" "log"
"sync" "sync"
"github.com/go-kratos/kratos/examples/event/event"
) )
var _ event.Sender = (*memorySender)(nil) var (
var _ event.Receiver = (*memoryReceiver)(nil) _ event.Sender = (*memorySender)(nil)
var _ event.Event = (*Message)(nil) _ event.Receiver = (*memoryReceiver)(nil)
_ event.Event = (*Message)(nil)
)
var ( var (
chanMap = struct { chanMap = struct {
@ -47,6 +50,7 @@ func (m *memorySender) Send(ctx context.Context, msg event.Event) error {
} }
return nil return nil
} }
func (m *memorySender) Close() error { func (m *memorySender) Close() error {
return nil return nil
} }
@ -74,9 +78,9 @@ func (m *memoryReceiver) Close() error {
func NewMemory(topic string) (event.Sender, event.Receiver) { func NewMemory(topic string) (event.Sender, event.Receiver) {
chanMap.RLock() chanMap.RLock()
if _, ok := chanMap.cm[topic]; !ok { if _, ok := chanMap.cm[topic]; !ok {
//chanMap.Lock() // chanMap.Lock()
chanMap.cm[topic] = make(chan *Message, ChanSize) chanMap.cm[topic] = make(chan *Message, ChanSize)
//chanMap.Unlock() // chanMap.Unlock()
} }
defer chanMap.RUnlock() defer chanMap.RUnlock()

@ -3,9 +3,10 @@ package memory
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/go-kratos/kratos/examples/event/event"
"testing" "testing"
"time" "time"
"github.com/go-kratos/kratos/examples/event/event"
) )
func TestSendAndReceive(t *testing.T) { 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())) t.Log(fmt.Sprintf("key:%s, value:%s\n", event.Key(), event.Value()))
return nil return nil
}) })
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }

@ -19,10 +19,9 @@ func main() {
panic(err) panic(err)
} }
receive(receiver) receive(receiver)
select {
case <-sigs: <-sigs
_ = receiver.Close() _ = receiver.Close()
}
} }
func receive(receiver event.Receiver) { func receive(receiver event.Receiver) {

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/go-kratos/kratos/examples/event/event" "github.com/go-kratos/kratos/examples/event/event"
"github.com/go-kratos/kratos/examples/event/kafka" "github.com/go-kratos/kratos/examples/event/kafka"
) )

@ -17,7 +17,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "helloworld" Name = "helloworld"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
) )
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.

@ -36,7 +36,7 @@ func callHTTP() {
log.Printf("[http] SayHello %s\n", reply.Message) log.Printf("[http] SayHello %s\n", reply.Message)
// returns error // returns error
reply, err = client.SayHello(context.Background(), &pb.HelloRequest{Name: "error"}) _, err = client.SayHello(context.Background(), &pb.HelloRequest{Name: "error"})
if err != nil { if err != nil {
log.Printf("[http] SayHello error: %v\n", err) log.Printf("[http] SayHello error: %v\n", err)
} }

@ -18,7 +18,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "helloworld" Name = "helloworld"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
) )
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.

@ -1,10 +1,11 @@
package main package main
import ( import (
"log"
"github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport/http" "github.com/go-kratos/kratos/v2/transport/http"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"log"
) )
func main() { func main() {

@ -41,7 +41,7 @@ func errorEncoder(w stdhttp.ResponseWriter, r *stdhttp.Request, err error) {
} }
w.Header().Set("Content-Type", "application/"+codec.Name()) w.Header().Set("Content-Type", "application/"+codec.Name())
w.WriteHeader(se.Code) w.WriteHeader(se.Code)
w.Write(body) _, _ = w.Write(body)
} }
func main() { func main() {

@ -19,7 +19,7 @@ func uploadFile(ctx http.Context) error {
} }
defer file.Close() 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 { if err != nil {
return err return err
} }

@ -2,6 +2,7 @@ package biz
import ( import (
"context" "context"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
) )

@ -10,14 +10,13 @@ import (
var ProviderSet = wire.NewSet(NewData, NewGreeterRepo) var ProviderSet = wire.NewSet(NewData, NewGreeterRepo)
// Data . // Data .
type Data struct { type Data struct { // TODO wrapped database client
// TODO wrapped database client
} }
// NewData . // NewData .
func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) { func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) {
cleanup := func() { cleanup := func() {
logger.Log(log.LevelInfo, "closing the data resources") _ = logger.Log(log.LevelInfo, "closing the data resources")
} }
return &Data{}, cleanup, nil return &Data{}, cleanup, nil
} }

@ -2,6 +2,7 @@ package data
import ( import (
"context" "context"
"github.com/go-kratos/kratos/examples/i18n/internal/biz" "github.com/go-kratos/kratos/examples/i18n/internal/biz"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
) )

@ -2,6 +2,7 @@ package localize
import ( import (
"context" "context"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/nicksnyder/go-i18n/v2/i18n" "github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language" "golang.org/x/text/language"

@ -15,7 +15,7 @@ import (
// NewGRPCServer new a gRPC server. // NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *grpc.Server { func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *grpc.Server {
var opts = []grpc.ServerOption{ opts := []grpc.ServerOption{
grpc.Middleware( grpc.Middleware(
recovery.Recovery(), recovery.Recovery(),
tracing.Server(), tracing.Server(),

@ -16,7 +16,7 @@ import (
// NewHTTPServer new a HTTP server. // NewHTTPServer new a HTTP server.
func NewHTTPServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *http.Server { func NewHTTPServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *http.Server {
var opts = []http.ServerOption{ opts := []http.ServerOption{
http.Middleware( http.Middleware(
recovery.Recovery(), recovery.Recovery(),
tracing.Server(), tracing.Server(),

@ -2,10 +2,11 @@ package service
import ( import (
"context" "context"
"github.com/go-kratos/kratos/examples/i18n/internal/pkg/middleware/localize" "github.com/go-kratos/kratos/examples/i18n/internal/pkg/middleware/localize"
"github.com/nicksnyder/go-i18n/v2/i18n" "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/examples/i18n/internal/biz"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
) )

@ -18,9 +18,27 @@ func TestLoggerLog(t *testing.T) {
kvs []interface{} kvs []interface{}
want string 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"`}, "json format": {
"level unmatch": {level: logrus.InfoLevel, formatter: &logrus.JSONFormatter{}, logLevel: log.LevelDebug, kvs: []interface{}{"case", "level unmatch", "msg", "1"}, want: ""}, level: logrus.InfoLevel,
"no tags": {level: logrus.InfoLevel, formatter: &logrus.JSONFormatter{}, logLevel: log.LevelInfo, kvs: []interface{}{"msg", "1"}, want: `{"level":"info","msg":"1"`}, 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 { for name, test := range tests {

@ -35,5 +35,6 @@ func TestZapLogger(t *testing.T) {
zlog.Infow("name", "kratos", "from", "opensource") zlog.Infow("name", "kratos", "from", "opensource")
zlog.Infow("name", "kratos", "from") 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()
} }

@ -18,7 +18,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "helloworld" Name = "helloworld"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
) )
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.

@ -21,7 +21,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "metrics" Name = "metrics"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
_metricSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{ _metricSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "server", Namespace: "server",

@ -65,5 +65,4 @@ func callHTTP(cli *api.Client) {
log.Fatal(err) log.Fatal(err)
} }
log.Printf("[http] SayHello %s\n", reply.Message) log.Printf("[http] SayHello %s\n", reply.Message)
} }

@ -9,6 +9,7 @@ import (
"github.com/go-kratos/kratos/examples/helloworld/helloworld" "github.com/go-kratos/kratos/examples/helloworld/helloworld"
pb "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" consulregistry "github.com/go-kratos/kratos/contrib/registry/consul/v2"
etcdregistry "github.com/go-kratos/kratos/contrib/registry/etcd/v2" etcdregistry "github.com/go-kratos/kratos/contrib/registry/etcd/v2"
@ -105,7 +106,7 @@ func TestETCD(t *testing.T) {
} }
callHTTP(t, r) callHTTP(t, r)
callGRPC(t, r) callGRPC(t, r)
srv.Stop() assert.NoError(t, srv.Stop())
} }
func TestConsul(t *testing.T) { func TestConsul(t *testing.T) {
@ -120,5 +121,5 @@ func TestConsul(t *testing.T) {
} }
callHTTP(t, r) callHTTP(t, r)
callGRPC(t, r) callGRPC(t, r)
srv.Stop() assert.NoError(t, srv.Stop())
} }

@ -74,7 +74,6 @@ func uploadLog(client hello.HelloClient) {
time.Sleep(time.Millisecond * 50) time.Sleep(time.Millisecond * 50)
number++ number++
} }
return
} }
func chat(client hello.HelloClient) { func chat(client hello.HelloClient) {

@ -28,6 +28,7 @@ func (s *HelloService) GetNumber(req *pb.GetNumberRequest, conn pb.Hello_GetNumb
time.Sleep(time.Second) time.Sleep(time.Second)
} }
} }
func (s *HelloService) UploadLog(conn pb.Hello_UploadLogServer) error { func (s *HelloService) UploadLog(conn pb.Hello_UploadLogServer) error {
for { for {
req, err := conn.Recv() req, err := conn.Recv()
@ -40,6 +41,7 @@ func (s *HelloService) UploadLog(conn pb.Hello_UploadLogServer) error {
fmt.Println(req.Log) fmt.Println(req.Log)
} }
} }
func (s *HelloService) Chat(conn pb.Hello_ChatServer) error { func (s *HelloService) Chat(conn pb.Hello_ChatServer) error {
for { for {
req, err := conn.Recv() req, err := conn.Recv()

@ -19,7 +19,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "helloworld" Name = "helloworld"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
) )
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.

@ -17,7 +17,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "helloworld" Name = "helloworld"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
) )
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.

@ -16,6 +16,7 @@ import (
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport/grpc" "github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http" "github.com/go-kratos/kratos/v2/transport/http"
"github.com/stretchr/testify/assert"
etcd "go.etcd.io/etcd/client/v3" etcd "go.etcd.io/etcd/client/v3"
) )
@ -127,6 +128,6 @@ func TestETCD(t *testing.T) {
} }
callHTTP(t, r, tlsConf) callHTTP(t, r, tlsConf)
callGRPC(t, r, tlsConf) callGRPC(t, r, tlsConf)
srv.Stop() assert.NoError(t, srv.Stop())
srvTLS.Stop() assert.NoError(t, srvTLS.Stop())
} }

@ -25,7 +25,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "message" Name = "message"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
) )
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.

@ -29,7 +29,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "user" Name = "user"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
) )
// server is used to implement helloworld.GreeterServer. // server is used to implement helloworld.GreeterServer.

@ -4,7 +4,7 @@ import (
"context" "context"
"log" "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"
"github.com/go-kratos/kratos/v2/middleware/validate" "github.com/go-kratos/kratos/v2/middleware/validate"
"github.com/go-kratos/kratos/v2/transport/grpc" "github.com/go-kratos/kratos/v2/transport/grpc"
@ -16,7 +16,7 @@ var (
// Name is the name of the compiled software. // Name is the name of the compiled software.
Name = "errors" Name = "errors"
// Version is the version of the compiled software. // Version is the version of the compiled software.
Version = "v1.0.0" // Version = "v1.0.0"
) )
type server struct { type server struct {

@ -1,4 +1,3 @@
./examples
./contrib/metrics/datadog ./contrib/metrics/datadog
./contrib/config/nacos ./contrib/config/nacos
./contrib/config/apollo ./contrib/config/apollo

@ -100,7 +100,7 @@ function tidy() {
} }
function help() { function help() {
echo "use: lint, test, fix, tidy" echo "use: lint, test, test_coverage, fix, tidy"
} }
case $1 in case $1 in

@ -45,7 +45,7 @@ func TestServer(t *testing.T) {
}() }()
time.Sleep(time.Second) time.Sleep(time.Second)
testClient(t, srv) testClient(t, srv)
_ = srv.Stop(ctx) assert.NoError(t, srv.Stop(ctx))
} }
func testClient(t *testing.T, srv *Server) { func testClient(t *testing.T, srv *Server) {

Loading…
Cancel
Save