/
types.go
67 lines (56 loc) · 1.26 KB
/
types.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 core
import (
"fmt"
)
// Category denotes whether a node is a composite, decorator or leaf.
type Category string
// A list of behavior tree node categories.
const (
CategoryInvalid = Category("invalid")
CategoryComposite = Category("composite")
CategoryDecorator = Category("decorator")
CategoryLeaf = Category("leaf")
)
// Status denotes the return value of the execution of a node.
type Status int
// A list of possible statuses.
const (
StatusInvalid Status = iota
StatusSuccess
StatusFailure
StatusRunning
)
type (
// Params denotes a list of parameters to a node.
Params map[string]interface{}
// Returns is just a type alias for Params.
Returns = Params
)
func (p Params) GetInt(key string) (int, error) {
val, ok := p[key]
if !ok {
return 0, ErrParamNotFound(key)
}
n, ok := val.(int)
if !ok {
return 0, ErrInvalidType(key)
}
return n, nil
}
func (p Params) GetString(key string) (string, error) {
val, ok := p[key]
if !ok {
return "", ErrParamNotFound(key)
}
s, ok := val.(string)
if !ok {
return "", ErrInvalidType(key)
}
return s, nil
}
func ErrParamNotFound(name string) error {
return fmt.Errorf("parameter %s not found", name)
}
func ErrInvalidType(name string) error {
return fmt.Errorf("invalid type for %s", name)
}