Don't use pointers to interfaces

There are few situations where a pointer to an interface is useful in
Go, and this isn't one. Interfaces can hold types by value or pointer,
so long as that type fulfills the interface.
This commit is contained in:
John Brooks 2017-09-23 16:29:46 -06:00
parent d2dceef028
commit ea788d58ef
5 changed files with 11 additions and 11 deletions

View File

@ -51,7 +51,7 @@ func (rai *RicochetApplicationInstance) SendChatMessage(message string) {
rai.connection.Do(func() error { rai.connection.Do(func() error {
channel := rai.connection.Channel("im.ricochet.chat", channels.Outbound) channel := rai.connection.Channel("im.ricochet.chat", channels.Outbound)
if channel != nil { if channel != nil {
chatchannel, ok := (*channel.Handler).(*channels.ChatChannel) chatchannel, ok := channel.Handler.(*channels.ChatChannel)
if ok { if ok {
chatchannel.SendMessage(message) chatchannel.SendMessage(message)
} }

View File

@ -23,7 +23,7 @@ type Channel struct {
Type string Type string
Direction Direction Direction Direction
Handler *Handler Handler Handler
Pending bool Pending bool
ServerHostname string ServerHostname string
ClientHostname string ClientHostname string

View File

@ -45,7 +45,7 @@ func (cm *ChannelManager) OpenChannelRequest(chandler channels.Handler) (*channe
channel.ID = cm.nextFreeChannel channel.ID = cm.nextFreeChannel
cm.nextFreeChannel += 2 cm.nextFreeChannel += 2
channel.Type = chandler.Type() channel.Type = chandler.Type()
channel.Handler = &chandler channel.Handler = chandler
channel.Pending = true channel.Pending = true
channel.Direction = channels.Outbound channel.Direction = channels.Outbound
cm.channels[channel.ID] = channel cm.channels[channel.ID] = channel
@ -76,7 +76,7 @@ func (cm *ChannelManager) OpenChannelRequestFromPeer(channelID int32, chandler c
channel := new(channels.Channel) channel := new(channels.Channel)
channel.ID = channelID channel.ID = channelID
channel.Type = chandler.Type() channel.Type = chandler.Type()
channel.Handler = &chandler channel.Handler = chandler
channel.Pending = true channel.Pending = true
channel.Direction = channels.Inbound channel.Direction = channels.Inbound
@ -90,7 +90,7 @@ func (cm *ChannelManager) OpenChannelRequestFromPeer(channelID int32, chandler c
func (cm *ChannelManager) Channel(ctype string, way channels.Direction) *channels.Channel { func (cm *ChannelManager) Channel(ctype string, way channels.Direction) *channels.Channel {
var foundChannel *channels.Channel var foundChannel *channels.Channel
for _, channel := range cm.channels { for _, channel := range cm.channels {
if (*channel.Handler).Type() == ctype && channel.Direction == way { if channel.Handler.Type() == ctype && channel.Direction == way {
if foundChannel == nil { if foundChannel == nil {
foundChannel = channel foundChannel = channel
} else { } else {

View File

@ -203,11 +203,11 @@ func (rc *Connection) Process(handler Handler) error {
if len(packet.Data) == 0 { if len(packet.Data) == 0 {
rc.traceLog(fmt.Sprintf("removing channel %d", packet.Channel)) rc.traceLog(fmt.Sprintf("removing channel %d", packet.Channel))
rc.channelManager.RemoveChannel(packet.Channel) rc.channelManager.RemoveChannel(packet.Channel)
(*channel.Handler).Closed(utils.ChannelClosedByPeerError) channel.Handler.Closed(utils.ChannelClosedByPeerError)
} else { } else {
rc.traceLog(fmt.Sprintf("received packet on %v channel %d", (*channel.Handler).Type(), packet.Channel)) rc.traceLog(fmt.Sprintf("received packet on %v channel %d", channel.Handler.Type(), packet.Channel))
// Send The Ricochet Packet to the Handler // Send The Ricochet Packet to the Handler
(*channel.Handler).Packet(packet.Data[:]) channel.Handler.Packet(packet.Data[:])
} }
} else { } else {
// When a non-zero packet is received for an unknown // When a non-zero packet is received for an unknown
@ -297,10 +297,10 @@ func (rc *Connection) controlPacket(handler Handler, res *Protocol_Data_Control.
if cr.GetOpened() { if cr.GetOpened() {
rc.traceLog(fmt.Sprintf("channel of type %v opened on %v", channel.Type, id)) rc.traceLog(fmt.Sprintf("channel of type %v opened on %v", channel.Type, id))
(*channel.Handler).OpenOutboundResult(nil, cr) channel.Handler.OpenOutboundResult(nil, cr)
} else { } else {
rc.traceLog(fmt.Sprintf("channel of type %v rejected on %v", channel.Type, id)) rc.traceLog(fmt.Sprintf("channel of type %v rejected on %v", channel.Type, id))
(*channel.Handler).OpenOutboundResult(errors.New(""), cr) channel.Handler.OpenOutboundResult(errors.New(""), cr)
} }
} else if res.GetKeepAlive() != nil { } else if res.GetKeepAlive() != nil {

View File

@ -79,7 +79,7 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string)
channel := rc.Channel("im.ricochet.chat", channels.Outbound) channel := rc.Channel("im.ricochet.chat", channels.Outbound)
if channel != nil { if channel != nil {
log.Printf("Found Chat Channel") log.Printf("Found Chat Channel")
chatchannel, ok := (*channel.Handler).(*channels.ChatChannel) chatchannel, ok := channel.Handler.(*channels.ChatChannel)
if ok { if ok {
chatchannel.SendMessage(message) chatchannel.SendMessage(message)
} }