Repo: use Option to signal retrieval success or failure
This commit is contained in:
parent
520f3f3bc9
commit
28d1ddf7e3
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue