Skip to content

Commit bc03649

Browse files
Added @ConfigAttribute for attr names with dash
1 parent f71b70b commit bc03649

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.zetaplugins.zetacore.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Indicates that a field represents a configuration attribute.
10+
*/
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@Target(ElementType.FIELD)
13+
public @interface ConfigAttribute {
14+
/**
15+
* The name of the configuration attribute.
16+
* @return The name of the configuration attribute.
17+
*/
18+
String name();
19+
}

zetacore/src/main/java/com/zetaplugins/zetacore/services/config/ConfigMapper.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.zetaplugins.zetacore.services.config;
22

3+
import com.zetaplugins.zetacore.annotations.ConfigAttribute;
34
import com.zetaplugins.zetacore.annotations.PluginConfig;
45
import com.zetaplugins.zetacore.annotations.NestedConfig;
56
import org.bukkit.configuration.ConfigurationSection;
@@ -54,10 +55,11 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
5455
for (Field field : configClass.getDeclaredFields()) {
5556
field.setAccessible(true);
5657
Class<?> fieldType = field.getType();
58+
String fieldName = getFieldName(field);
5759

5860
// Nested objects annotated with @NestedConfig
5961
if (fieldType.isAnnotationPresent(NestedConfig.class)) {
60-
ConfigurationSection nestedSection = section.getConfigurationSection(field.getName());
62+
ConfigurationSection nestedSection = section.getConfigurationSection(fieldName);
6163
if (nestedSection == null) continue;
6264
Object nestedInstance = fieldType.getConstructor().newInstance();
6365
mapSection(nestedSection, nestedInstance);
@@ -67,9 +69,9 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
6769

6870
// Lists
6971
if (isListType(fieldType)) {
70-
Object rawListObj = section.get(field.getName());
72+
Object rawListObj = section.get(fieldName);
7173
if (rawListObj instanceof List<?> rawList) {
72-
List<Object> mappedList = convertList(field.getGenericType(), rawList, field.getName());
74+
List<Object> mappedList = convertList(field.getGenericType(), rawList, fieldName);
7375
field.set(instance, mappedList);
7476
} else if (rawListObj == null) {
7577
// nothing present - skip
@@ -82,13 +84,13 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
8284
// Maps
8385
if (isMapType(fieldType)) {
8486
// raw can be a ConfigurationSection or a Map/LinkedHashMap
85-
Object rawMap = section.get(field.getName());
86-
ConfigurationSection rawSection = section.getConfigurationSection(field.getName());
87+
Object rawMap = section.get(fieldName);
88+
ConfigurationSection rawSection = section.getConfigurationSection(fieldName);
8789
if (rawSection != null) {
88-
Map<?, ?> mapped = convertMap(field.getGenericType(), rawSection, field.getName());
90+
Map<?, ?> mapped = convertMap(field.getGenericType(), rawSection, fieldName);
8991
field.set(instance, mapped);
9092
} else if (rawMap instanceof LinkedHashMap<?, ?> || rawMap instanceof Map<?, ?>) {
91-
Map<?, ?> mapped = convertMap(field.getGenericType(), rawMap, field.getName());
93+
Map<?, ?> mapped = convertMap(field.getGenericType(), rawMap, fieldName);
9294
field.set(instance, mapped);
9395
} else if (rawMap == null) {
9496
// nothing present - skip
@@ -100,7 +102,7 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
100102

101103
// Enum types
102104
if (fieldType.isEnum()) {
103-
String enumName = section.getString(field.getName());
105+
String enumName = section.getString(fieldName);
104106
if (enumName == null) continue;
105107
String upperEnumName = enumName.toUpperCase();
106108
Object enumValue = Enum.valueOf((Class<Enum>) fieldType, upperEnumName);
@@ -110,7 +112,7 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
110112

111113
// Primitive / String / basic types
112114
if (isSimpleType(fieldType)) {
113-
Object value = section.get(field.getName());
115+
Object value = section.get(fieldName);
114116
if (value != null) {
115117
field.set(instance, value);
116118
}
@@ -135,7 +137,7 @@ private static void mapFromMap(LinkedHashMap<?, ?> map, Object instance) throws
135137
Class<?> clazz = instance.getClass();
136138
for (Field field : clazz.getDeclaredFields()) {
137139
field.setAccessible(true);
138-
Object value = map.get(field.getName());
140+
Object value = map.get(getFieldName(field));
139141
if (value == null) continue;
140142

141143
Class<?> fieldType = field.getType();
@@ -146,11 +148,11 @@ private static void mapFromMap(LinkedHashMap<?, ?> map, Object instance) throws
146148
field.set(instance, nestedInstance);
147149

148150
} else if (isListType(fieldType) && value instanceof List<?> rawList) {
149-
List<Object> mappedList = convertList(field.getGenericType(), rawList, field.getName());
151+
List<Object> mappedList = convertList(field.getGenericType(), rawList, getFieldName(field));
150152
field.set(instance, mappedList);
151153

152154
} else if (isMapType(fieldType) && value instanceof LinkedHashMap<?, ?> rawLinkedMap) {
153-
Map<?, ?> mappedMap = convertMap(field.getGenericType(), rawLinkedMap, field.getName());
155+
Map<?, ?> mappedMap = convertMap(field.getGenericType(), rawLinkedMap, getFieldName(field));
154156
field.set(instance, mappedMap);
155157

156158
} else if (isSimpleType(fieldType)) {
@@ -348,4 +350,12 @@ private static boolean isListType(Class<?> type) {
348350
private static boolean isMapType(Class<?> type) {
349351
return Map.class.isAssignableFrom(type);
350352
}
353+
354+
private static String getFieldName(Field field) {
355+
if (field.isAnnotationPresent(ConfigAttribute.class)) {
356+
ConfigAttribute annotation = field.getAnnotation(ConfigAttribute.class);
357+
return annotation.name();
358+
}
359+
return field.getName();
360+
}
351361
}

zetacore/src/test/java/com/zetaplugins/zetacore/services/config/ConfigMapperTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void testBasicMapping() {
3333
assertNotNull(myConfig.settings);
3434
assertTrue(myConfig.settings.enableFeature);
3535
assertEquals(List.of("LustigerName1", "LustigerName2", "LustigerName3"), myConfig.settings.funnynames);
36+
assertEquals("test-value", myConfig.attrWithDash);
3637

3738
assertNotNull(myConfig.items);
3839
assertEquals(2, myConfig.items.size());

zetacore/src/test/java/com/zetaplugins/zetacore/services/config/testconfigs/MyConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.zetaplugins.zetacore.services.config.testconfigs;
22

33

4+
import com.zetaplugins.zetacore.annotations.ConfigAttribute;
45
import com.zetaplugins.zetacore.annotations.PluginConfig;
56

67
import java.util.List;
@@ -10,6 +11,8 @@
1011
public class MyConfig {
1112
public String lang = "de-DE";
1213
public SomeStatus someStatus = SomeStatus.ACTIVE;
14+
@ConfigAttribute(name = "attr-with-dash")
15+
public String attrWithDash = "default-test-value";
1316
public List<SomeStatus> statusList = List.of(SomeStatus.ACTIVE, SomeStatus.INACTIVE);
1417
public Map<SomeStatus, Integer> statusToCodeMap = Map.of(
1518
SomeStatus.ACTIVE, 200,

zetacore/src/test/resources/test-config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ lang: "de-DE"
33

44
someStatus: "active"
55

6+
attr-with-dash: "test-value"
7+
68
statusList:
79
- "active"
810
- "inactive"

0 commit comments

Comments
 (0)