Minecraft

Codec으로 List<{keyLike: String, valueLike: String | List<String>}> 인디코딩하기

no

noeul
답변 대기중
30 XP

Mojang DataFixerUpper의 Codec을 사용해 List<{keyLike: String, valueLike: List<String> | String}>List<SomeRecord(keyLike: String, valueLike: List<String>)>를 인디코딩하려면 ```java
Codec.list(
RecordCodecBuilder.create(instance -> instance
.group(
Codec.STRING.fieldOf("keyLike")
.forGetter(SomeRecord::keyLike),
Codec.either(CODEC.STRING.listOf(), Codec.STRING)
.fieldOf("valueLike")
.xmap(
either -> either.map(UnaryOperator.identity(), List::of),
list -> list.size() > 1
? Either.left(list)
: Either.right(list.getFirst())
)
.forGetter(SomeRecord::valueLike)
)
.apply(instance, SomeRecord::new)
)
)

// SomeRecord.java
public record SomeRecord(String keyLike, List<String> valueLike) {
// ...
}


불러오는 중...