Skip to content

Commit

Permalink
Merge pull request #9 from m7rlin/master
Browse files Browse the repository at this point in the history
Fix removing cache; Add annotation ConfigOptional on custom DTO classes
  • Loading branch information
mikigal authored Jun 8, 2023
2 parents e8f204d + 8d7d891 commit b6ea331
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ maven {
url = 'https://repo.mikigal.pl/releases'
}
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.3'
compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.4'
```

#### Maven
Expand All @@ -34,7 +34,7 @@ compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.3'
<dependency>
<groupId>pl.mikigal</groupId>
<artifactId>ConfigAPI</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
<scope>compile</scope>
</dependency>
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'pl.mikigal'
version '1.2.3'
version '1.2.4'

publishing {
repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @since 1.0
* @author Mikołaj Gałązka
*/
@Target({ElementType.METHOD})
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface ConfigOptional {
}
1 change: 1 addition & 0 deletions src/main/java/pl/mikigal/config/serializer/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final void serialize(String path, Object object, BukkitConfiguration conf
}

this.saveObject(path, (T) object, configuration);
configuration.addToCache(path, object);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pl.mikigal.config.serializer.universal;

import pl.mikigal.config.BukkitConfiguration;
import pl.mikigal.config.annotation.ConfigOptional;
import pl.mikigal.config.exception.InvalidConfigException;
import pl.mikigal.config.exception.MissingSerializerException;
import pl.mikigal.config.serializer.Serializer;
Expand All @@ -17,7 +18,7 @@
* Class must have default constructor (no-args).
* @see Serializer
* @see Serializable
* @since 1.1.7
* @since 1.1.8
* @author Mikołaj Gałązka
*/
public class UniversalObjectSerializer extends Serializer<Serializable> {
Expand All @@ -34,6 +35,14 @@ protected void saveObject(String path, Serializable object, BukkitConfiguration

field.setAccessible(true);
Object value = field.get(object);

// Check if field is optional
if (field.isAnnotationPresent(ConfigOptional.class)) {
field.setAccessible(false);
if (value == null) {
continue;
}
}

try {
if (TypeUtils.isSimpleType(field.getType())) {
Expand Down Expand Up @@ -88,6 +97,15 @@ public Serializable deserialize(String path, BukkitConfiguration configuration)
}

field.setAccessible(true);

String fullpath = path + "." + configuration.getNameStyle().format(field.getName());

// Check if field is optional
if (field.isAnnotationPresent(ConfigOptional.class)) {
if (configuration.get(fullpath) == null){
continue;
}
}

Class<?> type = field.getType();
if (TypeUtils.isSimpleType(type)) {
Expand Down

0 comments on commit b6ea331

Please sign in to comment.