/
receiver.go
155 lines (135 loc) · 3.29 KB
/
receiver.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package config
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"text/template"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v3"
)
var (
ErrInvalidActionType = errors.New("invalid action type")
)
// Config represents a config.yml.
type Config struct {
Version string `yaml:"version"`
Handlers []EventHandler `yaml:"handlers"`
}
// EventHandler is event handler which contains action names.
type EventHandler struct {
Subscription string `yaml:"subscription"`
Action Action `yaml:"action"`
}
// ActionType represents action type.
type ActionType string
const (
// ActionNone is uncategorized action type.
ActionNone ActionType = ""
// ActionHTTP is action type for HTTP action.
ActionHTTP ActionType = "http"
)
// Validate validates action type.
func (t ActionType) Validate() error {
switch t {
case ActionHTTP:
return nil
case ActionNone:
return ErrInvalidActionType
default:
return ErrInvalidActionType
}
}
// Action is action definition.
type Action struct {
Type ActionType `yaml:"type"`
HTTPRequestAction `yaml:",inline"`
}
// HTTPRequestAction is configuration of HTTP action.
type HTTPRequestAction struct {
Method string `yaml:"method"`
Header http.Header `yaml:"header"`
URL string `yaml:"url"`
Payload *yamlTemplate `yaml:"payload"`
}
type yamlTemplate struct {
tmpl *template.Template
res gjson.Result
}
func (t *yamlTemplate) UnmarshalYAML(node *yaml.Node) error {
b := &bytes.Buffer{}
e := yaml.NewEncoder(b)
defer e.Close()
if err := e.Encode(node); err != nil {
return err
}
funcs := template.FuncMap{
"path": func(path string) string { return t.res.Get(path).String() },
}
tmpl, err := template.New("tmpl").Delims("${", "}").Funcs(funcs).Parse(b.String())
if err != nil {
return err
}
t.tmpl = tmpl
return nil
}
func (t *yamlTemplate) Render(jsonData []byte) ([]byte, error) {
t.res = gjson.ParseBytes(jsonData)
b := &bytes.Buffer{}
if err := t.tmpl.Execute(b, nil); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (t *yamlTemplate) RenderMap(jsonData []byte) (map[string]interface{}, error) {
y, err := t.Render(jsonData)
if err != nil {
return nil, err
}
m := make(map[string]interface{})
if err := yaml.Unmarshal(y, m); err != nil {
return nil, err
}
return m, nil
}
func (t *yamlTemplate) RenderJSON(jsonData []byte) ([]byte, error) {
m, err := t.RenderMap(jsonData)
if err != nil {
return nil, err
}
j, err := json.Marshal(m)
if err != nil {
return nil, err
}
return j, nil
}
// Parse parses config file and returns config data.
func Parse(data []byte) (Config, error) {
config := Config{}
if err := yaml.Unmarshal(data, &config); err != nil {
return Config{}, err
}
if err := config.Validate(); err != nil {
return Config{}, fmt.Errorf("validation error: %w", err)
}
return config, nil
}
// Validate validates config.
func (c *Config) Validate() error {
if c.Version == "" {
return errors.New("version is required")
}
if len(c.Handlers) == 0 {
return errors.New("handler should be defined one or more")
}
for _, a := range c.Handlers {
if a.Subscription == "" {
return errors.New("subscription should be defined")
}
if err := a.Action.Type.Validate(); err != nil {
return fmt.Errorf("action type validation failed: %w", err)
}
}
return nil
}