From 6f7410c7a0ba2697b8d03d74143fef3b4feedda6 Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Fri, 26 Dec 2014 17:59:29 -0800 Subject: [PATCH] Making a dent in golint: 94 -> 70 --- chat/channel.go | 6 ++++-- chat/command.go | 16 +++++++++++++--- chat/set.go | 35 ++++++++++++++++++----------------- host.go | 2 +- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/chat/channel.go b/chat/channel.go index 2107419..646fe0c 100644 --- a/chat/channel.go +++ b/chat/channel.go @@ -9,6 +9,8 @@ import ( const historyLen = 20 const channelBuffer = 10 +// The error returned when a message is sent to a channel that is already +// closed. var ErrChannelClosed = errors.New("channel closed") // Channel definition, also a Set of User Items @@ -22,7 +24,7 @@ type Channel struct { closeOnce sync.Once } -// Create new channel and start broadcasting goroutine. +// NewChannel creates a new channel. func NewChannel() *Channel { broadcast := make(chan Message, channelBuffer) @@ -46,7 +48,7 @@ func (ch *Channel) Close() { }) } -// Handle a message, will block until done. +// HandleMsg reacts to a message, will block until done. func (ch *Channel) HandleMsg(m Message) { switch m := m.(type) { case *CommandMsg: diff --git a/chat/command.go b/chat/command.go index 0225662..cb329d1 100644 --- a/chat/command.go +++ b/chat/command.go @@ -8,18 +8,27 @@ import ( "sync" ) +// The error returned when an invalid command is issued. var ErrInvalidCommand = errors.New("invalid command") + +// The error returned when a command is given without an owner. var ErrNoOwner = errors.New("command without owner") + +// The error returned when a command is performed without the necessary number +// of arguments. var ErrMissingArg = errors.New("missing argument") +// CommandHandler is the function signature for command handlers.. type CommandHandler func(*Channel, CommandMsg) error +// Commands is a registry of available commands. type Commands struct { handlers map[string]CommandHandler help map[string]string sync.RWMutex } +// NewCommands returns a new Commands registry. func NewCommands() *Commands { return &Commands{ handlers: map[string]CommandHandler{}, @@ -27,7 +36,8 @@ func NewCommands() *Commands { } } -// Register command. If help string is empty, it will be hidden from Help(). +// Add will register a command. If help string is empty, it will be hidden from +// Help(). func (c Commands) Add(command string, help string, handler CommandHandler) { c.Lock() defer c.Unlock() @@ -52,9 +62,9 @@ func (c Commands) Alias(command string, alias string) error { return nil } -// Execute command message, assumes IsCommand was checked. +// Run executes a command message. func (c Commands) Run(channel *Channel, msg CommandMsg) error { - if msg.from == nil { + if msg.From == nil { return ErrNoOwner } diff --git a/chat/set.go b/chat/set.go index 7ee9281..2248de8 100644 --- a/chat/set.go +++ b/chat/set.go @@ -6,35 +6,35 @@ import ( "sync" ) -var ErrIdTaken error = errors.New("id already taken") -var ErrItemMissing error = errors.New("item does not exist") +// The error returned when an added id already exists in the set. +var ErrIdTaken = errors.New("id already taken") -// Unique identifier for an item +// The error returned when a requested item does not exist in the set. +var ErrItemMissing = errors.New("item does not exist") + +// Id is a unique identifier for an item. type Id string -// A prefix for a unique identifier -type IdPrefix Id - -// An interface for items to store-able in the set +// Item is an interface for items to store-able in the set type Item interface { Id() Id } -// Set with string lookup +// Set with string lookup. // TODO: Add trie for efficient prefix lookup? type Set struct { lookup map[Id]Item sync.RWMutex } -// Create a new set +// NewSet creates a new set. func NewSet() *Set { return &Set{ lookup: map[Id]Item{}, } } -// Remove all items and return the number removed +// Clear removes all items and returns the number removed. func (s *Set) Clear() int { s.Lock() n := len(s.lookup) @@ -43,12 +43,12 @@ func (s *Set) Clear() int { return n } -// Size of the set right now +// Len returns the size of the set right now. func (s *Set) Len() int { return len(s.lookup) } -// Check if user belongs in this set +// In checks if an item exists in this set. func (s *Set) In(item Item) bool { s.RLock() _, ok := s.lookup[item.Id()] @@ -56,7 +56,7 @@ func (s *Set) In(item Item) bool { return ok } -// Get user by name +// Get returns an item with the given Id. func (s *Set) Get(id Id) (Item, error) { s.RLock() item, ok := s.lookup[id] @@ -69,7 +69,7 @@ func (s *Set) Get(id Id) (Item, error) { return item, nil } -// Add user to set if user does not exist already +// Add item to this set if it does not exist already. func (s *Set) Add(item Item) error { s.Lock() defer s.Unlock() @@ -83,7 +83,7 @@ func (s *Set) Add(item Item) error { return nil } -// Remove user from set +// Remove item from this set. func (s *Set) Remove(item Item) error { s.Lock() defer s.Unlock() @@ -96,7 +96,8 @@ func (s *Set) Remove(item Item) error { return nil } -// Loop over every item while holding a read lock and apply fn +// Each loops over every item while holding a read lock and applies fn to each +// element. func (s *Set) Each(fn func(item Item)) { s.RLock() for _, item := range s.lookup { @@ -105,7 +106,7 @@ func (s *Set) Each(fn func(item Item)) { s.RUnlock() } -// List users by prefix, case insensitive +// ListPrefix returns a list of items with a prefix, case insensitive. func (s *Set) ListPrefix(prefix string) []Item { r := []Item{} prefix = strings.ToLower(prefix) diff --git a/host.go b/host.go index 2c85ccd..680d553 100644 --- a/host.go +++ b/host.go @@ -122,7 +122,7 @@ func (h *Host) AutoCompleteFunction(line string, pos int, key rune) (newLine str return } -// RefreshPrompt will update the terminal prompt with the latest user name. +// GetPrompt will render the terminal prompt string based on the user. func GetPrompt(user *chat.User) string { name := user.Name() if user.Config.Theme != nil {