/
sub_workflow_states_available.go
50 lines (41 loc) · 1.17 KB
/
sub_workflow_states_available.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
package main
import (
"encoding/json"
"github.com/nats-io/nats"
)
// WFStatesAvailableInput is the representation for the input to
// workflow.status.available
type WFStatesAvailableInput struct {
Issue *Issue `json:"issue"`
}
// WFStatesAvailable holds all related logic for event workflow.states.availabe
type WFStatesAvailable struct{}
// Subscribe to workflow.states.available in order to return
// all available status for the current status of the issue
func (w *WFStatesAvailable) Subscribe(nc *nats.Conn) {
e := ErrorMessage{}
nc.Subscribe("workflow.states.available", func(m *nats.Msg) {
i, err := w.mapInput(m.Data)
if err != nil {
e.Error = err.Error()
nc.Publish(m.Reply, e.toJSON())
return
}
s := i.Issue.AvailableExitStates()
json, err := json.Marshal(s)
if err != nil {
e.Error = err.Error()
nc.Publish(m.Reply, e.toJSON())
return
}
nc.Publish(m.Reply, json)
})
}
// Maps the json input to an InputMove structure
func (w *WFStatesAvailable) mapInput(body []byte) (*WFStatesAvailableInput, error) {
input := WFStatesAvailableInput{}
if err := json.Unmarshal(body, &input); err != nil {
return nil, err
}
return &input, nil
}