-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
bridge_run_result.go
50 lines (42 loc) · 1.41 KB
/
bridge_run_result.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 models
import (
"encoding/json"
"errors"
null "gopkg.in/guregu/null.v4"
)
// BridgeRunResult handles the parsing of RunResults from external adapters.
type BridgeRunResult struct {
Data JSON `json:"data"`
Status RunStatus `json:"status"`
ErrorMessage null.String `json:"error"`
ExternalPending bool `json:"pending"`
AccessToken string `json:"accessToken"`
}
// UnmarshalJSON parses the given input and updates the BridgeRunResult in the
// external adapter format.
func (brr *BridgeRunResult) UnmarshalJSON(input []byte) error {
// XXX: This indirection prevents an infinite regress during json.Unmarshal
type biAlias BridgeRunResult
var anon biAlias
err := json.Unmarshal(input, &anon)
*brr = BridgeRunResult(anon)
if brr.Status == RunStatusErrored || brr.ErrorMessage.Valid {
brr.Status = RunStatusErrored
} else if brr.ExternalPending || brr.Status.PendingBridge() {
brr.Status = RunStatusPendingBridge
} else {
brr.Status = RunStatusCompleted
}
return err
}
// HasError returns true if the status is errored or the error message is set
func (brr BridgeRunResult) HasError() bool {
return brr.Status == RunStatusErrored || brr.ErrorMessage.Valid
}
// GetError returns the error of a BridgeRunResult if it is present.
func (brr BridgeRunResult) GetError() error {
if brr.HasError() {
return errors.New(brr.ErrorMessage.ValueOrZero())
}
return nil
}