@@ -2,6 +2,7 @@ package bindnode_test
2
2
3
3
import (
4
4
"encoding/hex"
5
+ "math"
5
6
"testing"
6
7
7
8
qt "github.com/frankban/quicktest"
@@ -197,3 +198,77 @@ func TestSubNodeWalkAndUnwrap(t *testing.T) {
197
198
verifyMap (node )
198
199
})
199
200
}
201
+
202
+ func TestUint64Struct (t * testing.T ) {
203
+ t .Run ("in struct" , func (t * testing.T ) {
204
+ type IntHolder struct {
205
+ Int32 int32
206
+ Int64 int64
207
+ Uint64 uint64
208
+ }
209
+ schema := `
210
+ type IntHolder struct {
211
+ Int32 Int
212
+ Int64 Int
213
+ Uint64 Int
214
+ }
215
+ `
216
+
217
+ maxExpectedHex := "a365496e7433321a7fffffff65496e7436341b7fffffffffffffff6655696e7436341bffffffffffffffff"
218
+ maxExpected , err := hex .DecodeString (maxExpectedHex )
219
+ qt .Assert (t , err , qt .IsNil )
220
+
221
+ typeSystem , err := ipld .LoadSchemaBytes ([]byte (schema ))
222
+ qt .Assert (t , err , qt .IsNil )
223
+ schemaType := typeSystem .TypeByName ("IntHolder" )
224
+ proto := bindnode .Prototype (& IntHolder {}, schemaType )
225
+
226
+ node , err := ipld .DecodeUsingPrototype ([]byte (maxExpected ), dagcbor .Decode , proto )
227
+ qt .Assert (t , err , qt .IsNil )
228
+
229
+ typ := bindnode .Unwrap (node )
230
+ inst , ok := typ .(* IntHolder )
231
+ qt .Assert (t , ok , qt .IsTrue )
232
+
233
+ qt .Assert (t , * inst , qt .DeepEquals , IntHolder {
234
+ Int32 : math .MaxInt32 ,
235
+ Int64 : math .MaxInt64 ,
236
+ Uint64 : math .MaxUint64 ,
237
+ })
238
+
239
+ node = bindnode .Wrap (inst , schemaType ).Representation ()
240
+ byt , err := ipld .Encode (node , dagcbor .Encode )
241
+ qt .Assert (t , err , qt .IsNil )
242
+
243
+ qt .Assert (t , hex .EncodeToString (byt ), qt .Equals , maxExpectedHex )
244
+ })
245
+
246
+ t .Run ("plain" , func (t * testing.T ) {
247
+ type IntHolder uint64
248
+ schema := `type IntHolder int`
249
+
250
+ maxExpectedHex := "1bffffffffffffffff"
251
+ maxExpected , err := hex .DecodeString (maxExpectedHex )
252
+ qt .Assert (t , err , qt .IsNil )
253
+
254
+ typeSystem , err := ipld .LoadSchemaBytes ([]byte (schema ))
255
+ qt .Assert (t , err , qt .IsNil )
256
+ schemaType := typeSystem .TypeByName ("IntHolder" )
257
+ proto := bindnode .Prototype ((* IntHolder )(nil ), schemaType )
258
+
259
+ node , err := ipld .DecodeUsingPrototype ([]byte (maxExpected ), dagcbor .Decode , proto )
260
+ qt .Assert (t , err , qt .IsNil )
261
+
262
+ typ := bindnode .Unwrap (node )
263
+ inst , ok := typ .(* IntHolder )
264
+ qt .Assert (t , ok , qt .IsTrue )
265
+
266
+ qt .Assert (t , * inst , qt .Equals , IntHolder (math .MaxUint64 ))
267
+
268
+ node = bindnode .Wrap (inst , schemaType ).Representation ()
269
+ byt , err := ipld .Encode (node , dagcbor .Encode )
270
+ qt .Assert (t , err , qt .IsNil )
271
+
272
+ qt .Assert (t , hex .EncodeToString (byt ), qt .Equals , maxExpectedHex )
273
+ })
274
+ }
0 commit comments