Skip to content

Commit

Permalink
fix regresion, Record is not serializable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
tonivade committed Feb 16, 2024
1 parent 898533e commit dd144f3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import static com.github.tonivade.purefun.core.Precondition.checkNonNull;

import java.io.Serializable;

import com.github.tonivade.purefun.HigherKind;

@HigherKind
public record Const<T, A>(T value) implements ConstOf<T, A> {
public record Const<T, A>(T value) implements ConstOf<T, A>, Serializable {

public Const {
checkNonNull(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static com.github.tonivade.purefun.core.Function1.identity;
import static com.github.tonivade.purefun.core.Precondition.checkNonNull;

import java.io.Serializable;
import java.util.NoSuchElementException;
import java.util.stream.Stream;

Expand Down Expand Up @@ -200,7 +201,7 @@ static <L, A, B, Z> Either<L, Z> map2(
return eitherA.flatMap(a -> eitherB.map(b -> mapper.apply(a, b)));
}

record Left<L, R>(L value) implements Either<L, R> {
record Left<L, R>(L value) implements Either<L, R>, Serializable {

public Left {
checkNonNull(value);
Expand Down Expand Up @@ -232,7 +233,7 @@ public String toString() {
}
}

record Right<L, R>(R value) implements Either<L, R> {
record Right<L, R>(R value) implements Either<L, R>, Serializable {

public Right {
checkNonNull(value);
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/com/github/tonivade/purefun/type/Id.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import static com.github.tonivade.purefun.core.Precondition.checkNonNull;

import java.io.Serializable;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.core.Bindable;
Expand All @@ -18,7 +20,7 @@
* @param <T> the wrapped value
*/
@HigherKind
public record Id<T>(T value) implements IdOf<T>, Bindable<Id_, T> {
public record Id<T>(T value) implements IdOf<T>, Bindable<Id_, T>, Serializable {

public Id {
checkNonNull(value);
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/com/github/tonivade/purefun/type/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static com.github.tonivade.purefun.core.Function1.identity;
import static com.github.tonivade.purefun.core.Precondition.checkNonNull;

import java.io.Serializable;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Objects;
Expand Down Expand Up @@ -239,7 +240,7 @@ default <E> Validation<E, T> toValidation(Function1<? super Throwable, ? extends
return fold(map.andThen(Validation::invalid), Validation::valid);
}

record Success<T>(T value) implements Try<T> {
record Success<T>(T value) implements Try<T>, Serializable {

public Success {
checkNonNull(value);
Expand Down Expand Up @@ -271,7 +272,7 @@ public String toString() {
}
}

record Failure(Throwable cause) implements Try<Nothing>, Recoverable {
record Failure(Throwable cause) implements Try<Nothing>, Recoverable, Serializable {

private static final Equal<Failure> EQUAL =
Equal.<Failure>of().comparing(Failure::getMessage).comparingArray(Failure::getStackTrace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static Validation<String, Integer> requireLowerThanOrEqual(Integer value, int x)
return nonNullAnd(lowerThanOrEqual(x, () -> "require " + value + " <= " + x)).validate(value);
}

record Valid<E, T>(T value) implements Validation<E, T> {
record Valid<E, T>(T value) implements Validation<E, T>, Serializable {

public Valid {
checkNonNull(value);
Expand Down Expand Up @@ -294,7 +294,7 @@ public String toString() {
}
}

record Invalid<E, T>(E error) implements Validation<E, T> {
record Invalid<E, T>(E error) implements Validation<E, T>, Serializable {

public Invalid {
checkNonNull(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ public void optionOfSome() {

assertTrue(option.isPresent());
}

@Test
public void noneSerializable() throws IOException, ClassNotFoundException {
Option<Unit> option = Option.none();

byte[] bytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos)) {
Expand All @@ -183,15 +183,36 @@ public void noneSerializable() throws IOException, ClassNotFoundException {

bytes = baos.toByteArray();
}

Object result;
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
result = in.readObject();
}

assertSame(option, result);
}

@Test
public void someSerializable() throws IOException, ClassNotFoundException {
Option<String> option = Option.some("value");

byte[] bytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos)) {
out.writeObject(option);
out.flush();

bytes = baos.toByteArray();
}

Object result;
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
result = in.readObject();
}

assertEquals(option, result);
}

private String message() {
return "Hola mundo";
}
Expand Down

0 comments on commit dd144f3

Please sign in to comment.