fix: direction encoding (#464)

* fix: direction encoding

* fix: decoding pagingInfo direction
This commit is contained in:
RichΛrd 2021-04-08 23:09:20 -04:00 committed by GitHub
parent 5747ff5be0
commit b5f1becace
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 37 deletions

View File

@ -298,22 +298,10 @@ procSuite "Waku Store":
decodedEmptyIndex.isErr == false decodedEmptyIndex.isErr == false
decodedEmptyIndex.value == emptyIndex 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": test "PagingInfo Protobuf encod/init test":
let let
index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: defaultContentTopic)) 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() pb = pagingInfo.encode()
decodedPagingInfo = PagingInfo.init(pb.buffer) decodedPagingInfo = PagingInfo.init(pb.buffer)
@ -321,6 +309,7 @@ procSuite "Waku Store":
# the fields of decodedPagingInfo must be the same as the original pagingInfo # the fields of decodedPagingInfo must be the same as the original pagingInfo
decodedPagingInfo.isErr == false decodedPagingInfo.isErr == false
decodedPagingInfo.value == pagingInfo decodedPagingInfo.value == pagingInfo
decodedPagingInfo.value.direction == pagingInfo.direction
let let
emptyPagingInfo = PagingInfo() emptyPagingInfo = PagingInfo()

View File

@ -59,16 +59,6 @@ proc encode*(index: Index): ProtoBuffer =
result.write(1, index.digest.data) result.write(1, index.digest.data)
result.write(2, index.receivedTime) 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 = proc encode*(pinfo: PagingInfo): ProtoBuffer =
## encodes a PagingInfo object into a ProtoBuffer ## encodes a PagingInfo object into a ProtoBuffer
## returns the resultant ProtoBuffer ## returns the resultant ProtoBuffer
@ -79,7 +69,7 @@ proc encode*(pinfo: PagingInfo): ProtoBuffer =
# encodes pinfo # encodes pinfo
result.write(1, pinfo.pageSize) result.write(1, pinfo.pageSize)
result.write(2, pinfo.cursor.encode()) 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] = proc init*(T: type Index, buffer: seq[byte]): ProtoResult[T] =
## creates and returns an Index object out of buffer ## 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) 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] = proc init*(T: type PagingInfo, buffer: seq[byte]): ProtoResult[T] =
## creates and returns a PagingInfo object out of buffer ## creates and returns a PagingInfo object out of buffer
var pagingInfo = PagingInfo() var pagingInfo = PagingInfo()
@ -125,9 +105,9 @@ proc init*(T: type PagingInfo, buffer: seq[byte]): ProtoResult[T] =
discard ? pb.getField(2, cursorBuffer) discard ? pb.getField(2, cursorBuffer)
pagingInfo.cursor = ? Index.init(cursorBuffer) pagingInfo.cursor = ? Index.init(cursorBuffer)
var directionBuffer: seq[byte] var direction: uint32
discard ? pb.getField(3, directionBuffer) discard ? pb.getField(3, direction)
pagingInfo.direction = ? PagingDirection.init(directionBuffer) pagingInfo.direction = PagingDirection(direction)
ok(pagingInfo) ok(pagingInfo)