Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
unexport DeepCopy and CopyOverTo methods, introduce procedural option
Browse files Browse the repository at this point in the history
  • Loading branch information
andersfylling committed Oct 28, 2020
1 parent 68f320e commit 37def48
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 302 deletions.
54 changes: 25 additions & 29 deletions auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,14 @@ func (l *AuditLog) Bans() (bans []*PartialBan) {
}

// DeepCopy see interface at struct.go#DeepCopier
func (l *AuditLog) DeepCopy() (copy interface{}) {
copy = &AuditLog{}
l.CopyOverTo(copy)

return
func (l *AuditLog) deepCopy() interface{} {
cp := &AuditLog{}
_ = DeepCopyOver(cp, l)
return cp
}

// CopyOverTo see interface at struct.go#Copier
func (l *AuditLog) CopyOverTo(other interface{}) (err error) {
func (l *AuditLog) copyOverTo(other interface{}) (err error) {
var ok bool
var log *AuditLog
if log, ok = other.(*AuditLog); !ok {
Expand All @@ -136,13 +135,13 @@ func (l *AuditLog) CopyOverTo(other interface{}) (err error) {
}

for _, webhook := range l.Webhooks {
log.Webhooks = append(log.Webhooks, webhook.DeepCopy().(*Webhook))
log.Webhooks = append(log.Webhooks, DeepCopy(webhook).(*Webhook))
}
for _, user := range l.Users {
log.Users = append(log.Users, user.DeepCopy().(*User))
log.Users = append(log.Users, DeepCopy(user).(*User))
}
for _, entry := range l.AuditLogEntries {
log.AuditLogEntries = append(log.AuditLogEntries, entry.DeepCopy().(*AuditLogEntry))
log.AuditLogEntries = append(log.AuditLogEntries, DeepCopy(entry).(*AuditLogEntry))
}
return
}
Expand All @@ -159,15 +158,14 @@ type AuditLogEntry struct {
}

// DeepCopy see interface at struct.go#DeepCopier
func (l *AuditLogEntry) DeepCopy() (copy interface{}) {
copy = &AuditLogEntry{}
l.CopyOverTo(copy)

return
func (l *AuditLogEntry) deepCopy() interface{} {
cp := &AuditLogEntry{}
_ = DeepCopyOver(cp, l)
return cp
}

// CopyOverTo see interface at struct.go#Copier
func (l *AuditLogEntry) CopyOverTo(other interface{}) (err error) {
func (l *AuditLogEntry) copyOverTo(other interface{}) (err error) {
var ok bool
var log *AuditLogEntry
if log, ok = other.(*AuditLogEntry); !ok {
Expand All @@ -182,11 +180,11 @@ func (l *AuditLogEntry) CopyOverTo(other interface{}) (err error) {
log.Reason = l.Reason

for _, change := range l.Changes {
log.Changes = append(log.Changes, change.DeepCopy().(*AuditLogChanges))
log.Changes = append(log.Changes, DeepCopy(change).(*AuditLogChanges))
}

if l.Options != nil {
log.Options = l.Options.DeepCopy().(*AuditLogOption)
log.Options = DeepCopy(l.Options).(*AuditLogOption)
}
return
}
Expand All @@ -203,15 +201,14 @@ type AuditLogOption struct {
}

// DeepCopy see interface at struct.go#DeepCopier
func (l *AuditLogOption) DeepCopy() (copy interface{}) {
copy = &AuditLogOption{}
l.CopyOverTo(copy)

return
func (l *AuditLogOption) deepCopy() interface{} {
cp := &AuditLogOption{}
_ = DeepCopyOver(cp, l)
return cp
}

// CopyOverTo see interface at struct.go#Copier
func (l *AuditLogOption) CopyOverTo(other interface{}) (err error) {
func (l *AuditLogOption) copyOverTo(other interface{}) (err error) {
var ok bool
var log *AuditLogOption
if log, ok = other.(*AuditLogOption); !ok {
Expand All @@ -237,15 +234,14 @@ type AuditLogChanges struct {
}

// DeepCopy see interface at struct.go#DeepCopier
func (l *AuditLogChanges) DeepCopy() (copy interface{}) {
copy = &AuditLogChanges{}
l.CopyOverTo(copy)

return
func (l *AuditLogChanges) deepCopy() interface{} {
cp := &AuditLogChanges{}
_ = DeepCopyOver(cp, l)
return cp
}

// CopyOverTo see interface at struct.go#Copier
func (l *AuditLogChanges) CopyOverTo(other interface{}) (err error) {
func (l *AuditLogChanges) copyOverTo(other interface{}) (err error) {
var ok bool
var log *AuditLogChanges
if log, ok = other.(*AuditLogChanges); !ok {
Expand Down
52 changes: 26 additions & 26 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (c *CacheLFUImmutable) createDMChannel(msg *Message) {
channel := &Channel{
ID: channelID,
Recipients: []*User{
c.CurrentUser.DeepCopy().(*User),
msg.Author.DeepCopy().(*User),
DeepCopy(c.CurrentUser).(*User),
DeepCopy(msg.Author).(*User),
},
LastMessageID: msg.ID,
Type: ChannelTypeDM,
Expand Down Expand Up @@ -104,7 +104,7 @@ func (c *CacheLFUImmutable) Ready(data []byte) (*Ready, error) {
}

err := json.Unmarshal(data, rdy)
rdy.User = c.CurrentUser.DeepCopy().(*User)
rdy.User = DeepCopy(c.CurrentUser).(*User)
c.Patch(rdy)
return rdy, err
}
Expand All @@ -129,7 +129,7 @@ func (c *CacheLFUImmutable) ChannelCreate(data []byte) (*ChannelCreate, error) {
// assumption#3: a channel can not change from one type to another (text => news, text => voice)

wrap := func(c *Channel) *ChannelCreate {
return &ChannelCreate{Channel: c.DeepCopy().(*Channel)}
return &ChannelCreate{Channel: DeepCopy(c).(*Channel)}
}

channel := &Channel{}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (c *CacheLFUImmutable) ChannelUpdate(data []byte) (*ChannelUpdate, error) {
}
c.Patch(channel)

channel = channel.DeepCopy().(*Channel)
channel = DeepCopy(channel).(*Channel)
return channel, nil
}

Expand All @@ -197,7 +197,7 @@ func (c *CacheLFUImmutable) ChannelUpdate(data []byte) (*ChannelUpdate, error) {
return nil, err
}
c.Patch(tmp)
channel = tmp.DeepCopy().(*Channel)
channel = DeepCopy(tmp).(*Channel)
freshItem := c.Channels.CreateCacheableItem(tmp)

c.Channels.Lock()
Expand Down Expand Up @@ -280,7 +280,7 @@ func (c *CacheLFUImmutable) UserUpdate(data []byte) (*UserUpdate, error) {
return nil, err
}

update.User = c.CurrentUser.DeepCopy().(*User)
update.User = DeepCopy(c.CurrentUser).(*User)
c.Patch(update)

return update, nil
Expand Down Expand Up @@ -340,15 +340,15 @@ func (c *CacheLFUImmutable) GuildMemberAdd(data []byte) (*GuildMemberAdd, error)
// TODO: i assume the user is partial and doesn't hold any real updates
usr := cachedUser.Val.(*User)
// if err := json.Unmarshal(data, &Member{User:usr}); err == nil {
// gmr.Member.User = usr.DeepCopy().(*User)
// gmr.Member.User = DeepCopy(usr).(*User)
// }
gmr.Member.User = usr.DeepCopy().(*User)
gmr.Member.User = DeepCopy(usr).(*User)
} else {
c.Users.Lock()
defer c.Users.Unlock()

if _, exists := c.Users.Get(userID); !exists {
usr := c.Users.CreateCacheableItem(gmr.Member.User.DeepCopy().(*User))
usr := c.Users.CreateCacheableItem(DeepCopy(gmr.Member.User).(*User))
c.Users.Set(userID, usr)
}
}
Expand Down Expand Up @@ -376,7 +376,7 @@ func (c *CacheLFUImmutable) GuildMemberAdd(data []byte) (*GuildMemberAdd, error)
}
}
if member == nil {
member = gmr.Member.DeepCopy().(*Member)
member = DeepCopy(gmr.Member).(*Member)

guild.Members = append(guild.Members, member)
guild.MemberCount++
Expand Down Expand Up @@ -412,7 +412,7 @@ func (c *CacheLFUImmutable) GuildCreate(data []byte) (*GuildCreate, error) {
guild.Unavailable = false
c.Patch(guild)

guild = guild.DeepCopy().(*Guild)
guild = DeepCopy(guild).(*Guild)
} else if exists {
// not pre-loaded from ready event
if err := json.Unmarshal(data, &guild); err != nil {
Expand All @@ -427,7 +427,7 @@ func (c *CacheLFUImmutable) GuildCreate(data []byte) (*GuildCreate, error) {
c.Patch(guild)

e := c.Guilds.CreateCacheableItem(guild)
guild = guild.DeepCopy().(*Guild)
guild = DeepCopy(guild).(*Guild)

c.Guilds.Lock()
if _, exists := c.Guilds.Get(guildID); !exists {
Expand All @@ -454,7 +454,7 @@ func (c *CacheLFUImmutable) GuildUpdate(data []byte) (*GuildUpdate, error) {
}
c.Patch(guild)

return guild.DeepCopy().(*Guild), nil
return DeepCopy(guild).(*Guild), nil
}

var metadata *idHolder
Expand Down Expand Up @@ -484,7 +484,7 @@ func (c *CacheLFUImmutable) GuildUpdate(data []byte) (*GuildUpdate, error) {
guild, err = updateGuild(guildID, oldItem) // fallback
} else {
c.Guilds.Set(guildID, e)
guild = guild.DeepCopy().(*Guild)
guild = DeepCopy(guild).(*Guild)
}
}

Expand Down Expand Up @@ -529,7 +529,7 @@ func (c *CacheLFUImmutable) GetChannel(id Snowflake) (*Channel, error) {
defer mutex.Unlock()

channel := cachedItem.Val.(*Channel)
return channel.DeepCopy().(*Channel), nil
return DeepCopy(channel).(*Channel), nil
}
return nil, nil
}
Expand All @@ -545,7 +545,7 @@ func (c *CacheLFUImmutable) GetGuildEmoji(guildID, emojiID Snowflake) (*Emoji, e

guild := cachedItem.Val.(*Guild)
emoji, _ := guild.Emoji(emojiID)
return emoji.DeepCopy().(*Emoji), nil
return DeepCopy(emoji).(*Emoji), nil
}
return nil, errors.New("guild does not exist")
}
Expand All @@ -562,7 +562,7 @@ func (c *CacheLFUImmutable) GetGuildEmojis(id Snowflake) ([]*Emoji, error) {
guild := cachedItem.Val.(*Guild)
emojis := make([]*Emoji, len(guild.Emojis))
for i, emoji := range emojis {
emojis[i] = emoji.DeepCopy().(*Emoji)
emojis[i] = DeepCopy(emoji).(*Emoji)
}

return emojis, nil
Expand All @@ -580,7 +580,7 @@ func (c *CacheLFUImmutable) GetGuild(id Snowflake) (*Guild, error) {
mutex.Lock()
defer mutex.Unlock()

guild = cachedItem.Val.(*Guild).DeepCopy().(*Guild)
guild = DeepCopy(cachedItem.Val.(*Guild)).(*Guild)
}

return guild, nil
Expand All @@ -599,7 +599,7 @@ func (c *CacheLFUImmutable) GetGuildChannels(id Snowflake) ([]*Channel, error) {

channels := make([]*Channel, len(guild.Channels))
for i, channel := range guild.Channels {
channels[i] = channel.DeepCopy().(*Channel)
channels[i] = DeepCopy(channel).(*Channel)
}

return channels, nil
Expand All @@ -620,7 +620,7 @@ func (c *CacheLFUImmutable) GetMember(guildID, userID Snowflake) (*Member, error
if user != nil {
mutex := c.Mutex(&c.Users, userID)
mutex.Lock()
user = user.DeepCopy().(*User)
user = DeepCopy(user).(*User)
mutex.Unlock()
}
wg.Done()
Expand All @@ -637,7 +637,7 @@ func (c *CacheLFUImmutable) GetMember(guildID, userID Snowflake) (*Member, error
guild := cachedItem.Val.(*Guild)
member, _ = guild.Member(userID)
if member != nil {
member = member.DeepCopy().(*Member)
member = DeepCopy(member).(*Member)
}

mutex.Unlock()
Expand Down Expand Up @@ -665,7 +665,7 @@ func (c *CacheLFUImmutable) GetGuildRoles(guildID Snowflake) ([]*Role, error) {
guild := cachedItem.Val.(*Guild)
roles := make([]*Role, len(guild.Roles))
for i, role := range guild.Roles {
roles[i] = role.DeepCopy().(*Role)
roles[i] = DeepCopy(role).(*Role)
}

return roles, nil
Expand All @@ -676,7 +676,7 @@ func (c *CacheLFUImmutable) GetCurrentUser() (*User, error) {
c.CurrentUserMu.Lock()
defer c.CurrentUserMu.Unlock()

return c.CurrentUser.DeepCopy().(*User), nil
return DeepCopy(c.CurrentUser).(*User), nil
}
func (c *CacheLFUImmutable) GetUser(id Snowflake) (*User, error) {
currentUser := func() *User {
Expand All @@ -690,7 +690,7 @@ func (c *CacheLFUImmutable) GetUser(id Snowflake) (*User, error) {
}
// hmmm.. ugly
if match := currentUser(); match != nil {
return match.DeepCopy().(*User), nil
return DeepCopy(match).(*User), nil
}

c.Users.RLock()
Expand All @@ -703,7 +703,7 @@ func (c *CacheLFUImmutable) GetUser(id Snowflake) (*User, error) {
mutex.Lock()
defer mutex.Unlock()

user = item.Val.(*User).DeepCopy().(*User)
user = DeepCopy(item.Val.(*User)).(*User)
}

return user, nil
Expand Down
15 changes: 7 additions & 8 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (a *Attachment) updateInternals() {
}

// DeepCopy see interface at struct.go#DeepCopier
func (a *Attachment) DeepCopy() (copy interface{}) {
func (a *Attachment) deepCopy() (copy interface{}) {
copy = &Attachment{
ID: a.ID,
Filename: a.Filename,
Expand Down Expand Up @@ -201,15 +201,14 @@ func (c *Channel) Compare(other *Channel) bool {
}

// DeepCopy see interface at struct.go#DeepCopier
func (c *Channel) DeepCopy() (copy interface{}) {
copy = &Channel{}
_ = c.CopyOverTo(copy)

return
func (c *Channel) deepCopy() interface{} {
cp := &Channel{}
_ = DeepCopyOver(cp, c)
return cp
}

// CopyOverTo see interface at struct.go#Copier
func (c *Channel) CopyOverTo(other interface{}) (err error) {
func (c *Channel) copyOverTo(other interface{}) (err error) {
var channel *Channel
var valid bool
if channel, valid = other.(*Channel); !valid {
Expand Down Expand Up @@ -239,7 +238,7 @@ func (c *Channel) CopyOverTo(other interface{}) (err error) {
// add recipients if it's a DM
channel.Recipients = make([]*User, 0, len(c.Recipients))
for _, recipient := range c.Recipients {
channel.Recipients = append(channel.Recipients, recipient.DeepCopy().(*User))
channel.Recipients = append(channel.Recipients, DeepCopy(recipient).(*User))
}

return
Expand Down
2 changes: 1 addition & 1 deletion channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestChannel_DeepCopy(t *testing.T) {
Type: 1,
})

cp := test.DeepCopy().(*Channel)
cp := DeepCopy(test).(*Channel)
icon2 := "sfkjdsf"
test.Icon = icon2
if cp.Icon != icon1 {
Expand Down
Loading

0 comments on commit 37def48

Please sign in to comment.