Skip to content

Commit

Permalink
chore: minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiachun committed Feb 2, 2024
1 parent ced4c5d commit ff3b58a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc

Result<WriteOk, Err> result = puts.get();

if (result.isOk()) {
LOG.info("Write result: {}", result.getOk());
Result<Integer, String> simpleResult = result //
.map(WriteOk::getSuccess) //
.mapErr(err -> err.getError().getMessage());
if (simpleResult.isOk()) {
LOG.info("Write success: {}", simpleResult.getOk());
} else {
LOG.error("Failed to write: {}", result.getErr());
LOG.error("Failed to write: {}", simpleResult.getErr());
}

List<List<?>> delete_pojos = Arrays.asList(cpus.subList(0, 5), memories.subList(0, 5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc

Result<WriteOk, Err> result = future.get();

if (result.isOk()) {
LOG.info("Write result: {}", result.getOk());
Result<Integer, String> simpleResult = result //
.map(WriteOk::getSuccess) //
.mapErr(err -> err.getError().getMessage());
if (simpleResult.isOk()) {
LOG.info("Write success: {}", simpleResult.getOk());
} else {
LOG.error("Failed to write: {}", result.getErr());
LOG.error("Failed to write: {}", simpleResult.getErr());
}

List<Table> delete_objs = Arrays.asList(cpuMetric.subRange(0, 5), memMetric.subRange(0, 5));
Expand Down
64 changes: 32 additions & 32 deletions ingester-protocol/src/test/java/io/greptime/models/ResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,60 @@ public class ResultTest {

@Test
public void testMap() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
final Result<Integer, Err> r2 = r1.map(WriteOk::getSuccess);
Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
Result<Integer, Err> r2 = r1.map(WriteOk::getSuccess);
Assert.assertEquals(2, r2.getOk().intValue());

final Result<WriteOk, Err> r5 = Result.err(Err.writeErr(400, null, null));
final Result<Integer, Err> r6 = r5.map(WriteOk::getSuccess);
Result<WriteOk, Err> r5 = Result.err(Err.writeErr(400, null, null));
Result<Integer, Err> r6 = r5.map(WriteOk::getSuccess);
Assert.assertFalse(r6.isOk());
}

@Test
public void testMapOr() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
final Integer r2 = r1.mapOr(-1, WriteOk::getSuccess);
Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
Integer r2 = r1.mapOr(-1, WriteOk::getSuccess);
Assert.assertEquals(2, r2.intValue());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
final Integer r4 = r3.mapOr(-1, WriteOk::getSuccess);
Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
Integer r4 = r3.mapOr(-1, WriteOk::getSuccess);
Assert.assertEquals(-1, r4.intValue());
}

@Test
public void testMapOrElse() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
final Integer r2 = r1.mapOrElse(err -> -1, WriteOk::getSuccess);
Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
Integer r2 = r1.mapOrElse(err -> -1, WriteOk::getSuccess);
Assert.assertEquals(2, r2.intValue());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
final Integer r4 = r3.mapOrElse(err -> -1, WriteOk::getSuccess);
Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
Integer r4 = r3.mapOrElse(err -> -1, WriteOk::getSuccess);
Assert.assertEquals(-1, r4.intValue());
}

@Test
public void testMapErr() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
final Result<WriteOk, Throwable> r2 = r1.mapErr(Err::getError);
Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
Result<WriteOk, Throwable> r2 = r1.mapErr(Err::getError);
Assert.assertEquals(2, r2.getOk().getSuccess());

IllegalStateException error = new IllegalStateException("error test");
final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, error, null));
final Result<WriteOk, Throwable> r4 = r3.mapErr(Err::getError);
Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, error, null));
Result<WriteOk, Throwable> r4 = r3.mapErr(Err::getError);
Assert.assertEquals("error test", r4.getErr().getMessage());
}

@Test
public void testAndThen() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
final Result<WriteOk, Err> r2 = r1.andThen(writeOk -> {
Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
Result<WriteOk, Err> r2 = r1.andThen(writeOk -> {
WriteOk newOne = WriteOk.ok(writeOk.getSuccess() + 1, 0);
return newOne.mapToResult();
});
Assert.assertEquals(3, r2.getOk().getSuccess());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
final Result<WriteOk, Err> r4 = r3.andThen(writeOk -> {
Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
Result<WriteOk, Err> r4 = r3.andThen(writeOk -> {
WriteOk newOne = WriteOk.ok(writeOk.getSuccess() + 1, 0);
return newOne.mapToResult();
});
Expand All @@ -87,34 +87,34 @@ public void testAndThen() {

@Test
public void testOrElse() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
final Result<WriteOk, String> r2 = r1.orElse(err -> Result.ok(WriteOk.ok(0, 0)));
Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
Result<WriteOk, String> r2 = r1.orElse(err -> Result.ok(WriteOk.ok(0, 0)));
Assert.assertEquals(2, r2.getOk().getSuccess());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
final Result<WriteOk, String> r4 = r3.orElse(err -> Result.ok(WriteOk.ok(0, 0)));
Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
Result<WriteOk, String> r4 = r3.orElse(err -> Result.ok(WriteOk.ok(0, 0)));
Assert.assertEquals(0, r4.getOk().getSuccess());
}

@Test
public void testUnwrapOr() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
final WriteOk r2 = r1.unwrapOr(WriteOk.emptyOk());
Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
WriteOk r2 = r1.unwrapOr(WriteOk.emptyOk());
Assert.assertEquals(2, r2.getSuccess());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
final WriteOk r4 = r3.unwrapOr(WriteOk.emptyOk());
Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
WriteOk r4 = r3.unwrapOr(WriteOk.emptyOk());
Assert.assertEquals(0, r4.getSuccess());
}

@Test
public void testUnwrapOrElse() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
final WriteOk r2 = r1.unwrapOrElse(err -> WriteOk.emptyOk());
Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0));
WriteOk r2 = r1.unwrapOrElse(err -> WriteOk.emptyOk());
Assert.assertEquals(2, r2.getSuccess());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
final WriteOk r4 = r3.unwrapOrElse(err -> WriteOk.emptyOk());
Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null));
WriteOk r4 = r3.unwrapOrElse(err -> WriteOk.emptyOk());
Assert.assertEquals(0, r4.getSuccess());
}
}

0 comments on commit ff3b58a

Please sign in to comment.