forked from go-steem/rpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
int.go
110 lines (86 loc) · 1.83 KB
/
int.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
108
109
110
package types
import (
// Stdlib
"encoding/json"
"strconv"
// RPC
"github.com/go-steem/rpc/encoding/transaction"
// Vendor
"github.com/pkg/errors"
)
func unmarshalInt(data []byte) (int64, error) {
if len(data) == 0 {
return 0, errors.New("types: empty data received when unmarshalling an integer")
}
var (
i int64
err error
)
if data[0] == '"' {
d := data[1:]
d = d[:len(d)-1]
i, err = strconv.ParseInt(string(d), 10, 64)
} else {
err = json.Unmarshal(data, &i)
}
return i, errors.Wrapf(err, "types: failed to unmarshal integer: %v", data)
}
/*
type Int int
func (num *Int) UnmarshalJSON(data []byte) error {
v, err := unmarshalInt(data)
if err != nil {
return err
}
*num = Int(v)
return nil
}
*/
type Int8 int8
func (num *Int8) UnmarshalJSON(data []byte) error {
v, err := unmarshalInt(data)
if err != nil {
return err
}
*num = Int8(v)
return nil
}
func (num Int8) MarshalTransaction(encoder *transaction.Encoder) error {
return encoder.EncodeNumber(int(num))
}
type Int16 int16
func (num *Int16) UnmarshalJSON(data []byte) error {
v, err := unmarshalInt(data)
if err != nil {
return err
}
*num = Int16(v)
return nil
}
func (num Int16) MarshalTransaction(encoder *transaction.Encoder) error {
return encoder.EncodeNumber(int16(num))
}
type Int32 int32
func (num *Int32) UnmarshalJSON(data []byte) error {
v, err := unmarshalInt(data)
if err != nil {
return err
}
*num = Int32(v)
return nil
}
func (num Int32) MarshalTransaction(encoder *transaction.Encoder) error {
return encoder.EncodeNumber(int32(num))
}
type Int64 int64
func (num *Int64) UnmarshalJSON(data []byte) error {
v, err := unmarshalInt(data)
if err != nil {
return err
}
*num = Int64(v)
return nil
}
func (num Int64) MarshalTransaction(encoder *transaction.Encoder) error {
return encoder.EncodeNumber(int64(num))
}