mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-27 07:06:42 +00:00
fix: direction encoding (#464)
* fix: direction encoding * fix: decoding pagingInfo direction
This commit is contained in:
parent
5747ff5be0
commit
b5f1becace
@ -298,22 +298,10 @@ procSuite "Waku Store":
|
||||
decodedEmptyIndex.isErr == false
|
||||
decodedEmptyIndex.value == emptyIndex
|
||||
|
||||
|
||||
test "PagingDirection Protobuf encod/init test":
|
||||
let
|
||||
pagingDirection = PagingDirection.BACKWARD
|
||||
pb = pagingDirection.encode()
|
||||
decodedPagingDirection = PagingDirection.init(pb.buffer)
|
||||
|
||||
check:
|
||||
# the decodedPagingDirection must be the same as the original pagingDirection
|
||||
decodedPagingDirection.isErr == false
|
||||
decodedPagingDirection.value == pagingDirection
|
||||
|
||||
test "PagingInfo Protobuf encod/init test":
|
||||
let
|
||||
index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: defaultContentTopic))
|
||||
pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD)
|
||||
pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.FORWARD)
|
||||
pb = pagingInfo.encode()
|
||||
decodedPagingInfo = PagingInfo.init(pb.buffer)
|
||||
|
||||
@ -321,6 +309,7 @@ procSuite "Waku Store":
|
||||
# the fields of decodedPagingInfo must be the same as the original pagingInfo
|
||||
decodedPagingInfo.isErr == false
|
||||
decodedPagingInfo.value == pagingInfo
|
||||
decodedPagingInfo.value.direction == pagingInfo.direction
|
||||
|
||||
let
|
||||
emptyPagingInfo = PagingInfo()
|
||||
|
@ -59,16 +59,6 @@ proc encode*(index: Index): ProtoBuffer =
|
||||
result.write(1, index.digest.data)
|
||||
result.write(2, index.receivedTime)
|
||||
|
||||
proc encode*(pd: PagingDirection): ProtoBuffer =
|
||||
## encodes a PagingDirection into a ProtoBuffer
|
||||
## returns the resultant ProtoBuffer
|
||||
|
||||
# intiate a ProtoBuffer
|
||||
result = initProtoBuffer()
|
||||
|
||||
# encodes pd
|
||||
result.write(1, uint32(ord(pd)))
|
||||
|
||||
proc encode*(pinfo: PagingInfo): ProtoBuffer =
|
||||
## encodes a PagingInfo object into a ProtoBuffer
|
||||
## returns the resultant ProtoBuffer
|
||||
@ -79,7 +69,7 @@ proc encode*(pinfo: PagingInfo): ProtoBuffer =
|
||||
# encodes pinfo
|
||||
result.write(1, pinfo.pageSize)
|
||||
result.write(2, pinfo.cursor.encode())
|
||||
result.write(3, pinfo.direction.encode())
|
||||
result.write(3, uint32(ord(pinfo.direction)))
|
||||
|
||||
proc init*(T: type Index, buffer: seq[byte]): ProtoResult[T] =
|
||||
## creates and returns an Index object out of buffer
|
||||
@ -101,16 +91,6 @@ proc init*(T: type Index, buffer: seq[byte]): ProtoResult[T] =
|
||||
|
||||
ok(index)
|
||||
|
||||
proc init*(T: type PagingDirection, buffer: seq[byte]): ProtoResult[T] =
|
||||
## creates and returns a PagingDirection object out of buffer
|
||||
let pb = initProtoBuffer(buffer)
|
||||
|
||||
var dir: uint32
|
||||
discard ? pb.getField(1, dir)
|
||||
var direction = PagingDirection(dir)
|
||||
|
||||
ok(direction)
|
||||
|
||||
proc init*(T: type PagingInfo, buffer: seq[byte]): ProtoResult[T] =
|
||||
## creates and returns a PagingInfo object out of buffer
|
||||
var pagingInfo = PagingInfo()
|
||||
@ -125,9 +105,9 @@ proc init*(T: type PagingInfo, buffer: seq[byte]): ProtoResult[T] =
|
||||
discard ? pb.getField(2, cursorBuffer)
|
||||
pagingInfo.cursor = ? Index.init(cursorBuffer)
|
||||
|
||||
var directionBuffer: seq[byte]
|
||||
discard ? pb.getField(3, directionBuffer)
|
||||
pagingInfo.direction = ? PagingDirection.init(directionBuffer)
|
||||
var direction: uint32
|
||||
discard ? pb.getField(3, direction)
|
||||
pagingInfo.direction = PagingDirection(direction)
|
||||
|
||||
ok(pagingInfo)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user