Skip to content

Commit

Permalink
Simplify ParametricKeyedValue
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Jul 23, 2024
1 parent 3390fbb commit 2d1ffa2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public <A> DataResult<App<Mu, A>> keyed(Key<A> key) {
public <MuO extends K1, MuP extends K1, T> DataResult<App<Mu, App<MuO, T>>> parametricallyKeyed(Key2<MuP, MuO> key, App<MuP, T> parameter) {
return parametricKeys.get(key)
.map(ParametricKeyedValue::unbox)
.map(val -> val.converter().convert(parameter))
.map(val -> val.convert(parameter))
.map(DataResult::success)
.orElse(DataResult.error(() -> "Unknown key "+key.name()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public <B> App<CodecInterpreter.Holder.Mu, B> convert(App<Holder.Mu, B> app) {
@Override
public <A extends K1, B extends K1> App2<ParametricKeyedValue.Mu<CodecInterpreter.Holder.Mu>, A, B> convert(App2<ParametricKeyedValue.Mu<Holder.Mu>, A, B> input) {
var unboxed = ParametricKeyedValue.unbox(input);
return new ParametricKeyedValue<>(new ParametricKeyedValue.Converter<>() {
@Override
public <T> App<CodecInterpreter.Holder.Mu, App<B, T>> convert(App<A, T> parameter) {
var mapCodec = unbox(unboxed.converter().convert(parameter));
return new CodecInterpreter.Holder<>(mapCodec.codec());
}
});
return new ParametricKeyedValue<>() {
@Override
public <T> App<CodecInterpreter.Holder.Mu, App<B, T>> convert(App<A, T> parameter) {
var mapCodec = unbox(unboxed.convert(parameter));
return new CodecInterpreter.Holder<>(mapCodec.codec());
}
};
}
}).join(parametricCodecKeys));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import com.mojang.datafixers.kinds.K1;
import com.mojang.datafixers.kinds.K2;

public record ParametricKeyedValue<N extends K1, MuP extends K1, MuO extends K1>(Converter<N, MuP, MuO> converter) implements App2<ParametricKeyedValue.Mu<N>, MuP, MuO> {
public static class Mu<N extends K1> implements K2 {}
public interface ParametricKeyedValue<N extends K1, MuP extends K1, MuO extends K1> extends App2<ParametricKeyedValue.Mu<N>, MuP, MuO> {
class Mu<N extends K1> implements K2 {}

public interface Converter<N extends K1, MuP extends K1, MuO extends K1> {
<T> App<N, App<MuO, T>> convert(App<MuP, T> parameter);
}
<T> App<N, App<MuO, T>> convert(App<MuP, T> parameter);

public static <N extends K1, MuP extends K1, MuO extends K1> ParametricKeyedValue<N, MuP, MuO> unbox(App2<Mu<N>, MuP, MuO> boxed) {
static <N extends K1, MuP extends K1, MuO extends K1> ParametricKeyedValue<N, MuP, MuO> unbox(App2<Mu<N>, MuP, MuO> boxed) {
return (ParametricKeyedValue<N, MuP, MuO>) boxed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ private static <T> Codec<WithType<T>> withTypeCodec(Prefix<T> prefix) {
private static final Codec<WithType<Integer>> CODEC = new CodecInterpreter(
Keys.<CodecInterpreter.Holder.Mu, Object>builder().build(),
Keys2.<ParametricKeyedValue.Mu<CodecInterpreter.Holder.Mu>, K1, K1>builder()
.add(WITH_TYPE, new ParametricKeyedValue<>(new ParametricKeyedValue.Converter<>() {
.add(WITH_TYPE, new ParametricKeyedValue<>() {
@Override
public <T> App<CodecInterpreter.Holder.Mu, App<WithType.Mu, T>> convert(App<Prefix.Mu, T> parameter) {
return new CodecInterpreter.Holder<>(withTypeCodec(Prefix.unbox(parameter)).xmap(Function.identity(), WithType::unbox));
}
}))
})
.build()
).interpret(STRUCTURE).getOrThrow();

Expand Down

0 comments on commit 2d1ffa2

Please sign in to comment.