-
Notifications
You must be signed in to change notification settings - Fork 1
/
bool.go
74 lines (62 loc) · 1.16 KB
/
bool.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
package jaws
import (
"strconv"
"sync"
)
// Bool wraps a mutex and a boolean, and implements jaws.BoolSetter.
type Bool struct {
mu sync.Mutex
Value bool
}
func (s *Bool) Set(val bool) {
s.mu.Lock()
s.Value = val
s.mu.Unlock()
}
func (s *Bool) Get() (val bool) {
s.mu.Lock()
val = s.Value
s.mu.Unlock()
return
}
func (s *Bool) Swap(val bool) (old bool) {
s.mu.Lock()
old, s.Value = s.Value, val
s.mu.Unlock()
return
}
func (s *Bool) String() string {
if s.Get() {
return "true"
}
return "false"
}
func (s *Bool) JawsGetBool(*Element) bool {
return s.Get()
}
func (s *Bool) JawsSetBool(e *Element, val bool) error {
if s.Swap(val) == val {
return ErrValueUnchanged
}
return nil
}
func (s *Bool) MarshalJSON() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *Bool) UnmarshalJSON(b []byte) (err error) {
var val bool
if val, err = strconv.ParseBool(string(b)); err == nil {
s.Set(val)
}
return
}
func (s *Bool) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *Bool) UnmarshalText(b []byte) (err error) {
var val bool
if val, err = strconv.ParseBool(string(b)); err == nil {
s.Set(val)
}
return
}