forked from danryan/hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
67 lines (55 loc) · 1.38 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package hal
import (
"fmt"
)
// Adapter interface
type Adapter interface {
// New() (Adapter, error)
Run() error
Stop() error
Receive(*Message) error
Send(*Response, ...string) error
Emote(*Response, ...string) error
Reply(*Response, ...string) error
Topic(*Response, ...string) error
Play(*Response, ...string) error
String() string
}
type adapter struct {
name string
newFunc func(*Robot) (Adapter, error)
sendChan chan *Response
recvChan chan *Message
}
// AvailableAdapters is a map of registered adapters
var AvailableAdapters = map[string]adapter{}
// NewAdapter creates a new initialized adapter
func NewAdapter(robot *Robot) (Adapter, error) {
name := Config.AdapterName
if _, ok := AvailableAdapters[name]; !ok {
return nil, fmt.Errorf("%s is not a registered adapter", Config.AdapterName)
}
adapter, err := AvailableAdapters[name].newFunc(robot)
if err != nil {
return nil, err
}
return adapter, nil
}
// RegisterAdapter registers an adapter
func RegisterAdapter(name string, newFunc func(*Robot) (Adapter, error)) {
AvailableAdapters[name] = adapter{
name: name,
newFunc: newFunc,
}
}
// BasicAdapter declares common functions shared by all adapters
type BasicAdapter struct {
*Robot
}
// SetRobot sets the adapter's Robot
func (a *BasicAdapter) SetRobot(r *Robot) {
a.Robot = r
}
func (a *BasicAdapter) String() string {
return Config.AdapterName
}