forked from lysu/go-saga
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
61 lines (53 loc) · 1.23 KB
/
log.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
package saga
import (
"encoding/json"
"time"
)
// LogType present type flag for Log
type LogType int
const (
// SagaStart flag saga stared log
SagaStart LogType = iota + 1
// SagaEnd flag saga ended log
SagaEnd
// SagaAbort flag saga aborted
SagaAbort
// ActionStart flag action start log
ActionStart
// ActionEnd flag action end log
ActionEnd
// CompensateStart flag compensate start log
CompensateStart
// CompensateEnd flag compensate end log
CompensateEnd
)
// Log presents Saga Log.
// Saga Log used to log execute status for saga,
// and SEC use it to compensate and retry.
type Log struct {
Type LogType `json:"type,omitempty"`
SubTxID string `json:"subTxID,omitempty"`
Time time.Time `json:"time,omitempty"`
Params []ParamData `json:"params,omitempty"`
}
func (l *Log) mustMarshal() string {
return mustMarshal(l)
}
func mustUnmarshalLog(data string) Log {
var log Log
mustUnmarshal([]byte(data), &log)
return log
}
func mustMarshal(value interface{}) string {
s, err := json.Marshal(value)
if err != nil {
panic("Marshal Failure")
}
return string(s)
}
func mustUnmarshal(data []byte, v interface{}) {
err := json.Unmarshal([]byte(data), v)
if err != nil {
panic("Unmarshal Failure")
}
}