-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.go
137 lines (112 loc) · 2.29 KB
/
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package tezgen
import (
"encoding/hex"
"math/big"
"strconv"
"time"
jsoniter "github.com/json-iterator/go"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// Unit -
type Unit struct{}
// Bytes -
type Bytes []byte
// UnmarshalJSON -
func (b *Bytes) UnmarshalJSON(data []byte) error {
var x string
if err := json.Unmarshal(data, &x); err != nil {
return err
}
str, err := hex.DecodeString(x)
if err != nil {
return err
}
*b = Bytes(str)
return nil
}
// MarshalJSON -
func (b Bytes) MarshalJSON() ([]byte, error) {
str := hex.EncodeToString(b)
return []byte(strconv.Quote(str)), nil
}
// Timestamp -
type Timestamp struct {
val time.Time
}
// NewTimestamp -
func NewTimestamp(t time.Time) Timestamp {
return Timestamp{t}
}
// Value -
func (t Timestamp) Value() time.Time {
return t.val
}
// UnmarshalJSON -
func (t *Timestamp) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
ts, err := strconv.ParseInt(str, 10, 64)
if err == nil {
t.val = time.Unix(ts, 0)
return nil
}
t.val, err = time.Parse(time.RFC3339, str)
return err
}
// MarshalJSON -
func (t Timestamp) MarshalJSON() ([]byte, error) {
str := strconv.FormatInt(t.val.Unix(), 10)
return []byte(str), nil
}
// Address -
type Address string
// Contract -
type Contract string
// SaplingTransaction -
type SaplingTransaction string
// SaplingState -
type SaplingState struct {
State int64
Array []interface{}
}
// UnmarshalJSON -
func (ss *SaplingState) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &ss.State); err != nil {
return err
}
return json.Unmarshal(data, &ss.Array)
}
// MarshalJSON -
func (ss SaplingState) MarshalJSON() ([]byte, error) {
str := strconv.FormatInt(ss.State, 10)
return []byte(str), nil
}
// Int -
type Int struct{ *big.Int }
// NewInt -
func NewInt(val int64) Int {
return Int{
big.NewInt(val),
}
}
// UnmarshalJSON -
func (i *Int) UnmarshalJSON(data []byte) error {
if i.Int == nil {
i.Int = big.NewInt(0)
}
if len(data) > 2 {
if data[0] == '"' && data[len(data)-1] == '"' {
data = data[1 : len(data)-1]
}
}
return i.Int.UnmarshalJSON(data)
}
// MarshalJSON -
func (i Int) MarshalJSON() ([]byte, error) {
if i.Int == nil {
i.Int = big.NewInt(0)
}
return i.Int.MarshalJSON()
}