-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtype_uint.go
107 lines (93 loc) · 1.95 KB
/
type_uint.go
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
107
package generic
import (
"database/sql/driver"
"encoding/json"
"strconv"
)
// Uint is generic uint type structure
type Uint struct {
ValidFlag
uint uint64
}
// MarshalUint return generic.Uint converting of request data
func MarshalUint(x interface{}) (Uint, error) {
v := Uint{}
err := v.Scan(x)
return v, err
}
// MustUint return generic.Uint converting of request data
func MustUint(x interface{}) Uint {
v, err := MarshalUint(x)
if err != nil {
panic(err)
}
return v
}
// Value implements the driver Valuer interface.
func (v Uint) Value() (driver.Value, error) {
if !v.Valid() {
return nil, nil
}
return v.uint, nil
}
// Scan implements the sql.Scanner interface.
func (v *Uint) Scan(x interface{}) (err error) {
v.uint, v.ValidFlag, err = asUint(x)
if err != nil {
v.ValidFlag = false
return err
}
return
}
// Weak returns Uint.Uint, but if Uint.ValidFlag is false, returns nil.
func (v Uint) Weak() interface{} {
i, _ := v.Value()
return i
}
// Set sets a specified value.
func (v *Uint) Set(x interface{}) (err error) {
return v.Scan(x)
}
// Uint return uint value
func (v Uint) Uint() uint {
if !v.Valid() {
return 0
}
return uint(v.uint)
}
// Uint32 return uint32 value
func (v Uint) Uint32() uint32 {
if !v.Valid() {
return 0
}
return uint32(v.uint)
}
// Uint64 return uint64 value
func (v Uint) Uint64() uint64 {
if !v.Valid() {
return 0
}
return v.uint
}
// String implements the Stringer interface.
func (v Uint) String() string {
if !v.Valid() {
return ""
}
return strconv.FormatUint(v.uint, 10)
}
// MarshalJSON implements the json.Marshaler interface.
func (v Uint) MarshalJSON() ([]byte, error) {
if !v.Valid() {
return nullBytes, nil
}
return []byte(strconv.FormatUint(v.uint, 10)), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (v *Uint) UnmarshalJSON(data []byte) error {
var in interface{}
if err := json.Unmarshal(data, &in); err != nil {
return err
}
return v.Scan(in)
}