-
Notifications
You must be signed in to change notification settings - Fork 14
/
bool.go
66 lines (58 loc) · 1.22 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
package idl
import (
"bytes"
"fmt"
"math/big"
"github.com/aviate-labs/leb128"
)
// BoolType is a type of bool.
type BoolType struct {
primType
}
// Decode decodes a bool value from the given reader.
func (b BoolType) Decode(r *bytes.Reader) (any, error) {
v, err := r.ReadByte()
if err != nil {
return nil, err
}
switch v {
case 0x00:
return false, nil
case 0x01:
return true, nil
default:
return nil, fmt.Errorf("invalid bool values: %x", b)
}
}
// EncodeType returns the leb128 encoding of the bool type.
func (BoolType) EncodeType(_ *TypeDefinitionTable) ([]byte, error) {
return leb128.EncodeSigned(big.NewInt(boolType))
}
// EncodeValue encodes a bool value.
// Accepted types are: `bool`.
func (BoolType) EncodeValue(v any) ([]byte, error) {
v_, ok := v.(bool)
if !ok {
return nil, NewEncodeValueError(v, boolType)
}
if v_ {
return []byte{0x01}, nil
}
return []byte{0x00}, nil
}
// String returns the string representation of the type.
func (BoolType) String() string {
return "bool"
}
func (BoolType) UnmarshalGo(raw any, _v any) error {
v, ok := _v.(*bool)
if !ok {
return NewUnmarshalGoError(raw, _v)
}
b, ok := raw.(bool)
if !ok {
return NewUnmarshalGoError(raw, _v)
}
*v = b
return nil
}