update openim server remove duplicate code

pull/1532/head
AndrewZuo01 2 years ago
parent 31e8054cd8
commit c014ad173c

@ -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
}

@ -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,

Loading…
Cancel
Save