-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumber.go
74 lines (58 loc) · 1.28 KB
/
number.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
package jsons
import (
"database/sql/driver"
"encoding/json"
"errors"
"strconv"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func (n Number) Value() (driver.Value, error) {
return json.Marshal(n)
}
func (n *Number) Scan(v interface{}) error {
if v == nil {
*n = ""
return nil
}
switch val := v.(type) {
case []byte:
return json.Unmarshal(val, n)
case string:
return json.Unmarshal([]byte(val), n)
}
return errors.New("invalid scan number source")
}
func (Number) GormDBDataType(*gorm.DB, *schema.Field) string {
return "json"
}
func (n Number) MarshalJSON() ([]byte, error) {
return json.Marshal(json.Number(n))
}
func (n *Number) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, (*json.Number)(n))
}
func (n Number) Raw() Raw {
return n.JSONValue().Raw()
}
func (n Number) Float64() (float64, error) {
return json.Number(n).Float64()
}
func (n Number) Int64() (int64, error) {
return json.Number(n).Int64()
}
func (n Number) Uint64() (uint64, error) {
return strconv.ParseUint(string(n), 10, 64)
}
func (n Number) String() string {
return json.Number(n).String()
}
func (n Number) JSON() []byte {
return n.JSONValue().JSON()
}
func (n Number) JSONValue() Value {
return value(n)
}
func (n Number) JSONString() string {
return n.JSONValue().JSONString()
}