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