-
Notifications
You must be signed in to change notification settings - Fork 31
/
promise.go
109 lines (95 loc) · 2.39 KB
/
promise.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package promise
import (
"encoding/json"
"fmt"
"strings"
"github.com/resonatehq/resonate/pkg/idempotency"
)
type Promise struct {
Id string `json:"id"`
State State `json:"state"`
Param Value `json:"param,omitempty"`
Value Value `json:"value,omitempty"`
Timeout int64 `json:"timeout"`
IdempotencyKeyForCreate *idempotency.Key `json:"idempotencyKeyForCreate,omitempty"`
IdempotencyKeyForComplete *idempotency.Key `json:"idempotencyKeyForComplete,omitempty"`
CreatedOn *int64 `json:"createdOn,omitempty"`
CompletedOn *int64 `json:"completedOn,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
SortId int64 `json:"-"` // unexported
}
func (p *Promise) String() string {
return fmt.Sprintf(
"Promise(id=%s, state=%s, param=%s, value=%s, timeout=%d, idempotencyKeyForCreate=%s, idempotencyKeyForUpdate=%s, tags=%s)",
p.Id,
p.State,
p.Param,
p.Value,
p.Timeout,
p.IdempotencyKeyForCreate,
p.IdempotencyKeyForComplete,
p.Tags,
)
}
type State int
const (
Pending State = 1 << iota
Resolved
Rejected
Timedout
Canceled
)
func (s State) String() string {
switch s {
case Pending:
return "PENDING"
case Resolved:
return "RESOLVED"
case Rejected:
return "REJECTED"
case Timedout:
return "REJECTED_TIMEDOUT"
case Canceled:
return "REJECTED_CANCELED"
default:
panic("invalid state")
}
}
func (s *State) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s *State) UnmarshalJSON(data []byte) error {
var state string
if err := json.Unmarshal(data, &state); err != nil {
return err
}
switch strings.ToUpper(state) {
case "PENDING":
*s = Pending
case "RESOLVED":
*s = Resolved
case "REJECTED":
*s = Rejected
case "REJECTED_TIMEDOUT":
*s = Timedout
case "REJECTED_CANCELED":
*s = Canceled
default:
return fmt.Errorf("invalid state '%s'", state)
}
return nil
}
func (s State) In(mask State) bool {
return s&mask != 0
}
type Value struct {
Headers map[string]string `json:"headers,omitempty"`
Data []byte `json:"data,omitempty"`
}
func (v Value) String() string {
return fmt.Sprintf(
"Value(headers=%s, data=%s)",
v.Headers,
string(v.Data),
)
}