forked from centrifuge/go-substrate-rpc-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
option_uint.go
228 lines (185 loc) · 5.73 KB
/
option_uint.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"math/big"
"github.com/Cerebellum-Network/go-substrate-rpc-client/v8/scale"
)
// OptionU8 is a structure that can store a U8 or a missing value
type OptionU8 struct {
option
value U8
}
// NewOptionU8 creates an OptionU8 with a value
func NewOptionU8(value U8) OptionU8 {
return OptionU8{option{true}, value}
}
// NewOptionU8Empty creates an OptionU8 without a value
func NewOptionU8Empty() OptionU8 {
return OptionU8{option: option{false}}
}
func (o OptionU8) Encode(encoder scale.Encoder) error {
return encoder.EncodeOption(o.hasValue, o.value)
}
func (o *OptionU8) Decode(decoder scale.Decoder) error {
return decoder.DecodeOption(&o.hasValue, &o.value)
}
// SetSome sets a value
func (o *OptionU8) SetSome(value U8) {
o.hasValue = true
o.value = value
}
// SetNone removes a value and marks it as missing
func (o *OptionU8) SetNone() {
o.hasValue = false
o.value = U8(0)
}
// Unwrap returns a flag that indicates whether a value is present and the stored value
func (o OptionU8) Unwrap() (ok bool, value U8) {
return o.hasValue, o.value
}
// OptionU16 is a structure that can store a U16 or a missing value
type OptionU16 struct {
option
value U16
}
// NewOptionU16 creates an OptionU16 with a value
func NewOptionU16(value U16) OptionU16 {
return OptionU16{option{true}, value}
}
// NewOptionU16Empty creates an OptionU16 without a value
func NewOptionU16Empty() OptionU16 {
return OptionU16{option: option{false}}
}
func (o OptionU16) Encode(encoder scale.Encoder) error {
return encoder.EncodeOption(o.hasValue, o.value)
}
func (o *OptionU16) Decode(decoder scale.Decoder) error {
return decoder.DecodeOption(&o.hasValue, &o.value)
}
// SetSome sets a value
func (o *OptionU16) SetSome(value U16) {
o.hasValue = true
o.value = value
}
// SetNone removes a value and marks it as missing
func (o *OptionU16) SetNone() {
o.hasValue = false
o.value = U16(0)
}
// Unwrap returns a flag that indicates whether a value is present and the stored value
func (o OptionU16) Unwrap() (ok bool, value U16) {
return o.hasValue, o.value
}
// OptionU32 is a structure that can store a U32 or a missing value
type OptionU32 struct {
option
value U32
}
// NewOptionU32 creates an OptionU32 with a value
func NewOptionU32(value U32) OptionU32 {
return OptionU32{option{true}, value}
}
// NewOptionU32Empty creates an OptionU32 without a value
func NewOptionU32Empty() OptionU32 {
return OptionU32{option: option{false}}
}
func (o OptionU32) Encode(encoder scale.Encoder) error {
return encoder.EncodeOption(o.hasValue, o.value)
}
func (o *OptionU32) Decode(decoder scale.Decoder) error {
return decoder.DecodeOption(&o.hasValue, &o.value)
}
// SetSome sets a value
func (o *OptionU32) SetSome(value U32) {
o.hasValue = true
o.value = value
}
// SetNone removes a value and marks it as missing
func (o *OptionU32) SetNone() {
o.hasValue = false
o.value = U32(0)
}
// Unwrap returns a flag that indicates whether a value is present and the stored value
func (o OptionU32) Unwrap() (ok bool, value U32) {
return o.hasValue, o.value
}
// OptionU64 is a structure that can store a U64 or a missing value
type OptionU64 struct {
option
value U64
}
// NewOptionU64 creates an OptionU64 with a value
func NewOptionU64(value U64) OptionU64 {
return OptionU64{option{true}, value}
}
// NewOptionU64Empty creates an OptionU64 without a value
func NewOptionU64Empty() OptionU64 {
return OptionU64{option: option{false}}
}
func (o OptionU64) Encode(encoder scale.Encoder) error {
return encoder.EncodeOption(o.hasValue, o.value)
}
func (o *OptionU64) Decode(decoder scale.Decoder) error {
return decoder.DecodeOption(&o.hasValue, &o.value)
}
// SetSome sets a value
func (o *OptionU64) SetSome(value U64) {
o.hasValue = true
o.value = value
}
// SetNone removes a value and marks it as missing
func (o *OptionU64) SetNone() {
o.hasValue = false
o.value = U64(0)
}
// Unwrap returns a flag that indicates whether a value is present and the stored value
func (o OptionU64) Unwrap() (ok bool, value U64) {
return o.hasValue, o.value
}
// OptionU128 is a structure that can store a U128 or a missing value
type OptionU128 struct {
option
value U128
}
// NewOptionU128 creates an OptionU128 with a value
func NewOptionU128(value U128) OptionU128 {
return OptionU128{option{true}, value}
}
// NewOptionU128Empty creates an OptionU128 without a value
func NewOptionU128Empty() OptionU128 {
return OptionU128{option: option{false}}
}
func (o OptionU128) Encode(encoder scale.Encoder) error {
return encoder.EncodeOption(o.hasValue, o.value)
}
func (o *OptionU128) Decode(decoder scale.Decoder) error {
return decoder.DecodeOption(&o.hasValue, &o.value)
}
// SetSome sets a value
func (o *OptionU128) SetSome(value U128) {
o.hasValue = true
o.value = value
}
// SetNone removes a value and marks it as missing
func (o *OptionU128) SetNone() {
o.hasValue = false
o.value = NewU128(*big.NewInt(0))
}
// Unwrap returns a flag that indicates whether a value is present and the stored value
func (o OptionU128) Unwrap() (ok bool, value U128) {
return o.hasValue, o.value
}