mirror of
https://github.com/status-im/nim-rlp.git
synced 2025-01-23 12:58:54 +00:00
wrap objects and tuples in lists by default
This commit is contained in:
parent
34f1bee50d
commit
0b516cb2b6
10
rlp.nim
10
rlp.nim
@ -279,9 +279,17 @@ proc read*[E](rlp: var Rlp, T: typedesc[seq[E]]): T =
|
||||
for elem in rlp:
|
||||
result.add rlp.read(E)
|
||||
|
||||
proc read*(rlp: var Rlp, T: typedesc[object|tuple]): T =
|
||||
proc read*(rlp: var Rlp, T: typedesc[object|tuple],
|
||||
wrappedInList = wrapObjectsInList): T =
|
||||
mixin enumerateRlpFields, read
|
||||
|
||||
if wrappedInList:
|
||||
var
|
||||
payloadOffset = rlp.payloadOffset()
|
||||
payloadEnd = rlp.position + payloadOffset + rlp.payloadBytesCount()
|
||||
|
||||
rlp.position += payloadOffset
|
||||
|
||||
template op(field) =
|
||||
field = rlp.read(type(field))
|
||||
|
||||
|
@ -25,6 +25,9 @@ type
|
||||
|
||||
Integer* = SomeOrdinal or IntLike or uint or uint64
|
||||
|
||||
const
|
||||
wrapObjectsInList* = true
|
||||
|
||||
proc bytesNeeded(num: Integer): int =
|
||||
var n = num
|
||||
while n != 0:
|
||||
@ -172,8 +175,15 @@ proc append*[T](self; list: seq[T]) =
|
||||
for i in 0 ..< list.len:
|
||||
self.append list[i]
|
||||
|
||||
proc append*(self; data: object|tuple) =
|
||||
proc append*(self; data: object|tuple, wrapInList = wrapObjectsInList) =
|
||||
mixin enumerateRlpFields, append
|
||||
|
||||
if wrapInList:
|
||||
var fieldsCount = 0
|
||||
template countFields(x) = inc fieldsCount
|
||||
enumerateRlpFields(data, countFields)
|
||||
self.startList(fieldsCount)
|
||||
|
||||
template op(x) = append(self, x)
|
||||
enumerateRlpFields(data, op)
|
||||
|
||||
|
@ -30,7 +30,6 @@ test "encoding and decoding an object":
|
||||
f: Foo(x: 5'u64, y: "hocus pocus", z: @[100, 200, 300]))
|
||||
|
||||
var bytes = encode(originalBar)
|
||||
|
||||
var r = rlpFromBytes(bytes)
|
||||
var restoredBar = r.read(Bar)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user