Skip to content

Commit b636575

Browse files
committed
feat(bindnode): support full uint64 range
1 parent 89855cd commit b636575

File tree

4 files changed

+304
-44
lines changed

4 files changed

+304
-44
lines changed

node/bindnode/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func Wrap(ptrVal interface{}, schemaType schema.Type, options ...Option) schema.
301301
// inferred.
302302
verifyCompatibility(cfg, make(map[seenEntry]bool), goVal.Type(), schemaType)
303303
}
304-
return &_node{cfg: cfg, val: goVal, schemaType: schemaType}
304+
return newNode(cfg, schemaType, goVal)
305305
}
306306

307307
// TODO: consider making our own Node interface, like:

node/bindnode/api_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bindnode_test
22

33
import (
44
"encoding/hex"
5+
"math"
56
"testing"
67

78
qt "github.com/frankban/quicktest"
@@ -197,3 +198,77 @@ func TestSubNodeWalkAndUnwrap(t *testing.T) {
197198
verifyMap(node)
198199
})
199200
}
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

Comments
 (0)