Skip to content

Commit

Permalink
Extract isSuccess()/isError() helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Mar 19, 2024
1 parent 7c1fe95 commit 1440716
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 21 deletions.
4 changes: 1 addition & 3 deletions src/main/java/com/mojang/datafixers/optics/Affine.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ default <P extends K2> FunctionType<App2<P, A, B>, App2<P, S, T>> eval(final App
return input -> cartesian.dimap(
cocartesian.left(cartesian.rmap(cartesian.<A, B, S>first(input), p -> set(p.getFirst(), p.getSecond()))),
(S s) -> preview(s).map(Either::right, a -> Either.left(Pair.of(a, s))),
(Either<T, T> e) -> {
return e.map(Function.identity(), Function.identity());
}
Either::unwrap
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mojang/serialization/Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ static <T> Codec<T> withAlternative(final Codec<T> primary, final Codec<? extend
primary,
alternative
).xmap(
either -> either.map(v -> v, v -> v),
Either::unwrap,
Either::left
);
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/mojang/serialization/DataResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ default DataResult<R> addLifecycle(final Lifecycle lifecycle) {
return setLifecycle(lifecycle().add(lifecycle));
}

boolean isSuccess();

default boolean isError() {
return !isSuccess();
}

record Success<R>(R value, Lifecycle lifecycle) implements DataResult<R> {
@Override
public Optional<R> result() {
Expand Down Expand Up @@ -223,6 +229,11 @@ public DataResult<R> setLifecycle(final Lifecycle lifecycle) {
return new Success<>(value, lifecycle);
}

@Override
public boolean isSuccess() {
return true;
}

@Override
public String toString() {
return "DataResult.Success[" + value + "]";
Expand Down Expand Up @@ -361,6 +372,11 @@ public Error<R> setLifecycle(final Lifecycle lifecycle) {
return new Error<>(messageSupplier, partialValue, lifecycle);
}

@Override
public boolean isSuccess() {
return false;
}

@Override
public String toString() {
return "DataResult.Error['" + message() + "'" + partialValue.map(value -> ": " + value).orElse("") + "]";
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/mojang/serialization/DynamicOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ default DataResult<Consumer<Consumer<T>>> getList(final T input) {
default DataResult<ByteBuffer> getByteBuffer(final T input) {
return getStream(input).flatMap(stream -> {
final List<T> list = stream.collect(Collectors.toList());
if (list.stream().allMatch(element -> getNumberValue(element).result().isPresent())) {
if (list.stream().allMatch(element -> getNumberValue(element).isSuccess())) {
final ByteBuffer buffer = ByteBuffer.wrap(new byte[list.size()]);
for (int i = 0; i < list.size(); i++) {
buffer.put(i, getNumberValue(list.get(i)).result().get().byteValue());
Expand All @@ -168,9 +168,9 @@ default T createByteList(final ByteBuffer input) {

default DataResult<IntStream> getIntStream(final T input) {
return getStream(input).flatMap(stream -> {
final List<T> list = stream.collect(Collectors.toList());
if (list.stream().allMatch(element -> getNumberValue(element).result().isPresent())) {
return DataResult.success(list.stream().mapToInt(element -> getNumberValue(element).result().get().intValue()));
final List<T> list = stream.toList();
if (list.stream().allMatch(element -> getNumberValue(element).isSuccess())) {
return DataResult.success(list.stream().mapToInt(element -> getNumberValue(element).getOrThrow().intValue()));
}
return DataResult.error(() -> "Some elements are not ints: " + input);
});
Expand All @@ -182,9 +182,9 @@ default T createIntList(final IntStream input) {

default DataResult<LongStream> getLongStream(final T input) {
return getStream(input).flatMap(stream -> {
final List<T> list = stream.collect(Collectors.toList());
if (list.stream().allMatch(element -> getNumberValue(element).result().isPresent())) {
return DataResult.success(list.stream().mapToLong(element -> getNumberValue(element).result().get().longValue()));
final List<T> list = stream.toList();
if (list.stream().allMatch(element -> getNumberValue(element).isSuccess())) {
return DataResult.success(list.stream().mapToLong(element -> getNumberValue(element).getOrThrow().longValue()));
}
return DataResult.error(() -> "Some elements are not longs: " + input);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ default <T> DataResult<Map<K, V>> decode(final DynamicOps<T> ops, final MapLike<
return r.apply2stable((u, p) -> u, DataResult.error(() -> "Duplicate entry for key: '" + entry.get().getFirst() + "'"));
}
}
if (entryResult.error().isPresent()) {
if (entryResult.isError()) {
failed.add(pair);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private <T> DataResult<Unit> parseEntry(final DataResult<Unit> result, final Dyn
return result.apply2stable((u, p) -> u, DataResult.error(() -> "Duplicate entry for key: '" + key + "'"));
}
}
if (entryResult.error().isPresent()) {
if (entryResult.isError()) {
failed.add(input);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public record EitherCodec<F, S>(Codec<F> first, Codec<S> second) implements Code
@Override
public <T> DataResult<Pair<Either<F, S>, T>> decode(final DynamicOps<T> ops, final T input) {
final DataResult<Pair<Either<F, S>, T>> firstRead = first.decode(ops, input).map(vo -> vo.mapFirst(Either::left));
if (firstRead.error().isEmpty()) {
if (firstRead.isSuccess()) {
return firstRead;
}
final DataResult<Pair<Either<F, S>, T>> secondRead = second.decode(ops, input).map(vo -> vo.mapFirst(Either::right));
if (secondRead.error().isEmpty()) {
if (secondRead.isSuccess()) {
return secondRead;
}
return firstRead.apply2((f, s) -> s, secondRead);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public EitherMapCodec(final MapCodec<F> first, final MapCodec<S> second) {
@Override
public <T> DataResult<Either<F, S>> decode(final DynamicOps<T> ops, final MapLike<T> input) {
final DataResult<Either<F, S>> firstRead = first.decode(ops, input).map(Either::left);
if (firstRead.error().isEmpty()) {
if (firstRead.isSuccess()) {
return firstRead;
}
final DataResult<Either<F, S>> secondRead = second.decode(ops, input).map(Either::right);
if (secondRead.error().isEmpty()) {
if (secondRead.isSuccess()) {
return secondRead;
}
return firstRead.apply2((f, s) -> s, secondRead);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public <T> DataResult<V> decode(final DynamicOps<T> ops, final MapLike<T> input)
public <T> RecordBuilder<T> encode(final V input, final DynamicOps<T> ops, final RecordBuilder<T> prefix) {
final DataResult<? extends MapEncoder<V>> encoderResult = encoder.apply(input);
final RecordBuilder<T> builder = prefix.withErrorsFrom(encoderResult);
if (encoderResult.result().isEmpty()) {
if (encoderResult.isError()) {
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public <T> DataResult<Optional<A>> decode(final DynamicOps<T> ops, final MapLike
return DataResult.success(Optional.empty());
}
final DataResult<A> parsed = elementCodec.parse(ops, value);
if (parsed.result().isEmpty() && lenient) {
if (parsed.isError() && lenient) {
return DataResult.success(Optional.empty());
}
return parsed.map(Optional::of).setPartial(parsed.resultOrPartial());
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/mojang/serialization/CodecTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static <T> T fromJavaOrPartial(final Codec<T> codec, final Object value)

private static void assertFromJavaFails(final Codec<?> codec, final Object value) {
final DataResult<?> result = codec.parse(JavaOps.INSTANCE, value);
assertTrue("Expected data result error, but got: " + result.result(), result.result().isEmpty());
assertTrue("Expected data result error, but got: " + result.result(), result.isError());
}

private static void assertFromJavaFailsPartial(final Codec<?> codec, final Object value) {
Expand All @@ -44,7 +44,7 @@ private static void assertFromJavaFailsPartial(final Codec<?> codec, final Objec

private static <T> void assertToJavaFails(final Codec<T> codec, final T value) {
final DataResult<Object> result = codec.encodeStart(JavaOps.INSTANCE, value);
assertTrue("Expected data result error, but got: " + result.result(), result.result().isEmpty());
assertTrue("Expected data result error, but got: " + result.result(), result.isError());
}

private static <T> void assertRoundTrip(final Codec<T> codec, final T value, final Object java) {
Expand Down

0 comments on commit 1440716

Please sign in to comment.