forked from volatiletech/null
-
Notifications
You must be signed in to change notification settings - Fork 1
/
byte.go
146 lines (124 loc) · 2.73 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package null
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
)
// Byte is an nullable int.
type Byte struct {
Byte byte
Valid bool
}
// NewByte creates a new Byte
func NewByte(b byte, valid bool) Byte {
return Byte{
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)
}
// UnmarshalJSON implements json.Unmarshaler.
func (b *Byte) UnmarshalJSON(data []byte) error {
if len(data) == 0 || bytes.Equal(data, NullBytes) {
b.Valid = false
b.Byte = 0
return nil
}
var x string
if err := json.Unmarshal(data, &x); err != nil {
return err
}
if len(x) > 1 {
return errors.New("json: cannot convert to byte, text len is greater than one")
}
b.Byte = x[0]
b.Valid = true
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *Byte) UnmarshalText(text []byte) error {
if text == nil || len(text) == 0 {
b.Valid = false
return nil
}
if len(text) > 1 {
return errors.New("text: cannot convert to byte, text len is greater than one")
}
b.Valid = true
b.Byte = text[0]
return nil
}
// MarshalJSON implements json.Marshaler.
func (b Byte) MarshalJSON() ([]byte, error) {
if !b.Valid {
return NullBytes, nil
}
return []byte{'"', b.Byte, '"'}, nil
}
// MarshalText implements encoding.TextMarshaler.
func (b Byte) MarshalText() ([]byte, error) {
if !b.Valid {
return []byte{}, nil
}
return []byte{b.Byte}, 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?)
func (b Byte) IsZero() bool {
return !b.Valid
}
// Scan implements the Scanner interface.
func (b *Byte) Scan(value interface{}) error {
if value == nil {
b.Byte, b.Valid = 0, false
return nil
}
val := value.(string)
if len(val) == 0 {
b.Valid = false
b.Byte = 0
return nil
}
b.Valid = true
b.Byte = byte(val[0])
return nil
}
// Value implements the driver Valuer interface.
func (b Byte) Value() (driver.Value, error) {
if !b.Valid {
return nil, nil
}
return []byte{b.Byte}, nil
}
// Randomize for sqlboiler
func (b *Byte) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) {
if shouldBeNull {
b.Byte = byte(0)
b.Valid = false
} else {
b.Byte = byte(nextInt()%60 + 65) // Ascii range
b.Valid = true
}
}