| 1 |
package zero |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"reflect" |
| 8 |
"strconv" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type Int struct { |
| 15 |
sql.NullInt64 |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
func NewInt(i int64, valid bool) Int { |
| 20 |
return Int{ |
| 21 |
NullInt64: sql.NullInt64{ |
| 22 |
Int64: i, |
| 23 |
Valid: valid, |
| 24 |
}, |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
func IntFrom(i int64) Int { |
| 30 |
return NewInt(i, i != 0) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
func IntFromPtr(i *int64) Int { |
| 35 |
if i == nil { |
| 36 |
return NewInt(0, false) |
| 37 |
} |
| 38 |
n := NewInt(*i, true) |
| 39 |
return n |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func (i Int) ValueOrZero() int64 { |
| 44 |
if !i.Valid { |
| 45 |
return 0 |
| 46 |
} |
| 47 |
return i.Int64 |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func (i *Int) UnmarshalJSON(data []byte) error { |
| 55 |
var err error |
| 56 |
var v interface{} |
| 57 |
if err = json.Unmarshal(data, &v); err != nil { |
| 58 |
return err |
| 59 |
} |
| 60 |
switch x := v.(type) { |
| 61 |
case float64: |
| 62 |
|
| 63 |
err = json.Unmarshal(data, &i.Int64) |
| 64 |
case string: |
| 65 |
str := string(x) |
| 66 |
if len(str) == 0 { |
| 67 |
i.Valid = false |
| 68 |
return nil |
| 69 |
} |
| 70 |
i.Int64, err = strconv.ParseInt(str, 10, 64) |
| 71 |
case map[string]interface{}: |
| 72 |
err = json.Unmarshal(data, &i.NullInt64) |
| 73 |
case nil: |
| 74 |
i.Valid = false |
| 75 |
return nil |
| 76 |
default: |
| 77 |
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Int", reflect.TypeOf(v).Name()) |
| 78 |
} |
| 79 |
i.Valid = (err == nil) && (i.Int64 != 0) |
| 80 |
return err |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
func (i *Int) UnmarshalText(text []byte) error { |
| 87 |
str := string(text) |
| 88 |
if str == "" || str == "null" { |
| 89 |
i.Valid = false |
| 90 |
return nil |
| 91 |
} |
| 92 |
var err error |
| 93 |
i.Int64, err = strconv.ParseInt(string(text), 10, 64) |
| 94 |
i.Valid = (err == nil) && (i.Int64 != 0) |
| 95 |
return err |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func (i Int) MarshalJSON() ([]byte, error) { |
| 101 |
n := i.Int64 |
| 102 |
if !i.Valid { |
| 103 |
n = 0 |
| 104 |
} |
| 105 |
return []byte(strconv.FormatInt(n, 10)), nil |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
func (i Int) MarshalText() ([]byte, error) { |
| 111 |
n := i.Int64 |
| 112 |
if !i.Valid { |
| 113 |
n = 0 |
| 114 |
} |
| 115 |
return []byte(strconv.FormatInt(n, 10)), nil |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (i *Int) SetValid(n int64) { |
| 120 |
i.Int64 = n |
| 121 |
i.Valid = true |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func (i Int) Ptr() *int64 { |
| 126 |
if !i.Valid { |
| 127 |
return nil |
| 128 |
} |
| 129 |
return &i.Int64 |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
func (i Int) IsZero() bool { |
| 134 |
return !i.Valid || i.Int64 == 0 |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
func (i Int) Equal(other Int) bool { |
| 139 |
return i.ValueOrZero() == other.ValueOrZero() |
| 140 |
} |