forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool.go
59 lines (49 loc) · 1 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
package ormfield
import (
io "io"
"google.golang.org/protobuf/reflect/protoreflect"
)
// BoolCodec encodes a bool value as a single byte 0 or 1.
type BoolCodec struct{}
func (b BoolCodec) Decode(r Reader) (protoreflect.Value, error) {
x, err := r.ReadByte()
return protoreflect.ValueOfBool(x != 0), err
}
var (
zeroBz = []byte{0}
oneBz = []byte{1}
)
func (b BoolCodec) Encode(value protoreflect.Value, w io.Writer) error {
var err error
if !value.IsValid() || !value.Bool() {
_, err = w.Write(zeroBz)
} else {
_, err = w.Write(oneBz)
}
return err
}
func (b BoolCodec) Compare(v1, v2 protoreflect.Value) int {
var b1, b2 bool
if v1.IsValid() {
b1 = v1.Bool()
}
if v2.IsValid() {
b2 = v2.Bool()
}
if b1 == b2 {
return 0
} else if b1 {
return -1
} else {
return 1
}
}
func (b BoolCodec) IsOrdered() bool {
return false
}
func (b BoolCodec) FixedBufferSize() int {
return 1
}
func (b BoolCodec) ComputeBufferSize(protoreflect.Value) (int, error) {
return b.FixedBufferSize(), nil
}