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

View file

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