This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
/
intent.go
91 lines (79 loc) · 2.16 KB
/
intent.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package content
import (
"errors"
"fmt"
"io"
)
var handlers = map[string]Handler{}
type (
IntentHandler interface {
CanHandle(it *Intent) bool
Handle(it *Intent) *Response
}
// TODO(@dskinner): Replace Handler with IntentHandler or something else?
Handler func(io.Writer, Intent)
Intent struct {
Version int64
Operation string
Data Settings
}
Response struct {
Version int64 `json:"version"`
Data Settings `json:"data"`
}
Session struct {
Settings Settings
}
)
func NewIntent(key string) (ret Intent) {
ret.Operation = key
ret.Data = *NewSettings()
return
}
func NewResponse() (ret Response) {
ret.Data = *NewSettings()
return
}
// Returns the Session object associated with this Intent or nil.
func (it *Intent) Session() *Session {
// TODO(someone): Intent.Session not implemented yet
if sessionid, ok := it.Data.Get("sessionid").(int); ok {
_ = sessionid // TODO actually look up session and return it
}
return nil
}
// If a Session is associated with this Intent, then the Session's
// Settings is cloned and the intent's Settings are merged into the
// cloned Settings object. This to allow Intents to be "slim", i.e.
// only contain the keys of the specific settings it wants to override.
//
// If there's no Session nor Intent Settings, the empty Settings object
// is returned.
func (it *Intent) Settings() *Settings {
if session := it.Session(); session != nil {
set := session.Settings.Clone()
if settings, ok := it.Data.Get("settings").(Settings); ok {
set.Merge(&settings)
}
return set
} else if settings, ok := it.Data.Get("settings").(Settings); ok {
return &settings
}
return NewSettings()
}
func AddHandler(name string, h Handler) {
if _, ok := handlers[name]; ok {
// immediately exit to prevent double-register which should
// come up during development & testing.
panic(fmt.Sprintf("Handler already registered for %s", name))
}
handlers[name] = h
}
func Handle(conn io.Writer, intent Intent) error {
if handler, ok := handlers[intent.Operation]; !ok {
return errors.New(fmt.Sprintf("Failed to locate handler for operation %s", intent.Operation))
} else {
handler(conn, intent)
}
return nil
}