Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting group comments using annotations #78

Open
wants to merge 1 commit into
base: 0.24.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@
* @return An empty string ({@code ""}) if no custom name was set, or the custom name if one was set.
*/
String name() default "";

/**
* Sets the comment that will be used for this setting group.
*
* <p>If empty, no comment will be set.
*
* @return An empty string ({@code ""}) if no comment was set, or the comment if one was set.
*/
String comment() default "";
}

@Target({})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public void processListenerField(Object pojo, Field field, String name) {
@Override
public void processGroup(Object pojo, Field group) throws ProcessingMemberException {
try {
String name = this.findName(group);
ConfigTreeBuilder sub = this.builder.fork(name);
String name = findSettingAnnotation(Setting.Group.class, group).map(Setting.Group::name).filter(s -> !s.isEmpty()).orElse(null);
String comment = findSettingAnnotation(Setting.Group.class, group).map(Setting.Group::comment).filter(s -> !s.isEmpty()).orElse(null);
ConfigTreeBuilder sub = this.builder.fork(name).withComment(comment);
group.setAccessible(true);
Object subPojo = group.get(pojo);

Expand Down Expand Up @@ -167,11 +168,10 @@ private <R, S> void processSetting(Object pojo, Field setting, ConfigType<R, S,

@Nonnull
private String findName(Field field) {
return findSettingAnnotation(Setting.Group.class, field).map(Setting.Group::name).filter(s -> !s.isEmpty()).orElseGet(
() -> findSettingAnnotation(Setting.class, field).map(Setting::name).filter(s -> !s.isEmpty()).orElseGet(
() -> this.convention.name(field.getName())
)
);
return findSettingAnnotation(Setting.class, field)
.map(Setting::name)
.filter(s -> !s.isEmpty())
.orElseGet(() -> this.convention.name(field.getName()));
}

@Nullable
Expand Down Expand Up @@ -304,24 +304,20 @@ private <C> void applyAnnotationProcessors(Object pojo, Field field, C sub, Map<
return (T) type.constrain(processor, annotation, annotated);
}

@SuppressWarnings("unchecked")
private <T> T findDefaultValue(Object pojo, Field field) throws FiberException {
boolean accessible = field.isAccessible();
field.setAccessible(true);
T value;

try {
value = (T) field.get(pojo);
@SuppressWarnings("unchecked") T value = (T) field.get(pojo);

if (value == null) {
throw new MalformedFieldException("Default value for field '" + field.getName() + "' is null");
}

return value;
} catch (IllegalAccessException e) {
throw new FiberException("Couldn't get value for field '" + field.getName() + "'", e);
}

field.setAccessible(accessible);
return value;
}

private <T> BiConsumer<T, T> constructListenerFromMember(Object pojo, Member listener, Class<T> wantedType) throws FiberException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.StringSerializableType;
import io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.derived.ConfigTypes;
import io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.derived.ListConfigType;
import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigBranch;
import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigLeaf;
import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigNode;
import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigTree;
Expand Down Expand Up @@ -226,9 +227,10 @@ void testSubNodes() throws FiberException {
SubNodePojo pojo = new SubNodePojo();
this.annotatedSettings.applyToNode(this.node, pojo);
assertEquals(1, this.node.getItems().size(), "Node has one item");
ConfigTree subnode = (ConfigTree) this.node.lookup("a");
ConfigBranch subnode = this.node.lookupBranch("a");
assertNotNull(subnode, "Subnode exists");
assertEquals(1, subnode.getItems().size(), "Subnode has one item");
assertEquals("A subnode", subnode.getComment());
}

@Test
Expand Down Expand Up @@ -388,7 +390,7 @@ private static class IgnoredPojo {
}

private static class SubNodePojo {
@Setting.Group(name = "a")
@Setting.Group(name = "a", comment = "A subnode")
public SubNode node = new SubNode();

// we want to test this edge case
Expand Down