-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
status.go
92 lines (78 loc) · 1.66 KB
/
status.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
package tunnel
import (
"encoding/json"
"errors"
"strings"
"sync/atomic"
)
type TunnelStatus int
// StatusMapping is a mapping for Status enum
var StatusMapping = map[string]TunnelStatus{
Suspend.String(): Suspend,
Inner.String(): Inner,
Running.String(): Running,
}
const (
Suspend TunnelStatus = iota
Inner
Running
)
// UnmarshalJSON unserialize Status
func (s *TunnelStatus) UnmarshalJSON(data []byte) error {
var tp string
json.Unmarshal(data, &tp)
status, exist := StatusMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid mode")
}
*s = status
return nil
}
// UnmarshalYAML unserialize Status with yaml
func (s *TunnelStatus) UnmarshalYAML(unmarshal func(any) error) error {
var tp string
unmarshal(&tp)
status, exist := StatusMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid status")
}
*s = status
return nil
}
// MarshalJSON serialize Status
func (s TunnelStatus) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
// MarshalYAML serialize TunnelMode with yaml
func (s TunnelStatus) MarshalYAML() (any, error) {
return s.String(), nil
}
func (s TunnelStatus) String() string {
switch s {
case Suspend:
return "suspend"
case Inner:
return "inner"
case Running:
return "running"
default:
return "Unknown"
}
}
type AtomicStatus struct {
value atomic.Int32
}
func (a *AtomicStatus) Store(s TunnelStatus) {
a.value.Store(int32(s))
}
func (a *AtomicStatus) Load() TunnelStatus {
return TunnelStatus(a.value.Load())
}
func (a *AtomicStatus) String() string {
return a.Load().String()
}
func newAtomicStatus(s TunnelStatus) *AtomicStatus {
a := &AtomicStatus{}
a.Store(s)
return a
}