-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyte.go
More file actions
106 lines (93 loc) · 2.1 KB
/
byte.go
File metadata and controls
106 lines (93 loc) · 2.1 KB
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
package nullable
// Do not modify. Generated by nullable-generate.
import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/json"
)
// Byte represents a byte value that may be null.
// This type implements the Scanner interface so it
// can be used as a scan destination, similar to NullString.
// It also implements the necessary interfaces to serialize
// to and from JSON.
type Byte struct {
Byte byte
Valid bool
}
// ByteFromPtr returns a Byte whose value matches ptr.
func ByteFromPtr(ptr *byte) Byte {
var v Byte
return v.Assign(ptr)
}
// Assign the value of the pointer. If the pointer is nil,
// then then Valid is false, otherwise Valid is true.
func (n *Byte) Assign(ptr *byte) Byte {
if ptr == nil {
n.Valid = false
n.Byte = 0
} else {
n.Valid = true
n.Byte = *ptr
}
return *n
}
// Ptr returns a pointer to byte. If Valid is false
// then the pointer is nil, otherwise it is non-nil.
func (n Byte) Ptr() *byte {
if n.Valid {
v := n.Byte
return &v
}
return nil
}
// Normalized returns a Byte that can be compared with
// another Byte for equality.
func (n Byte) Normalized() Byte {
if n.Valid {
return n
}
// If !Valid, then Byte could be any value.
// Normalized value can be compared for equality.
return Byte{}
}
// Scan implements the sql.Scanner interface.
func (n *Byte) Scan(value interface{}) error {
var nt sql.NullInt64
err := nt.Scan(value)
if err != nil {
return err
}
n.Valid = nt.Valid
n.Byte = byte(nt.Int64)
return nil
}
// Value implements the driver.Valuer interface.
func (n Byte) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return int64(n.Byte), nil
}
// MarshalJSON implements the json.Marshaler interface.
func (n Byte) MarshalJSON() ([]byte, error) {
if n.Valid {
return json.Marshal(n.Byte)
}
return []byte("null"), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (n *Byte) UnmarshalJSON(p []byte) error {
if bytes.Equal(p, jsonNull) {
n.Byte = 0
n.Valid = false
return nil
}
var v byte
if err := json.Unmarshal(p, &v); err != nil {
return err
}
n.Byte = v
n.Valid = true
return nil
}