From 070f51052afa76294d18982ba1f37599ff4a9c9b Mon Sep 17 00:00:00 2001 From: yusank Date: Fri, 2 Sep 2022 15:53:35 +0800 Subject: [PATCH 1/7] fix: update set --- pkg/types/id.go | 5 ++--- pkg/util/session.go | 6 +++--- pkg/util/session_test.go | 2 +- pkg/util/sets.go | 8 ++------ 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/pkg/types/id.go b/pkg/types/id.go index 70f409c..0509199 100644 --- a/pkg/types/id.go +++ b/pkg/types/id.go @@ -19,7 +19,6 @@ var decodeBase58Map [256]byte // Create maps for decoding Base58/Base32. // This speeds up the process tremendously. func init() { - for i := 0; i < len(decodeBase58Map); i++ { decodeBase58Map[i] = 0xFF } @@ -119,7 +118,7 @@ func ParseBase16(id string) (ID, error) { // Base32 uses the z-base-32 character set but encodes and decodes similar // to base58, allowing it to create an even smaller result string. -// NOTE: There are many different base32 implementations so becareful when +// NOTE: There are many base32 implementations so be careful when // doing any interoperation. func (f ID) Base32() string { if f < 32 { @@ -141,7 +140,7 @@ func (f ID) Base32() string { } // ParseBase32 parses a base32 []byte into a snowflake ID -// NOTE: There are many different base32 implementations so becareful when +// NOTE: There are many base32 implementations so be careful when // doing any interoperation. func ParseBase32(b []byte) (ID, error) { var id int64 diff --git a/pkg/util/session.go b/pkg/util/session.go index 14c990c..9a5b00a 100644 --- a/pkg/util/session.go +++ b/pkg/util/session.go @@ -61,7 +61,7 @@ var ( ErrInvalidSessionIDLength = fmt.Errorf("invalid session id length") ) -func ParseSession(s string) (tp int32, from, to types.ID, err error) { +func ParseSession(s string) (tp messagev1.SessionType, from, to types.ID, err error) { // check if s is valid if len(s) < 2+2*11 { return 0, 0, 0, ErrInvalidSessionIDLength @@ -69,11 +69,11 @@ func ParseSession(s string) (tp int32, from, to types.ID, err error) { // first 2 bytes is session type tpStr := s[:2] - i64, err := strconv.ParseInt(tpStr, 16, 32) + i32, err := strconv.ParseInt(tpStr, 16, 32) if err != nil { return 0, 0, 0, err } - tp = int32(i64) + tp = messagev1.SessionType(i32) // check if tp is valid if tp > 0xFF || tp < 0 { diff --git a/pkg/util/session_test.go b/pkg/util/session_test.go index e028c9c..7150cfa 100644 --- a/pkg/util/session_test.go +++ b/pkg/util/session_test.go @@ -103,7 +103,7 @@ func TestParseSession(t *testing.T) { tests := []struct { name string args args - wantTp int32 + wantTp messagev1.SessionType wantFrom types.ID wantTo types.ID wantErr assert.ErrorAssertionFunc diff --git a/pkg/util/sets.go b/pkg/util/sets.go index bca286b..dd2082d 100644 --- a/pkg/util/sets.go +++ b/pkg/util/sets.go @@ -10,20 +10,16 @@ func NewSet[T comparable]() *Set[T] { } } -func (s *Set[T]) Add(elem ...T) *Set[T] { +func (s *Set[T]) Add(elem ...T) { for _, e := range elem { s.m[e] = true } - - return s } -func (s *Set[T]) Remove(elem ...T) *Set[T] { +func (s *Set[T]) Remove(elem ...T) { for _, e := range elem { delete(s.m, e) } - - return s } func (s *Set[T]) Contains(elem T) bool { From b1d1ad5366a286bc823834657ee2251cf5ed40a1 Mon Sep 17 00:00:00 2001 From: yusank Date: Fri, 2 Sep 2022 16:03:55 +0800 Subject: [PATCH 2/7] fix: update go mod --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 5ede0ec..ccca82a 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( ) require ( - github.com/go-goim/api v0.0.7 + github.com/go-goim/api v0.0.8 github.com/go-kratos/kratos/contrib/config/etcd/v2 v2.0.0-20220528114537-97c103a39562 github.com/panjf2000/ants/v2 v2.5.0 github.com/tsuna/gohbase v0.0.0-20220517082425-cb1f77f08e4f diff --git a/go.sum b/go.sum index 3283850..8ea0003 100644 --- a/go.sum +++ b/go.sum @@ -97,8 +97,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8= github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= -github.com/go-goim/api v0.0.7 h1:JnECTk4ait2Nkzv2YJx16+H8vmQMX4qFeo/sncuTOtE= -github.com/go-goim/api v0.0.7/go.mod h1:93A7t8glrNTyYuWA2n4IhalnCtgw4uQ9lGV3E2t17gk= +github.com/go-goim/api v0.0.8 h1:K2FTSMXFwGyjeCWHoQMJ+oNRsT6yEgwPef3YzoE/cs4= +github.com/go-goim/api v0.0.8/go.mod h1:93A7t8glrNTyYuWA2n4IhalnCtgw4uQ9lGV3E2t17gk= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= From 48f8b1ff4a8cb66255e35f665a08934a9b1d7347 Mon Sep 17 00:00:00 2001 From: yusank Date: Fri, 2 Sep 2022 16:10:14 +0800 Subject: [PATCH 3/7] fix: lint & test --- pkg/mid/auth_test.go | 4 ++-- pkg/util/session.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/mid/auth_test.go b/pkg/mid/auth_test.go index 4a3c821..e22b9f6 100644 --- a/pkg/mid/auth_test.go +++ b/pkg/mid/auth_test.go @@ -9,8 +9,8 @@ import ( ) func TestNewJwtToken(t *testing.T) { - var uid int64 = 1 - jwt, err := NewJwtToken(types.ID(uid)) + var uid = types.ID(1) + jwt, err := NewJwtToken(uid) assert.Nil(t, err) assert.NotEmpty(t, jwt) diff --git a/pkg/util/session.go b/pkg/util/session.go index 9a5b00a..899feba 100644 --- a/pkg/util/session.go +++ b/pkg/util/session.go @@ -95,7 +95,7 @@ func ParseSession(s string) (tp messagev1.SessionType, from, to types.ID, err er } // if tp is group chat, to is group id - if messagev1.SessionType(tp) == messagev1.SessionType_GroupChat { + if tp == messagev1.SessionType_GroupChat { to = from } From 6e5bdcc7f4a6ce5cc528cff44c09b37e25843b88 Mon Sep 17 00:00:00 2001 From: yusank Date: Fri, 2 Sep 2022 16:16:25 +0800 Subject: [PATCH 4/7] fix: update lint --- pkg/web/request/jsonpb.go | 4 ++-- tests/batch-user/user.go | 14 +++++++------- tests/client-gui/client.go | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/web/request/jsonpb.go b/pkg/web/request/jsonpb.go index 8063060..9af60ef 100644 --- a/pkg/web/request/jsonpb.go +++ b/pkg/web/request/jsonpb.go @@ -2,7 +2,7 @@ package request import ( "fmt" - "io/ioutil" + "io" "net/http" "github.com/gin-gonic/gin/binding" @@ -38,7 +38,7 @@ func (b PbJSONBinding) Name() string { } func (b PbJSONBinding) Bind(req *http.Request, obj interface{}) error { - buf, err := ioutil.ReadAll(req.Body) + buf, err := io.ReadAll(req.Body) if err != nil { return err } diff --git a/tests/batch-user/user.go b/tests/batch-user/user.go index 10b3f51..c18a89a 100644 --- a/tests/batch-user/user.go +++ b/tests/batch-user/user.go @@ -4,7 +4,7 @@ import ( "encoding/json" "flag" "fmt" - "io/ioutil" + "io" "log" "net/http" "net/url" @@ -43,7 +43,7 @@ func (u *user) register() error { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return err } @@ -83,7 +83,7 @@ func (u *user) login() error { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return err } @@ -130,7 +130,7 @@ func (u *user) addFriend(fuid string) error { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return err } @@ -164,7 +164,7 @@ func (u *user) queryFriend(email string) (uid string, err error) { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", err } @@ -208,7 +208,7 @@ func (u *user) queryFriendRequests() (ids []uint64, err error) { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -264,7 +264,7 @@ func (u *user) acceptFriend(requestID uint64) error { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return err } diff --git a/tests/client-gui/client.go b/tests/client-gui/client.go index 3535e23..61a5051 100644 --- a/tests/client-gui/client.go +++ b/tests/client-gui/client.go @@ -4,7 +4,7 @@ import ( "encoding/json" "flag" "fmt" - "io/ioutil" + "io" "log" "net" "net/http" @@ -205,7 +205,7 @@ func login() (serverIP string, err error) { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", err } @@ -245,7 +245,7 @@ func loadFriends() error { logger.Println(rsp.StatusCode) defer rsp.Body.Close() - b, err := ioutil.ReadAll(rsp.Body) + b, err := io.ReadAll(rsp.Body) if err != nil { logger.Println(err) return err From 8c8ba721ddf1a175fa770d75e976d5525d0da820 Mon Sep 17 00:00:00 2001 From: yusank Date: Fri, 2 Sep 2022 16:24:31 +0800 Subject: [PATCH 5/7] fix: gofmt --- tests/ws.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ws.go b/tests/ws.go index ca1e9c8..ca8701d 100644 --- a/tests/ws.go +++ b/tests/ws.go @@ -1,2 +1 @@ package tests - From 44fbf64f6dfa49c5a0856f2148a0adbe8423ecdf Mon Sep 17 00:00:00 2001 From: yusank Date: Fri, 2 Sep 2022 16:41:22 +0800 Subject: [PATCH 6/7] fix: update lint --- pkg/app/application.go | 2 +- pkg/db/mysql.go | 42 ++++++++++++++++++++------------------ pkg/db/redis.go | 35 ++++++++++++++++--------------- pkg/goroutine/goroutine.go | 2 +- 4 files changed, 42 insertions(+), 39 deletions(-) diff --git a/pkg/app/application.go b/pkg/app/application.go index f3ada2b..8e129c8 100644 --- a/pkg/app/application.go +++ b/pkg/app/application.go @@ -31,7 +31,7 @@ import ( // Application is a common app entry. // All apps can use this Application as a base entry and add own fields and methods -// in their own app packages. +// in their own app packages. type Application struct { Core *kratos.App Register registry.RegisterDiscover diff --git a/pkg/db/mysql.go b/pkg/db/mysql.go index 22ca3f9..5e2ac2a 100644 --- a/pkg/db/mysql.go +++ b/pkg/db/mysql.go @@ -41,26 +41,28 @@ func ctxWithGormDB(ctx context.Context, tx *gorm.DB) context.Context { } // Transaction get gorm.DB from ctx and run Transaction Operation with ctx -// How to use: -// // 所有 db 操作的第一参数为 context.Context, 然后通过 ctx 读取 DB 对象 -// if err := db.Transaction(context.Background(), func(ctx context.Context) error { -// if err := d.Create(ctx); err != nil { -// return err -// } -// -// d.Name = "123" -// return d.Update(ctx) -// }); err != nil { -// return -// } -// -// func (d *Domain) Create(ctx context.Context) error { -// return GetDBFromCtx(ctx).Create(d).Error -// } -// -// func (d *Domain) Update(ctx context.Context) error { -// return GetDBFromCtx(ctx).Updates(d).Error -// } +/* + How to use: + // 所有 db 操作的第一参数为 context.Context, 然后通过 ctx 读取 DB 对象 + if err := db.Transaction(context.Background(), func(ctx context.Context) error { + if err := d.Create(ctx); err != nil { + return err + } + + d.Name = "123" + return d.Update(ctx) + }); err != nil { + return + } + + func (d *Domain) Create(ctx context.Context) error { + return GetDBFromCtx(ctx).Create(d).Error + } + + func (d *Domain) Update(ctx context.Context) error { + return GetDBFromCtx(ctx).Updates(d).Error + } +*/ func Transaction(ctx context.Context, f func(context.Context) error) error { if ctx == nil { ctx = context.Background() diff --git a/pkg/db/redis.go b/pkg/db/redis.go index 5bf8929..45d3469 100644 --- a/pkg/db/redis.go +++ b/pkg/db/redis.go @@ -51,23 +51,24 @@ func Pipeline(ctx context.Context, fn func(pipeline redisv8.Pipeliner) error) er return err } -// TxPipeline get redisv8.Client from ctx and run Transaction Pipeline Operation with ctx -// It's same as Pipeline, but wraps queued commands with MULTI/EXEC. -// How to use: -// var ( -// get *redisv8.StringCmd -// ) -// err := TxPipeline(ctx, func(pipeline redisv8.Pipeliner) error { -// pipeline.Set(ctx, "key", "value", 0) -// get = pipeline.Get(ctx, "key") -// return nil -// }) -// if err != nil { -// return err -// } -// -// fmt.Println(get.Val()) -// // Output: value +// TxPipeline get redisv8.Client from ctx and run Transaction Pipeline Operation with ctx. +// It's same as Pipeline, but wraps queued commands with MULTI/EXEC. +/* + How to use: + var ( + get *redisv8.StringCmd + ) + err := TxPipeline(ctx, func(pipeline redisv8.Pipeliner) error { + pipeline.Set(ctx, "key", "value", 0) + get = pipeline.Get(ctx, "key") + return nil + }) + if err != nil { + return err + } + + fmt.Println(get.Val()) +*/ func TxPipeline(ctx context.Context, fn func(pipeline redisv8.Pipeliner) error) error { if ctx == nil { ctx = context.Background() diff --git a/pkg/goroutine/goroutine.go b/pkg/goroutine/goroutine.go index efef6f3..21e1c95 100644 --- a/pkg/goroutine/goroutine.go +++ b/pkg/goroutine/goroutine.go @@ -31,7 +31,7 @@ func init() { } // Submit new task to pool -// It will block if goroutine is up to max +// It will block if goroutine is up to max func Submit(f func()) error { return _defaultPool.Submit(f) } From b599555704aff4e47a1415e7ad8d572e3ff3d070 Mon Sep 17 00:00:00 2001 From: yusank Date: Fri, 2 Sep 2022 16:44:19 +0800 Subject: [PATCH 7/7] fix: lint fmt --- pkg/types/id.go | 4 ++-- pkg/util/session.go | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pkg/types/id.go b/pkg/types/id.go index 0509199..7b8d659 100644 --- a/pkg/types/id.go +++ b/pkg/types/id.go @@ -245,7 +245,7 @@ func ParseIntBytes(id [8]byte) ID { // MarshalJSON returns a json byte array string of the snowflake ID. // Note: this is not regular MarshalJSON, it converts the ID to a base58 string, -// instead of a regular strconv.FormatInt(id,10). +// instead of a regular strconv.FormatInt(id,10). func (f ID) MarshalJSON() ([]byte, error) { b58 := f.Base58() buff := make([]byte, 0, len(b58)+2) @@ -257,7 +257,7 @@ func (f ID) MarshalJSON() ([]byte, error) { // UnmarshalJSON converts a json byte array of a snowflake ID into an ID type. // Note: this is not regular UnmarshalJSON, it converts the ID from a base58, -// instead of a regular strconv.ParseInt(id,10). +// instead of a regular strconv.ParseInt(id,10). func (f *ID) UnmarshalJSON(b []byte) error { if len(b) < 3 || b[0] != '"' || b[len(b)-1] != '"' { return JSONSyntaxError{b} diff --git a/pkg/util/session.go b/pkg/util/session.go index 899feba..a331a56 100644 --- a/pkg/util/session.go +++ b/pkg/util/session.go @@ -10,13 +10,14 @@ import ( ) // Session generate session id -// switch tpye { -// case messagev1.SessionType_SingleChat: -// 001fromUIDtoUID -// case messagev1.SessionType_GroupChat: -// 010groupID00000000 -// } -// to is groupID when type is groupChat +/* + switch tpye { + case messagev1.SessionType_SingleChat: + 001fromUIDtoUID + case messagev1.SessionType_GroupChat: + 010groupID00000000 + } +*/ func Session(tp messagev1.SessionType, from, to types.ID) string { // check if tp is valid if tp > 0xFF || tp < 0 {