-
Notifications
You must be signed in to change notification settings - Fork 17
/
basic_types.go
64 lines (56 loc) · 1.14 KB
/
basic_types.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
package tl
import (
"database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"
"io"
)
type Int256 [32]byte
// Scan implements Scanner for database/sql.
func (i *Int256) Scan(src interface{}) error {
srcB, ok := src.([]byte)
if !ok {
return fmt.Errorf("can't scan %T into Hash", src)
}
if len(srcB) != 32 {
return fmt.Errorf("can't scan []byte of len %d into Hash, want %d", len(srcB), 32)
}
copy(i[:], srcB)
return nil
}
// Value implements valuer for database/sql.
func (i Int256) Value() (driver.Value, error) {
return i[:], nil
}
func (i Int256) MarshalTL() ([]byte, error) {
return i[:], nil
}
func (i *Int256) UnmarshalTL(r io.Reader) error {
var b [32]byte
_, err := io.ReadFull(r, b[:])
if err != nil {
return err
}
*i = b
return nil
}
func (i Int256) MarshalJSON() ([]byte, error) {
return json.Marshal(hex.EncodeToString(i[:]))
}
func (i *Int256) UnmarshalJSON(data []byte) error {
var h string
err := json.Unmarshal(data, &h)
if err != nil {
return err
}
b, err := hex.DecodeString(h)
if err != nil {
return err
}
if len(b) != len(i) {
return fmt.Errorf("invalid int256 len")
}
copy(i[:], b)
return nil
}