-
Notifications
You must be signed in to change notification settings - Fork 20
/
runsummary.go
87 lines (70 loc) · 2.13 KB
/
runsummary.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
package flows
import (
"encoding/json"
"github.com/nyaruka/goflow/utils"
)
type runSummary struct {
uuid RunUUID
flow Flow
contact *Contact
status RunStatus
results Results
}
func (r *runSummary) UUID() RunUUID { return r.uuid }
func (r *runSummary) Flow() Flow { return r.flow }
func (r *runSummary) Contact() *Contact { return r.contact }
func (r *runSummary) Status() RunStatus { return r.status }
func (r *runSummary) Results() Results { return r.results }
func NewRunSummaryFromRun(run FlowRun) RunSummary {
return &runSummary{
uuid: run.UUID(),
flow: run.Flow(),
contact: run.Contact().Clone(),
status: run.Status(),
results: run.Results().Clone(),
}
}
var _ RunSummary = (*runSummary)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type runSummaryEnvelope struct {
UUID RunUUID `json:"uuid" validate:"uuid4"`
FlowUUID FlowUUID `json:"flow_uuid" validate:"uuid4"`
Contact json.RawMessage `json:"contact" validate:"required"`
Status RunStatus `json:"status" validate:"required"`
Results Results `json:"results"`
}
func ReadRunSummary(session Session, data json.RawMessage) (RunSummary, error) {
var err error
e := runSummaryEnvelope{}
if err = utils.UnmarshalAndValidate(data, &e, "runsummary"); err != nil {
return nil, err
}
run := &runSummary{
uuid: e.UUID,
status: e.Status,
results: e.Results,
}
// lookup the flow
if run.flow, err = session.Assets().GetFlow(e.FlowUUID); err != nil {
return nil, err
}
// read the contact
if run.contact, err = ReadContact(session, e.Contact); err != nil {
return nil, err
}
return run, nil
}
func (r *runSummary) MarshalJSON() ([]byte, error) {
envelope := runSummaryEnvelope{}
var err error
envelope.UUID = r.uuid
envelope.FlowUUID = r.flow.UUID()
envelope.Status = r.status
envelope.Results = r.results
if envelope.Contact, err = r.contact.MarshalJSON(); err != nil {
return nil, err
}
return json.Marshal(envelope)
}