Skip to content

Commit

Permalink
Merge pull request #202 from Dragon-Seeker/1.20.3-StructEndecToMapCodec
Browse files Browse the repository at this point in the history
[1.20.3] Add ability to convert a StructEndec to MapCodec
  • Loading branch information
gliscowo authored Dec 14, 2023
2 parents b9074c0 + b0326cf commit c5a0700
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 30 deletions.
62 changes: 60 additions & 2 deletions src/main/java/io/wispforest/owo/serialization/StructEndec.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package io.wispforest.owo.serialization;

import io.wispforest.owo.serialization.endec.StructEndecBuilder;
import com.mojang.serialization.*;
import io.wispforest.owo.serialization.format.edm.EdmDeserializer;
import io.wispforest.owo.serialization.format.edm.EdmElement;
import io.wispforest.owo.serialization.format.edm.EdmOps;
import io.wispforest.owo.serialization.format.edm.EdmSerializer;
import net.minecraft.util.Util;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* Helper Interface for Structs that do not conform to the {@link StructEndecBuilder} format
* Marker and template interface for all endecs which serialize structs
* <p>
* Every such endec should extend this interface to profit from the implementation of {@link #mapCodec()}
* and composability which allows {@link Endec#dispatchedStruct(Function, Function, Endec, String)} to work
*/
public interface StructEndec<T> extends Endec<T> {

Expand All @@ -22,4 +35,49 @@ default void encode(Serializer<?> serializer, T value) {
default T decode(Deserializer<?> deserializer) {
return this.decodeStruct(deserializer.struct());
}

default MapCodec<T> mapCodec() {
return new MapCodec<>() {
@Override
public <T1> Stream<T1> keys(DynamicOps<T1> ops) {
throw new UnsupportedOperationException("MapCodec generated from StructEndec cannot report keys");
}

@Override
public <T1> DataResult<T> decode(DynamicOps<T1> ops, MapLike<T1> input) {
try {
var map = new HashMap<String, EdmElement<?>>();
input.entries().forEach(pair -> {
map.put(
Util.getResult(
ops.getStringValue(pair.getFirst()),
s -> new IllegalStateException("Unable to parse key: " + s)
),
ops.convertTo(EdmOps.INSTANCE, pair.getSecond())
);
});

return DataResult.success(StructEndec.this.decode(new EdmDeserializer(EdmElement.wrapMap(map))));
} catch (Exception e) {
return DataResult.error(e::getMessage);
}
}

@Override
public <T1> RecordBuilder<T1> encode(T input, DynamicOps<T1> ops, RecordBuilder<T1> prefix) {
try {
var element = StructEndec.this.encodeFully(EdmSerializer::new, input).<Map<String, EdmElement<?>>>cast();

var result = prefix;
for (var entry : element.entrySet()) {
result = result.add(entry.getKey(), EdmOps.INSTANCE.convertTo(ops, entry.getValue()));
}

return result;
} catch (Exception e) {
return prefix.withErrorsFrom(DataResult.error(e::getMessage, input));
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mojang.datafixers.util.*;
import io.wispforest.owo.serialization.Deserializer;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.Serializer;
import io.wispforest.owo.serialization.StructEndec;

Expand All @@ -12,7 +11,7 @@
// An autogenerated beauty
public class StructEndecBuilder {

public static <S, F1> Endec<S> of(StructField<S, F1> f1, Function<F1, S> constructor) {
public static <S, F1> StructEndec<S> of(StructField<S, F1> f1, Function<F1, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand All @@ -26,7 +25,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, BiFunction<F1, F2, S> constructor) {
public static <S, F1, F2> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, BiFunction<F1, F2, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand All @@ -42,7 +41,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, Function3<F1, F2, F3, S> constructor) {
public static <S, F1, F2, F3> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, Function3<F1, F2, F3, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand All @@ -60,7 +59,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, Function4<F1, F2, F3, F4, S> constructor) {
public static <S, F1, F2, F3, F4> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, Function4<F1, F2, F3, F4, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand All @@ -80,7 +79,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, Function5<F1, F2, F3, F4, F5, S> constructor) {
public static <S, F1, F2, F3, F4, F5> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, Function5<F1, F2, F3, F4, F5, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand All @@ -102,7 +101,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, Function6<F1, F2, F3, F4, F5, F6, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, Function6<F1, F2, F3, F4, F5, F6, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand All @@ -126,7 +125,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, Function7<F1, F2, F3, F4, F5, F6, F7, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, Function7<F1, F2, F3, F4, F5, F6, F7, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand All @@ -152,7 +151,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, Function8<F1, F2, F3, F4, F5, F6, F7, F8, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, Function8<F1, F2, F3, F4, F5, F6, F7, F8, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down Expand Up @@ -180,7 +179,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, Function9<F1, F2, F3, F4, F5, F6, F7, F8, F9, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, Function9<F1, F2, F3, F4, F5, F6, F7, F8, F9, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down Expand Up @@ -210,7 +209,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, Function10<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, Function10<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down Expand Up @@ -242,7 +241,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, Function11<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, Function11<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down Expand Up @@ -276,7 +275,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, Function12<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, Function12<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down Expand Up @@ -312,7 +311,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, Function13<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, Function13<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down Expand Up @@ -350,7 +349,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, StructField<S, F14> f14, Function14<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, StructField<S, F14> f14, Function14<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down Expand Up @@ -390,7 +389,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, StructField<S, F14> f14, StructField<S, F15> f15, Function15<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, StructField<S, F14> f14, StructField<S, F15> f15, Function15<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down Expand Up @@ -432,7 +431,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, StructField<S, F14> f14, StructField<S, F15> f15, StructField<S, F16> f16, Function16<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, StructField<S, F14> f14, StructField<S, F15> f15, StructField<S, F16> f16, Function16<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down Expand Up @@ -476,7 +475,7 @@ public S decodeStruct(Deserializer.Struct struct) {
};
}

public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17> Endec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, StructField<S, F14> f14, StructField<S, F15> f15, StructField<S, F16> f16, StructField<S, F17> f17, Function17<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, S> constructor) {
public static <S, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17> StructEndec<S> of(StructField<S, F1> f1, StructField<S, F2> f2, StructField<S, F3> f3, StructField<S, F4> f4, StructField<S, F5> f5, StructField<S, F6> f6, StructField<S, F7> f7, StructField<S, F8> f8, StructField<S, F9> f9, StructField<S, F10> f10, StructField<S, F11> f11, StructField<S, F12> f12, StructField<S, F13> f13, StructField<S, F14> f14, StructField<S, F15> f15, StructField<S, F16> f16, StructField<S, F17> f17, Function17<F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, S> constructor) {
return new StructEndec<>() {
@Override
public void encodeStruct(Serializer.Struct struct, S value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.wispforest.owo.text;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.StructEndecBuilder;
import net.minecraft.text.StringVisitable;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
Expand All @@ -12,9 +12,7 @@
public record InsertingTextContent(int index) implements TextContent {

public static final TextContent.Type<InsertingTextContent> TYPE = new Type<>(
RecordCodecBuilder.mapCodec(
instance -> instance.group(Codec.INT.fieldOf("index").forGetter(InsertingTextContent::index)).apply(instance, InsertingTextContent::new)
),
StructEndecBuilder.of(Endec.INT.fieldOf("index", InsertingTextContent::index), InsertingTextContent::new).mapCodec(),
"owo:insert"
);

Expand Down
Loading

0 comments on commit c5a0700

Please sign in to comment.