Add lookup/2 to the specific types

They proxy to geef_object:lookup/3 which looks up the object and
asserts the type.
This commit is contained in:
Carlos Martín Nieto
2013-04-22 01:57:53 +02:00
parent 3adfa507c0
commit 54d9e8b63a
4 changed files with 25 additions and 8 deletions

View File

@@ -10,8 +10,7 @@
-spec lookup(repo(), oid() | iolist()) -> object().
lookup(Repo, Id) ->
{ok, Obj = #object{type=blob}} = geef_object:lookup(Repo, Id),
Obj.
geef_object:lookup(Repo, Id, blob).
-spec id(object()) -> oid().
id(Obj = #object{type=blob}) ->

View File

@@ -1,5 +1,5 @@
-module(geef_commit).
-export([tree_id/1, tree/1, id/1]).
-export([tree_id/1, tree/1, id/1, lookup/2]).
-include("geef_records.hrl").
@@ -16,5 +16,9 @@ tree(#object{type=commit,handle=Handle}) ->
Other
end.
-spec lookup(repo(), oid() | iolist()) -> object().
lookup(Repo, Id) ->
geef_object:lookup(Repo, Id, commit).
id(Obj = #object{type=commit}) ->
geef_object:id(Obj).

View File

@@ -6,19 +6,29 @@
-include("geef_records.hrl").
-export([lookup/2, id/1]).
-export([lookup/2, lookup/3, id/1]).
-spec lookup(pid(), oid() | iolist()) -> object().
-spec lookup(pid(), oid() | iolist()) -> {ok, object()} | {error, term()}.
lookup(Repo, #oid{oid=Oid}) ->
case geef_repo:lookup_object(Repo, Oid) of
{ok, Type, Handle} ->
{ok, #object{type=Type, handle=Handle}};
Other ->
Other
{error, Err} ->
{error, Err}
end;
lookup(Repo, Id) ->
lookup(Repo, geef_oid:parse(Id)).
%% As lookup/2, but it asserts that the type is correct
-spec lookup(pid(), oid() | iolist(), atom()) -> {ok, object()} | {error, term()}.
lookup(Repo, Id, Type) ->
case lookup(Repo, Id) of
{ok, Obj = #object{type=Type}} ->
{ok, Obj};
{error, Err} ->
{error, Err}
end.
id(#object{handle=Handle}) ->
case geef:object_id(Handle) of
{ok, Oid} ->

View File

@@ -4,7 +4,7 @@
-include_lib("eunit/include/eunit.hrl").
-endif.
-export([bypath/2, id/1]).
-export([bypath/2, id/1, lookup/2]).
-include("geef_records.hrl").
@@ -19,6 +19,10 @@ bypath(#object{type=tree,handle=Handle}, Path) ->
id(Obj = #object{type=tree}) ->
geef_object:id(Obj).
-spec lookup(repo(), oid() | iolist()) -> {ok, object()} | {error, term()}.
lookup(Repo, Id) ->
geef_object:lookup(Repo, Id, tree).
-ifdef(TEST).
%% This is somewhat hacky, assuming that this is running under .eunit,