NamedRevBlob returns Result<T> values

This commit is contained in:
Paul Campbell 2018-06-26 23:06:03 +01:00
parent 2de83ba3bc
commit 98b81a44dc
2 changed files with 21 additions and 12 deletions

View file

@ -21,7 +21,6 @@
package net.kemitix.gitdb.impl;
import lombok.val;
import net.kemitix.mon.maybe.Maybe;
import net.kemitix.mon.result.Result;
import org.eclipse.jgit.lib.ObjectId;
@ -30,6 +29,7 @@ import org.eclipse.jgit.lib.Repository;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
/**
* Wrapper for interacting with the GitDB Repository.
@ -85,15 +85,17 @@ class GitDBRepo {
final Ref branchRef,
final String key
) {
try {
val blob = new GitTreeReader(repository)
.treeFilter(key)
.stream(branchRef)
.findFirst();
if (blob.isPresent()) {
return Result.ok(Maybe.just(blob.get().blobAsString()));
final GitTreeReader treeFilter = new GitTreeReader(repository).treeFilter(key);
return streamTree(branchRef, treeFilter).flatMap(s ->
Result.invert(s.findFirst()
.map(NamedRevBlob::blobAsString)
.map(Maybe::just)
.orElseGet(Maybe::nothing)));
}
return Result.ok(Maybe.nothing());
private Result<Stream<NamedRevBlob>> streamTree(final Ref branchRef, final GitTreeReader treeFilter) {
try {
return Result.ok(treeFilter.stream(branchRef));
} catch (IOException e) {
return Result.error(e);
}

View file

@ -24,7 +24,9 @@ package net.kemitix.gitdb.impl;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.kemitix.mon.result.Result;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevBlob;
@ -48,10 +50,15 @@ class NamedRevBlob {
* Converts the blob to a String.
*
* @return a string
* @throws IOException of there was an error reading the blob
*/
String blobAsString() throws IOException {
return new String(repository.open(revBlob.getId(), Constants.OBJ_BLOB).getBytes());
Result<String> blobAsString() {
try {
return Result.ok(repository.open(revBlob.getId(), Constants.OBJ_BLOB))
.map(ObjectLoader::getBytes)
.map(String::new);
} catch (IOException e) {
return Result.error(e);
}
}
}