forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte.go
107 lines (91 loc) · 2.6 KB
/
byte.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package null
import (
"database/sql"
"strconv"
"github.com/guregu/null/v5/internal"
)
// Byte is an nullable byte.
// It does not consider zero values to be null.
// It will decode to null, not zero, if null.
type Byte struct {
sql.NullByte
}
// NewByte creates a new Byte.
func NewByte(b byte, valid bool) Byte {
return Byte{
NullByte: sql.NullByte{
Byte: b,
Valid: valid,
},
}
}
// ByteFrom creates a new Byte that will always be valid.
func ByteFrom(b byte) Byte {
return NewByte(b, true)
}
// ByteFromPtr creates a new Byte that be null if i is nil.
func ByteFromPtr(b *byte) Byte {
if b == nil {
return NewByte(0, false)
}
return NewByte(*b, true)
}
// ValueOrZero returns the inner value if valid, otherwise zero.
func (b Byte) ValueOrZero() byte {
if !b.Valid {
return 0
}
return b.Byte
}
// UnmarshalJSON implements json.Unmarshaler.
// It supports number, string, and null input.
// 0 will not be considered a null Byte.
func (b *Byte) UnmarshalJSON(data []byte) error {
return internal.UnmarshalIntJSON(data, &b.Byte, &b.Valid, 8, strconv.ParseUint)
}
// UnmarshalText implements encoding.TextUnmarshaler.
// It will unmarshal to a null Byte if the input is blank.
// It will return an error if the input is not an integer, blank, or "null".
func (b *Byte) UnmarshalText(text []byte) error {
return internal.UnmarshalIntText(text, &b.Byte, &b.Valid, 8, strconv.ParseUint)
}
// MarshalJSON implements json.Marshaler.
// It will encode null if this Byte is null.
func (b Byte) MarshalJSON() ([]byte, error) {
if !b.Valid {
return []byte("null"), nil
}
return []byte(strconv.FormatInt(int64(b.Byte), 10)), nil
}
// MarshalText implements encoding.TextMarshaler.
// It will encode a blank string if this Byte is null.
func (b Byte) MarshalText() ([]byte, error) {
if !b.Valid {
return []byte{}, nil
}
return []byte(strconv.FormatInt(int64(b.Byte), 10)), nil
}
// SetValid changes this Byte's value and also sets it to be non-null.
func (b *Byte) SetValid(n byte) {
b.Byte = n
b.Valid = true
}
// Ptr returns a pointer to this Byte's value, or a nil pointer if this Byte is null.
func (b Byte) Ptr() *byte {
if !b.Valid {
return nil
}
return &b.Byte
}
// IsZero returns true for invalid Bytes, for future omitempty support (Go 1.4?)
// A non-null Byte with a 0 value will not be considered zero.
func (b Byte) IsZero() bool {
return !b.Valid
}
// Equal returns true if both ints have the same value or are both null.
func (b Byte) Equal(other Byte) bool {
return b.Valid == other.Valid && (!b.Valid || b.Byte == other.Byte)
}
func (b Byte) value() (int64, bool) {
return int64(b.Byte), b.Valid
}