gobot.io/x/gobot@v1.16.0/commander.go (about)

     1  package gobot
     2  
     3  type commander struct {
     4  	commands map[string]func(map[string]interface{}) interface{}
     5  }
     6  
     7  // Commander is the interface which describes the behaviour for a Driver or Adaptor
     8  // which exposes API commands.
     9  type Commander interface {
    10  	// Command returns a command given a name. Returns nil if the command is not found.
    11  	Command(string) (command func(map[string]interface{}) interface{})
    12  	// Commands returns a map of commands.
    13  	Commands() (commands map[string]func(map[string]interface{}) interface{})
    14  	// AddCommand adds a command given a name.
    15  	AddCommand(name string, command func(map[string]interface{}) interface{})
    16  }
    17  
    18  // NewCommander returns a new Commander.
    19  func NewCommander() Commander {
    20  	return &commander{
    21  		commands: make(map[string]func(map[string]interface{}) interface{}),
    22  	}
    23  }
    24  
    25  // Command returns the command interface whene passed a valid command name
    26  func (c *commander) Command(name string) (command func(map[string]interface{}) interface{}) {
    27  	command, _ = c.commands[name]
    28  	return
    29  }
    30  
    31  // Commands returns the entire map of valid commands
    32  func (c *commander) Commands() map[string]func(map[string]interface{}) interface{} {
    33  	return c.commands
    34  }
    35  
    36  // AddCommand adds a new command, when passed a command name and the command interface.
    37  func (c *commander) AddCommand(name string, command func(map[string]interface{}) interface{}) {
    38  	c.commands[name] = command
    39  }