-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint16.go
More file actions
75 lines (63 loc) · 1.63 KB
/
Copy pathint16.go
File metadata and controls
75 lines (63 loc) · 1.63 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
package opt
import (
"strconv"
"github.com/coregx/opt/internal"
)
// Int16 is a nullable int16 with optimized JSON marshaling.
type Int16 struct {
Option[int16]
}
// NewInt16 creates an Int16 with the given value and validity.
func NewInt16(i int16, valid bool) Int16 {
return Int16{New(i, valid)}
}
// Int16From creates an Int16 that is always valid.
func Int16From(i int16) Int16 {
return Int16{From(i)}
}
// Int16FromPtr creates an Int16 from a pointer. Nil results in null.
func Int16FromPtr(i *int16) Int16 {
return Int16{FromPtr(i)}
}
// Int16OrNull creates a valid Int16 if n is non-zero, null otherwise.
func Int16OrNull(n int16) Int16 {
return NewInt16(n, n != 0)
}
// Equal reports whether two Int16s are equal.
func (i Int16) Equal(other Int16) bool {
return Equal(i.Option, other.Option)
}
// MarshalJSON implements json.Marshaler.
func (i Int16) MarshalJSON() ([]byte, error) {
if !i.Valid {
return jsonNull, nil
}
return []byte(strconv.FormatInt(int64(i.V), 10)), nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (i *Int16) UnmarshalJSON(data []byte) error {
n, valid, err := internal.UnmarshalIntJSON(data, 16)
if err != nil {
return err
}
i.V = int16(n)
i.Valid = valid
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (i Int16) MarshalText() ([]byte, error) {
if !i.Valid {
return []byte{}, nil
}
return []byte(strconv.FormatInt(int64(i.V), 10)), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Int16) UnmarshalText(text []byte) error {
n, valid, err := internal.UnmarshalIntText(text, 16)
if err != nil {
return err
}
i.V = int16(n)
i.Valid = valid
return nil
}