/
action.go
58 lines (46 loc) · 1.12 KB
/
action.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
package rules
import (
"errors"
"fmt"
"reflect"
"github.com/mitchellh/mapstructure"
"github.com/doron-cohen/antidot/internal/shell"
)
type ActionContext struct {
KeyValueStore *shell.KeyValueStore
}
type Action interface {
Apply(*ActionContext) error
Pprint()
}
var actionRegistry = make(map[string]Action)
func registerAction(name string, action Action) {
actionRegistry[name] = action
}
func getActionByName(name string) (Action, error) {
action, present := actionRegistry[name]
if !present {
errMessage := fmt.Sprintf("Unknown action type '%s'", name)
return nil, errors.New(errMessage)
} else {
return action, nil
}
}
func actionDecodeHook(sourceType, destType reflect.Type, raw interface{}) (interface{}, error) {
// TODO: find a better way to compare these types
if destType.String() == "rules.Action" {
var err error
var result Action
rawMap := raw.(map[interface{}]interface{})
result, err = getActionByName(rawMap["type"].(string))
if err != nil {
return nil, err
}
err = mapstructure.Decode(raw, &result)
if err != nil {
return nil, err
}
return result, nil
}
return raw, nil
}