chore(config-core): lift up logic for serializeOneTo

This commit is contained in:
Johannes Frohnmeyer 2024-05-10 09:01:32 +02:00
parent 41e8a71d28
commit 78bcde1b86
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 15 additions and 16 deletions

View File

@ -7,9 +7,13 @@ import io.gitlab.jfronny.commons.serialize.databind.api.TypeToken;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.config.api.v2.type.Type;
import io.gitlab.jfronny.libjf.config.impl.dsl.DslEntryInfo;
import io.gitlab.jfronny.libjf.config.impl.entrypoint.JfConfigSafe;
import org.jetbrains.annotations.ApiStatus;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
public interface EntryInfo<T> {
static EntryInfo<?> ofField(Field field) {
@ -84,11 +88,19 @@ public interface EntryInfo<T> {
}
default <TEx extends Exception, Writer extends SerializeWriter<TEx, Writer>> void serializeOneTo(T value, String translationPrefix, Writer writer) throws TEx, MalformedDataException {
String commentText;
if ((commentText = JfConfigSafe.TRANSLATION_SUPPLIER.apply(translationPrefix + getName() + ".tooltip")) != null) {
writer.comment(commentText);
}
if (getValueType().isEnum()) {
writer.comment("Valid: [" + Arrays.stream(getValueType().asEnum().options()).map(Objects::toString).collect(Collectors.joining(", ")) + "]");
}
writer.name(getName());
LibJf.MAPPER.getAdapter(getTypeToken()).serialize(value, writer);
}
default TypeToken<T> getTypeToken() {
return (TypeToken<T>) getValueType().asToken();
return (TypeToken<T>) Objects.requireNonNullElse(getValueType().asToken(), TypeToken.get(String.class));
}
/**

View File

@ -185,23 +185,10 @@ public class DslEntryInfo<T> implements EntryInfo<T> {
@Override
public <TEx extends Exception, Writer extends SerializeWriter<TEx, Writer>> void writeTo(Writer writer, String translationPrefix) throws TEx, IllegalAccessException {
serializeOneTo(getValue(), translationPrefix, writer);
}
@Override
public <TEx extends Exception, Writer extends SerializeWriter<TEx, Writer>> void serializeOneTo(T value, String translationPrefix, Writer writer) throws TEx {
String commentText;
if ((commentText = JfConfigSafe.TRANSLATION_SUPPLIER.apply(translationPrefix + getName() + ".tooltip")) != null) {
writer.comment(commentText);
}
if (type.isEnum()) {
writer.comment("Valid: [" + Arrays.stream(((Type.TEnum<T>)type).options()).map(Objects::toString).collect(Collectors.joining(", ")) + "]");
}
writer.name(name);
try {
LibJf.MAPPER.getAdapter((TypeToken<T>) Objects.requireNonNullElse(type.asToken(), TypeToken.get(String.class))).serialize(value, writer);
serializeOneTo(getValue(), translationPrefix, writer);
} catch (MalformedDataException e) {
LibJf.LOGGER.error("Could not write " + name, e);
LibJf.LOGGER.error("Could not write " + getName(), e);
}
}