Validation fallback values, tests and fixes
This commit is contained in:
@@ -43,6 +43,10 @@ public class TweedEntryReaderWriters {
|
||||
return TweedEntryReaderWriterImpls.STRING_READER_WRITER;
|
||||
}
|
||||
|
||||
public static <T, C extends ConfigEntry<T>> TweedEntryReaderWriter<T, C> nullableReaderWriter(TweedEntryReaderWriter<T, C> delegate) {
|
||||
return new TweedEntryReaderWriterImpls.NullableReaderWriter<>(delegate);
|
||||
}
|
||||
|
||||
public static <T, C extends Collection<T>> TweedEntryReaderWriter<C, CoherentCollectionConfigEntry<T, C>> coherentCollectionReaderWriter() {
|
||||
//noinspection unchecked
|
||||
return (TweedEntryReaderWriter<C, CoherentCollectionConfigEntry<T,C>>)(TweedEntryReaderWriter<?, ?>) TweedEntryReaderWriterImpls.COHERENT_COLLECTION_READER_WRITER;
|
||||
|
||||
@@ -19,6 +19,7 @@ import de.siphalor.tweed5.patchwork.api.Patchwork;
|
||||
import de.siphalor.tweed5.patchwork.api.PatchworkClassCreator;
|
||||
import de.siphalor.tweed5.patchwork.impl.PatchworkClass;
|
||||
import de.siphalor.tweed5.patchwork.impl.PatchworkClassGenerator;
|
||||
import de.siphalor.tweed5.patchwork.impl.PatchworkClassPart;
|
||||
import lombok.Setter;
|
||||
import lombok.Value;
|
||||
|
||||
@@ -91,6 +92,10 @@ public class ReadWriteExtensionImpl implements ReadWriteExtension {
|
||||
|
||||
try {
|
||||
readWriteContextExtensionsDataPatchwork = patchworkClassCreator.createClass(readWriteContextExtensionsDataClasses.keySet());
|
||||
for (PatchworkClassPart patchworkClassPart : readWriteContextExtensionsDataPatchwork.parts()) {
|
||||
RegisteredExtensionDataImpl<ReadWriteContextExtensionsData, ?> registeredExtension = readWriteContextExtensionsDataClasses.get(patchworkClassPart.partInterface());
|
||||
registeredExtension.setter = patchworkClassPart.fieldSetter();
|
||||
}
|
||||
} catch (PatchworkClassGenerator.GenerationException e) {
|
||||
throw new IllegalStateException("Failed to generate read write context extensions' data patchwork class", e);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,28 @@ public class TweedEntryReaderWriterImpls {
|
||||
|
||||
public static final TweedEntryReaderWriter<Object, ConfigEntry<Object>> NOOP_READER_WRITER = new NoopReaderWriter();
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public static class NullableReaderWriter<T, C extends ConfigEntry<T>> implements TweedEntryReaderWriter<T, C> {
|
||||
private final TweedEntryReaderWriter<T, C> delegate;
|
||||
|
||||
@Override
|
||||
public T read(TweedDataReader reader, C entry, TweedReadContext context) throws TweedEntryReadException, TweedDataReadException {
|
||||
if (reader.peekToken().isNull()) {
|
||||
return null;
|
||||
}
|
||||
return delegate.read(reader, entry, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(TweedDataVisitor writer, T value, C entry, TweedWriteContext context) throws TweedEntryWriteException, TweedDataWriteException {
|
||||
if (value == null) {
|
||||
writer.visitNull();
|
||||
} else {
|
||||
delegate.write(writer, value, entry, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
private static class PrimitiveReaderWriter<T> implements TweedEntryReaderWriter<T, ConfigEntry<T>> {
|
||||
private final Function<TweedDataToken, T> readerCall;
|
||||
|
||||
Reference in New Issue
Block a user