From c014ad173c07563df446838240c3f657725cfbe5 Mon Sep 17 00:00:00 2001 From: AndrewZuo01 Date: Tue, 12 Dec 2023 19:02:41 +0800 Subject: [PATCH] update openim server remove duplicate code --- internal/rpc/user/user.go | 13 ++++++++----- pkg/common/db/mgo/user.go | 8 ++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index 51e277e8c..2ac2790ae 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -378,14 +378,17 @@ func (s *userServer) ProcessUserCommandGet(ctx context.Context, req *pbuser.Proc // Initialize commandInfoSlice as an empty slice commandInfoSlice := make([]*pbuser.CommandInfoResp, 0, len(commands)) - for _, v := range commands { + for i := range commands { + // Access each command using index to avoid copying the struct + command := &commands[i] // Change here: use a pointer to the struct to avoid copying + commandInfoSlice = append(commandInfoSlice, &pbuser.CommandInfoResp{ - Uuid: v.Uuid, - Value: v.Value, - CreateTime: v.CreateTime, + Uuid: command.Uuid, + Value: command.Value, + CreateTime: command.CreateTime, }) } - // Return the response with the slice, which is empty but not nil if there are no commands + // Return the response with the slice return &pbuser.ProcessUserCommandGetResp{KVArray: commandInfoSlice}, nil } diff --git a/pkg/common/db/mgo/user.go b/pkg/common/db/mgo/user.go index 19fd0e115..9756e8e9a 100644 --- a/pkg/common/db/mgo/user.go +++ b/pkg/common/db/mgo/user.go @@ -106,7 +106,7 @@ func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int _, err := collection.UpdateOne(ctx, filter, update) return err } -func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) ([]user.CommandInfoResp, error) { +func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) { collection := u.coll.Database().Collection("userCommands") filter := bson.M{"userID": userID, "type": Type} @@ -116,8 +116,8 @@ func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32 } defer cursor.Close(ctx) - // Initialize commands as an empty slice - commands := []user.CommandInfoResp{} + // Initialize commands as a slice of pointers + commands := []*user.CommandInfoResp{} for cursor.Next(ctx) { var document struct { @@ -130,7 +130,7 @@ func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32 return nil, err } - commandInfo := user.CommandInfoResp{ + commandInfo := &user.CommandInfoResp{ // Change here: use a pointer to the struct Uuid: document.UUID, Value: document.Value, CreateTime: document.CreateTime,