/
intent.go
41 lines (32 loc) · 939 Bytes
/
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
package interaction
import (
"reflect"
)
type Intent interface {
InstantiateRootNode(ctx *Context, graph *Graph) (Node, error)
DeriveEdgesForNode(graph *Graph, node Node) ([]Edge, error)
}
type IntentFactory func() Intent
var intentRegistry = map[string]IntentFactory{}
func RegisterIntent(intent Intent) {
intentType := reflect.TypeOf(intent).Elem()
intentKind := intentType.Name()
factory := IntentFactory(func() Intent {
return reflect.New(intentType).Interface().(Intent)
})
if _, hasKind := intentRegistry[intentKind]; hasKind {
panic("interaction: duplicated intent kind: " + intentKind)
}
intentRegistry[intentKind] = factory
}
func IntentKind(intent Intent) string {
intentType := reflect.TypeOf(intent).Elem()
return intentType.Name()
}
func InstantiateIntent(kind string) Intent {
factory, ok := intentRegistry[kind]
if !ok {
panic("interaction: unknown intent kind: " + kind)
}
return factory()
}