forked from romanyx/nullable
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathint.go
More file actions
54 lines (43 loc) · 1.08 KB
/
int.go
File metadata and controls
54 lines (43 loc) · 1.08 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
package nullable
import (
"bytes"
"encoding/json"
)
// Int represents an int that may be null or not
// present in json at all.
type Int struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid int64
Value int64
}
// UnmarshalJSON implements json.Marshaler interface.
func (i *Int) UnmarshalJSON(data []byte) error {
i.Present = true
if bytes.Equal(data, null) {
return nil
}
if err := json.Unmarshal(data, &i.Value); err != nil {
return err
}
i.Valid = true
return nil
}
// IntSlice represents an int slice that may be null or not
// present in json at all.
type IntSlice struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid []int64
Value []int64
}
// UnmarshalJSON implements json.Marshaler interface.
func (i *IntSlice) UnmarshalJSON(data []byte) error {
i.Present = true
if bytes.Equal(data, null) {
return nil
}
if err := json.Unmarshal(data, &i.Value); err != nil {
return err
}
i.Valid = true
return nil
}