gobot.io/x/gobot/v2@v2.1.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 when passed a valid command name
    26  func (c *commander) Command(name string) func(map[string]interface{}) interface{} {
    27  	return c.commands[name]
    28  }
    29  
    30  // Commands returns the entire map of valid commands
    31  func (c *commander) Commands() map[string]func(map[string]interface{}) interface{} {
    32  	return c.commands
    33  }
    34  
    35  // AddCommand adds a new command, when passed a command name and the command interface.
    36  func (c *commander) AddCommand(name string, command func(map[string]interface{}) interface{}) {
    37  	c.commands[name] = command
    38  }