From 28d1ddf7e353a84ffddaa0d05e7aa4b40c54d9e7 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 28 Jan 2021 10:09:21 +0100 Subject: [PATCH] Repo: use Option to signal retrieval success or failure --- ipfs/repo.nim | 9 +++++++-- tests/ipfs/testRepo.nim | 5 ++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ipfs/repo.nim b/ipfs/repo.nim index c82f87c8..eaefaeb2 100644 --- a/ipfs/repo.nim +++ b/ipfs/repo.nim @@ -1,8 +1,10 @@ +import std/options import std/tables import std/hashes import pkg/libp2p import ./ipfsobject +export options export ipfsobject type @@ -18,5 +20,8 @@ proc store*(repo: Repo, obj: IpfsObject) = proc contains*(repo: Repo, id: Cid): bool = repo.storage.hasKey(id) -proc retrieve*(repo: Repo, id: Cid): IpfsObject = - repo.storage[id] +proc retrieve*(repo: Repo, id: Cid): Option[IpfsObject] = + if repo.contains(id): + repo.storage[id].some + else: + IpfsObject.none diff --git a/tests/ipfs/testRepo.nim b/tests/ipfs/testRepo.nim index b54f5cef..a70f2962 100644 --- a/tests/ipfs/testRepo.nim +++ b/tests/ipfs/testRepo.nim @@ -14,7 +14,10 @@ suite "repo": test "retrieves IPFS objects by their content id": 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": check repo.contains(obj.cid) == false