-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuint64.go
More file actions
65 lines (55 loc) · 1.08 KB
/
uint64.go
File metadata and controls
65 lines (55 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
55
56
57
58
59
60
61
62
63
64
65
package cid
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
)
var ErrScanUint64 = errors.New("Incompatible type")
type Uint64 uint64
func (id Uint64) String() string {
return fmt.Sprintf("%d", uint64(id))
}
func (id Uint64) MarshalJSON() ([]byte, error) {
return json.Marshal(Uint64ToString(Uint64Hash(uint64(id))))
}
func (id *Uint64) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
n, err := StringToUint64(s)
if err != nil {
return err
}
*id = Uint64(Uint64Unhash(n))
return nil
}
func (id Uint64) Value() (driver.Value, error) {
i := uint64(id)
if i <= 0 {
return nil, nil
}
return i, nil
}
func (id *Uint64) Scan(value interface{}) error {
if value == nil {
*id = Uint64(0)
return nil
}
switch v := value.(type) {
case uint64:
*id = Uint64(v)
default:
return ErrScanUint64
}
return nil
}
func EncodeUint64(n uint64) string {
return Uint64ToString(Uint64Hash(n))
}
func DecodeUint64(s string) (uint64, error) {
n, err := StringToUint64(s)
return Uint64Unhash(n), err
}