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/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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue