/
webhook_filters.go
112 lines (100 loc) · 2.24 KB
/
webhook_filters.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
package content
import (
"encoding/json"
"errors"
)
type WebhookFilter interface{}
func mapWebhookFilter(input WebhookFilter) (WebhookFilter, error) {
data, ok := input.(map[string]interface{})
if !ok {
return nil, errors.New("Invalid data")
}
discriminator, ok := data["type"].(string)
if !ok {
return nil, errors.New("Error processing discriminator field 'type'")
}
args, ok := data["arguments"].([]interface{})
if !ok {
return nil, errors.New("Error processing arguments")
}
switch discriminator {
case "equal":
new := WebhookFilterEqual{Type: "equal"}
for _, arg := range args {
mapArg := arg.(map[string]interface{})
if value, ok := mapArg["jsonPath"]; ok {
new.JSONPath = value.(string)
}
if value, ok := mapArg["value"]; ok {
new.Value = value.(string)
}
}
return new, nil
case "in":
new := WebhookFilterIn{Type: "in"}
for _, arg := range args {
mapArg := arg.(map[string]interface{})
if value, ok := mapArg["jsonPath"]; ok {
new.JSONPath = value.(string)
}
if value, ok := mapArg["value"].([]interface{}); ok {
new.Values = make([]string, len(value))
for i, valItem := range value {
new.Values[i] = valItem.(string)
}
}
}
return new, nil
}
return nil, nil
}
type WebhookFilterEqual struct {
Type string `json:"type"`
JSONPath string
Value string
}
func (obj WebhookFilterEqual) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type string `json:"type"`
Arguments []interface{} `json:"arguments"`
}{
Type: "equal",
Arguments: []interface{}{
struct {
JSONPath string `json:"jsonPath"`
}{
JSONPath: obj.JSONPath,
},
struct {
Value string `json:"value"`
}{
Value: obj.Value,
},
},
})
}
type WebhookFilterIn struct {
Type string `json:"type"`
JSONPath string
Values []string `json:"values"`
}
func (obj WebhookFilterIn) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type string `json:"type"`
Arguments []interface{} `json:"arguments"`
}{
Type: "in",
Arguments: []interface{}{
struct {
JSONPath string `json:"jsonPath"`
}{
JSONPath: obj.JSONPath,
},
struct {
Value []string `json:"value"`
}{
Value: obj.Values,
},
},
})
}