Repo: use Option to signal retrieval success or failure

This commit is contained in:
Mark Spanbroek 2021-01-28 10:09:21 +01:00 committed by markspanbroek
parent 520f3f3bc9
commit 28d1ddf7e3
2 changed files with 11 additions and 3 deletions

View File

@ -1,8 +1,10 @@
import std/options
import std/tables import std/tables
import std/hashes import std/hashes
import pkg/libp2p import pkg/libp2p
import ./ipfsobject import ./ipfsobject
export options
export ipfsobject export ipfsobject
type type
@ -18,5 +20,8 @@ proc store*(repo: Repo, obj: IpfsObject) =
proc contains*(repo: Repo, id: Cid): bool = proc contains*(repo: Repo, id: Cid): bool =
repo.storage.hasKey(id) repo.storage.hasKey(id)
proc retrieve*(repo: Repo, id: Cid): IpfsObject = proc retrieve*(repo: Repo, id: Cid): Option[IpfsObject] =
repo.storage[id] if repo.contains(id):
repo.storage[id].some
else:
IpfsObject.none

View File

@ -14,7 +14,10 @@ suite "repo":
test "retrieves IPFS objects by their content id": test "retrieves IPFS objects by their content id":
repo.store(obj) repo.store(obj)
check repo.retrieve(obj.cid) == obj check repo.retrieve(obj.cid).get() == obj
test "signals retrieval failure":
check repo.retrieve(obj.cid).isNone
test "knows which content ids are stored": test "knows which content ids are stored":
check repo.contains(obj.cid) == false check repo.contains(obj.cid) == false