Skip to content

Commit

Permalink
Improved Map support
Browse files Browse the repository at this point in the history
  • Loading branch information
mikigal committed Feb 15, 2021
1 parent 873360f commit c957815
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 46 deletions.
6 changes: 1 addition & 5 deletions src/main/java/pl/mikigal/config/BukkitConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ public void set(String path, Object value, Comment comment) {

@Override
public void set(String path, Object value) {
if (value != null && value.getClass().isArray()) {
value = Arrays.asList(((Object[]) value));
}

if (!(value instanceof Collection) && (value == null || TypeUtils.isSimpleType(value))) {
if (!(value instanceof Collection) && !(value instanceof Map) && (value == null || TypeUtils.isSimpleType(value))) {
super.set(path, value);

if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pl.mikigal.config.serializer.Serializer;
import pl.mikigal.config.serializer.Serializers;
import pl.mikigal.config.util.TypeUtils;
import test.TestConfig;

import java.lang.reflect.Method;
import java.util.*;
Expand All @@ -34,7 +33,7 @@ protected void saveObject(String path, Collection object, BukkitConfiguration co
throw new MissingSerializerException(generic);
}

configuration.set(path + ".collection", object.getClass().getName());
configuration.set(path + ".structure", object.getClass().getName());
configuration.set(path + ".type", generic.getName());

int index = 0;
Expand All @@ -54,7 +53,7 @@ protected void saveObject(String path, Collection object, BukkitConfiguration co
public Collection<?> deserialize(String path, BukkitConfiguration configuration) {
ConfigurationSection section = configuration.getConfigurationSection(path);

String collectionRaw = section.getString("collection");
String collectionRaw = section.getString("structure");
String serializerRaw = section.getString("type");

Objects.requireNonNull(collectionRaw, "Collection type is not defined for " + path);
Expand All @@ -71,7 +70,7 @@ public Collection<?> deserialize(String path, BukkitConfiguration configuration)

Collection collection = (Collection) collectionClass.newInstance();
for (String index : section.getKeys(false)) {
if (index.equals("type") || index.equals("collection")) {
if (index.equals("type") || index.equals("structure")) {
continue;
}

Expand All @@ -85,7 +84,7 @@ public Collection<?> deserialize(String path, BukkitConfiguration configuration)

return collection;
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new MissingSerializerException("Could not find class", e);
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import pl.mikigal.config.serializer.Serializers;
import pl.mikigal.config.util.TypeUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* Helper built-in serializer for processing Map
Expand All @@ -34,7 +32,9 @@ protected void saveObject(String path, Map object, BukkitConfiguration configura
throw new MissingSerializerException(generic);
}

configuration.set(path + ".structure", object.getClass().getName());
configuration.set(path + ".type", generic.getName());

for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
if (serializer == null) {
configuration.set(path + "." + entry.getKey(), entry.getValue());
Expand All @@ -48,48 +48,39 @@ protected void saveObject(String path, Map object, BukkitConfiguration configura
@Override
public Map<?, ?> deserialize(String path, BukkitConfiguration configuration) {
ConfigurationSection section = configuration.getConfigurationSection(path);
if (section == null || section.getKeys(false).size() == 0) {
return new HashMap<>();
}

if (!section.contains("type")) {
Map map = new HashMap<>();
for (String key : section.getKeys(false)) {
map.put(key, section.get(key));
}

return map;
}
String mapRaw = section.getString("structure");
String serializerRaw = section.getString("type");

String serializerClass = section.getString("type");
if (serializerClass == null) {
throw new InvalidConfigFileException("Serializer type is not defined for " + path);
}

Serializer<?> serializer = Serializers.of(serializerClass);
Objects.requireNonNull(mapRaw, "Collection type is not defined for " + path);
Objects.requireNonNull(serializerRaw, "Serializer type is not defined for " + path);

try {
if (serializer == null && !TypeUtils.isSimpleType(Class.forName(serializerClass))) {
throw new MissingSerializerException(Class.forName(serializerClass));
}
} catch (ClassNotFoundException e) {
throw new MissingSerializerException("Could not find class " + serializerClass);
}
Serializer<?> serializer = Serializers.of(serializerRaw);
Class<?> mapClass = Class.forName(mapRaw);
Class<?> serializerClass = Class.forName(serializerRaw);

Map map = new HashMap<>();
for (String key : section.getKeys(false)) {
if (key.equals("type")) {
continue;
if (serializer == null && !TypeUtils.isSimpleType(serializerClass)) {
throw new MissingSerializerException(serializerClass);
}

if (serializer == null) {
map.put(key, configuration.get(path + "." + key));
continue;
Map map = (Map) mapClass.newInstance();
for (String key : section.getKeys(false)) {
if (key.equals("type") || key.equals("structure")) {
continue;
}

if (serializer == null) {
map.put(key, configuration.get(path + "." + key));
continue;
}

map.put(key, serializer.deserialize(path + "." + key, configuration));
}

map.put(key, serializer.deserialize(path + "." + key, configuration));
return map;
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}

return map;
}
}

0 comments on commit c957815

Please sign in to comment.