-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
65 lines (54 loc) · 1.26 KB
/
config.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
package main
import (
"encoding/json"
"github.com/bwmarrin/discordgo"
"github.com/kazokuco/grappler/discourse"
"github.com/kazokuco/grappler/unity/cloudbuild"
"github.com/pkg/errors"
"net/http"
)
type Impl interface {
Handle(req *http.Request, body []byte) (*discordgo.WebhookParams, error)
}
type Outbound struct {
ID int64 `json:"id"`
Token string `json:"token"`
}
type HookFields struct {
Key string `json:"key"`
Type string `json:"type"`
Invoke []Outbound `json:"invoke"`
}
type Hook struct {
HookFields
// Created by deserializing the payload.
Impl Impl `json:"-"`
}
func (hc *Hook) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &hc.HookFields); err != nil {
return nil
}
if hc.HookFields.Key == "" {
return errors.New("missing key for hook")
}
switch hc.HookFields.Type {
case "discourse":
var impl discourse.Hook
if err := json.Unmarshal(data, &impl); err != nil {
return err
}
hc.Impl = Impl(&impl)
case "unity.cloudbuild":
var impl cloudbuild.Hook
if err := json.Unmarshal(data, &impl); err != nil {
return err
}
hc.Impl = Impl(&impl)
default:
return errors.Errorf("unknown hook type: %s", hc.HookFields.Type)
}
return nil
}
type Config struct {
Hooks []Hook `json:"hooks"`
}