Skip to content

Commit

Permalink
fix: deserializing commented node in RecordDeserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Feb 20, 2024
1 parent 91c321c commit d2968db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.siroshun09.configapi.core.node.BooleanArray;
import com.github.siroshun09.configapi.core.node.BooleanValue;
import com.github.siroshun09.configapi.core.node.ByteArray;
import com.github.siroshun09.configapi.core.node.CommentedNode;
import com.github.siroshun09.configapi.core.node.DoubleArray;
import com.github.siroshun09.configapi.core.node.EnumValue;
import com.github.siroshun09.configapi.core.node.FloatArray;
Expand Down Expand Up @@ -293,7 +294,9 @@ private Object processInlinedRecord(@NotNull RecordComponent parent, @NotNull Cl
@SuppressWarnings("unchecked")
private @Nullable Object deserializeNode(@NotNull Node<?> node, @NotNull Class<?> clazz,
@Nullable Object defaultObject) {
if (clazz == boolean.class || clazz == Boolean.class) {
if (node instanceof CommentedNode<?> commentedNode) {
return this.deserializeNode(commentedNode.node(), clazz, defaultObject);
} else if (clazz == boolean.class || clazz == Boolean.class) {
return node instanceof BooleanValue booleanValue ? booleanValue.value() : defaultObject;
} else if (clazz == String.class) {
return node instanceof StringValue stringValue ? stringValue.asString() : defaultObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.github.siroshun09.configapi.core.serialization.record;

import com.github.siroshun09.configapi.core.comment.SimpleComment;
import com.github.siroshun09.configapi.core.node.CommentableNode;
import com.github.siroshun09.configapi.core.node.MapNode;
import com.github.siroshun09.configapi.core.node.StringValue;
import com.github.siroshun09.configapi.core.serialization.SerializationException;
import com.github.siroshun09.configapi.core.serialization.key.KeyGenerator;
import com.github.siroshun09.configapi.test.shared.data.Samples;
Expand Down Expand Up @@ -58,4 +61,18 @@ void testCustomObjectAndCustomKeyGenerator() {
var deserializer = RecordDeserializer.builder(Samples.UUIDRecord.class).addDeserializer(UUID.class, node -> UUID.fromString(node.value().toString())).keyGenerator(KeyGenerator.CAMEL_TO_KEBAB).build();
Assertions.assertEquals(Samples.uuidRecord(), deserializer.deserialize(Samples.uuidRecordMapNode()));
}

@Test
void testDeserializingCommentedNode() {
var deserializer = RecordDeserializer.create(SimpleRecord.class);

var mapNode = MapNode.create();
mapNode.set("value", CommentableNode.withComment(StringValue.fromString("value"), SimpleComment.create("comment")));

var actual = deserializer.deserialize(mapNode);
Assertions.assertEquals("value", actual.value());
}

private record SimpleRecord(String value) {
}
}

0 comments on commit d2968db

Please sign in to comment.