/
compound.go
72 lines (62 loc) · 1.45 KB
/
compound.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
package trigger
import (
"bytes"
"encoding/json"
"fmt"
"github.com/proidiot/gone/log"
"github.com/stuphlabs/pullcord/config"
)
// CompoundTrigger is a Triggerrer that allows more than one trigger to be
// fired off at a time. It any trigger returns an error, it isn't guaranteed
// that all triggers will fire.
type CompoundTrigger struct {
Triggers []Triggerrer
}
func init() {
config.MustRegisterResourceType(
"compoundtrigger",
func() json.Unmarshaler {
return new(CompoundTrigger)
},
)
}
// UnmarshalJSON implements encoding/json.Unmarshaler.
func (c *CompoundTrigger) UnmarshalJSON(input []byte) error {
var t struct {
Triggers []config.Resource
}
dec := json.NewDecoder(bytes.NewReader(input))
if e := dec.Decode(&t); e != nil {
return e
}
c.Triggers = make([]Triggerrer, len(t.Triggers))
for _, i := range t.Triggers {
th := i.Unmarshalled
switch th := th.(type) {
case Triggerrer:
c.Triggers = append(c.Triggers, th)
default:
_ = log.Err(
fmt.Sprintf(
"Registry value is not a"+
" RequestFilter: %s",
th,
),
)
return config.UnexpectedResourceType
}
}
return nil
}
// Trigger executes all the child triggers, exiting immediately after a single
// failure.
func (c *CompoundTrigger) Trigger() error {
_ = log.Debug("compound trigger initiated")
for _, t := range c.Triggers {
if err := t.Trigger(); err != nil {
return err
}
}
_ = log.Debug("compound trigger completed")
return nil
}