-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintent.go
More file actions
50 lines (41 loc) · 1.05 KB
/
intent.go
File metadata and controls
50 lines (41 loc) · 1.05 KB
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
package main
import (
"errors"
"fmt"
)
type Intent interface {
Name() string
Do() error
}
// intentFactory contains a map[string] of all supported intents
// index value is the returned string from the intent's Name() function
type intentFactory struct {
intents map[string]Intent
}
// NewIntentFactory is the constructor function for the intents
func NewIntentFactory() *intentFactory {
factory := &intentFactory{}
factory.intents = make(map[string]Intent)
return factory
}
// Add intent
func (factory *intentFactory) AddIntent(intent Intent) *intentFactory {
factory.intents[intent.Name()] = intent
return factory
}
// Return the specified intent
func (factory *intentFactory) GetIntent(name *string) (Intent, error) {
var err error
intent, registered := factory.intents[*name]
if !registered {
errorMessage := fmt.Sprintf(
"Intent, %s has not been implemented. Available intents:",
*name,
)
for _, val := range factory.intents {
errorMessage += fmt.Sprintf("\n %s", val.Name())
}
err = errors.New(errorMessage)
}
return intent, err
}