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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
package lib
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
var (
inputConfigCreatorCache = make(map[string]inputConfigCreator)
outputConfigCreatorCache = make(map[string]outputConfigCreator)
)
type inputConfigCreator func(Action, json.RawMessage) (InputConverter, error)
type outputConfigCreator func(Action, json.RawMessage) (OutputConverter, error)
func RegisterInputConfigCreator(id string, fn inputConfigCreator) error {
id = strings.ToLower(id)
if _, found := inputConfigCreatorCache[id]; found {
return errors.New("config creator has already been registered")
}
inputConfigCreatorCache[id] = fn
return nil
}
func createInputConfig(id string, action Action, data json.RawMessage) (InputConverter, error) {
id = strings.ToLower(id)
fn, found := inputConfigCreatorCache[id]
if !found {
return nil, errors.New("unknown config type")
}
return fn(action, data)
}
func RegisterOutputConfigCreator(id string, fn outputConfigCreator) error {
id = strings.ToLower(id)
if _, found := outputConfigCreatorCache[id]; found {
return errors.New("config creator has already been registered")
}
outputConfigCreatorCache[id] = fn
return nil
}
func createOutputConfig(id string, action Action, data json.RawMessage) (OutputConverter, error) {
id = strings.ToLower(id)
fn, found := outputConfigCreatorCache[id]
if !found {
return nil, errors.New("unknown config type")
}
return fn(action, data)
}
type config struct {
Input []*inputConvConfig `json:"input"`
Output []*outputConvConfig `json:"output"`
}
type inputConvConfig struct {
iType string
action Action
converter InputConverter
}
func (i *inputConvConfig) UnmarshalJSON(data []byte) error {
var temp struct {
Type string `json:"type"`
Action Action `json:"action"`
Args json.RawMessage `json:"args"`
}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
if !ActionsRegistry[temp.Action] {
return fmt.Errorf("invalid action %s in type %s", temp.Action, temp.Type)
}
config, err := createInputConfig(temp.Type, temp.Action, temp.Args)
if err != nil {
return err
}
i.iType = config.GetType()
i.action = config.GetAction()
i.converter = config
return nil
}
type outputConvConfig struct {
iType string
action Action
converter OutputConverter
}
func (i *outputConvConfig) UnmarshalJSON(data []byte) error {
var temp struct {
Type string `json:"type"`
Action Action `json:"action"`
Args json.RawMessage `json:"args"`
}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
if temp.Action == "" {
temp.Action = ActionOutput
}
if !ActionsRegistry[temp.Action] {
return fmt.Errorf("invalid action %s in type %s", temp.Action, temp.Type)
}
config, err := createOutputConfig(temp.Type, temp.Action, temp.Args)
if err != nil {
return err
}
i.iType = config.GetType()
i.action = config.GetAction()
i.converter = config
return nil
}
|