Properly implement session deep copy (#8602)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
283f34b9c6
Коммит
853445dc2e
@@ -38,8 +38,21 @@ type Session struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (me *Session) DeepCopy() *Session {
|
func (me *Session) DeepCopy() *Session {
|
||||||
copy := *me
|
copySession := *me
|
||||||
return ©
|
|
||||||
|
if me.Props != nil {
|
||||||
|
copySession.Props = CopyStringMap(me.Props)
|
||||||
|
}
|
||||||
|
|
||||||
|
if me.TeamMembers != nil {
|
||||||
|
copySession.TeamMembers = make([]*TeamMember, len(me.TeamMembers))
|
||||||
|
for index, tm := range me.TeamMembers {
|
||||||
|
copySession.TeamMembers[index] = new(TeamMember)
|
||||||
|
*copySession.TeamMembers[index] = *tm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ©Session
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *Session) ToJson() string {
|
func (me *Session) ToJson() string {
|
||||||
|
|||||||
@@ -7,8 +7,38 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestSessionDeepCopy(t *testing.T) {
|
||||||
|
sessionId := NewId()
|
||||||
|
userId := NewId()
|
||||||
|
mapKey := "key"
|
||||||
|
mapValue := "val"
|
||||||
|
|
||||||
|
session := &Session{Id: sessionId, Props: map[string]string{mapKey: mapValue}, TeamMembers: []*TeamMember{&TeamMember{UserId: userId, TeamId: "someteamId"}}}
|
||||||
|
|
||||||
|
copySession := session.DeepCopy()
|
||||||
|
copySession.Id = "changed"
|
||||||
|
copySession.Props[mapKey] = "changed"
|
||||||
|
copySession.TeamMembers[0].UserId = "changed"
|
||||||
|
|
||||||
|
assert.Equal(t, sessionId, session.Id)
|
||||||
|
assert.Equal(t, mapValue, session.Props[mapKey])
|
||||||
|
assert.Equal(t, userId, session.TeamMembers[0].UserId)
|
||||||
|
|
||||||
|
session = &Session{Id: sessionId}
|
||||||
|
copySession = session.DeepCopy()
|
||||||
|
|
||||||
|
assert.Equal(t, sessionId, session.Id)
|
||||||
|
|
||||||
|
session = &Session{TeamMembers: []*TeamMember{}}
|
||||||
|
copySession = session.DeepCopy()
|
||||||
|
|
||||||
|
assert.Equal(t, 0, len(copySession.TeamMembers))
|
||||||
|
}
|
||||||
|
|
||||||
func TestSessionJson(t *testing.T) {
|
func TestSessionJson(t *testing.T) {
|
||||||
session := Session{}
|
session := Session{}
|
||||||
session.PreSave()
|
session.PreSave()
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user