Migrate complex maps properly

This commit is contained in:
Magnus Ulf Jørgensen 2018-04-14 19:30:00 +02:00
parent 8274b210ff
commit f82829c2e3
2 changed files with 54 additions and 4 deletions

View File

@ -293,7 +293,13 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
} }
// Migrate if another version is wanted // Migrate if another version is wanted
boolean migrated = MigratorUtil.migrate(this.getEntityClass(), raw); boolean migrated;
try {
migrated = MigratorUtil.migrate(this.getEntityClass(), raw);
} catch (RuntimeException e) {
System.out.println("FAILURE FOR CLASS: " + this.getEntityClass());
throw e;
}
// Calculate temp but handle raw cases. // Calculate temp but handle raw cases.
E temp; E temp;

View File

@ -5,6 +5,7 @@ import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.store.EntityInternalMap; import com.massivecraft.massivecore.store.EntityInternalMap;
import com.massivecraft.massivecore.util.ReflectionUtil; import com.massivecraft.massivecore.util.ReflectionUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.massivecore.xlib.gson.JsonArray;
import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject; import com.massivecraft.massivecore.xlib.gson.JsonObject;
import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName; import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName;
@ -170,8 +171,10 @@ public class MigratorUtil
Type jsonType = getJsonRepresentation(realType); Type jsonType = getJsonRepresentation(realType);
// JsonObject is if it is an object or a map
if (jsonElement.isJsonObject()) if (jsonElement.isJsonObject())
{ {
// For maps we loop over all the content and migrate the values
if (jsonType != null && Map.class.isAssignableFrom(getClassType(jsonType))) if (jsonType != null && Map.class.isAssignableFrom(getClassType(jsonType)))
{ {
ParameterizedType parameterizedType = (ParameterizedType) jsonType; ParameterizedType parameterizedType = (ParameterizedType) jsonType;
@ -188,6 +191,7 @@ public class MigratorUtil
return migrated; return migrated;
} }
// For objects we update the object itself and its fields
boolean migrated = false; boolean migrated = false;
JsonObject object = jsonElement.getAsJsonObject(); JsonObject object = jsonElement.getAsJsonObject();
Type classType = jsonType != null ? jsonType : realType; Type classType = jsonType != null ? jsonType : realType;
@ -196,20 +200,53 @@ public class MigratorUtil
return migrated; return migrated;
} }
// Arrays are for arrays, collections and maps where the key is complex
if (jsonElement.isJsonArray()) if (jsonElement.isJsonArray())
{ {
if (jsonType == null) throw new RuntimeException("jsonType is null");
Class<?> clazz = getClassType(jsonType); Class<?> clazz = getClassType(jsonType);
if (clazz == null) throw new RuntimeException("clazz is null");
// So if it is a map with a complex key it is represented as an array
if (Map.class.isAssignableFrom(clazz))
{
ParameterizedType parameterizedType = (ParameterizedType) jsonType;
Type keyType = parameterizedType.getActualTypeArguments()[0];
Type valueType = parameterizedType.getActualTypeArguments()[1];
JsonArray array = jsonElement.getAsJsonArray();
boolean migrated = false;
for (JsonElement element : array)
{
// And all of the contents are arrays with the key as 0 and the value as 1
JsonArray innerArray = element.getAsJsonArray();
JsonElement key = innerArray.get(0);
JsonElement value = innerArray.get(1);
migrated = migrate(keyType, key) | migrated;
migrated = migrate(valueType, value) | migrated;
}
return migrated;
}
Type elementType = null; Type elementType = null;
if (clazz.isArray()) if (clazz.isArray())
{ {
elementType = clazz.getComponentType(); elementType = clazz.getComponentType();
if (elementType == null) throw new RuntimeException("elementType is null");
} }
else if (Collection.class.isAssignableFrom(clazz)) else if (Collection.class.isAssignableFrom(clazz))
{ {
ParameterizedType parameterizedType = (ParameterizedType) jsonType; ParameterizedType parameterizedType = (ParameterizedType) jsonType;
elementType = parameterizedType.getActualTypeArguments()[0]; elementType = parameterizedType.getActualTypeArguments()[0];
if (elementType == null) throw new RuntimeException("elementType is null");
}
else
{
throw new RuntimeException("no elementType specified");
} }
boolean migrated = false; boolean migrated = false;
@ -279,6 +316,13 @@ public class MigratorUtil
} }
if (superClass == null) throw new RuntimeException(type.getTypeName() + " : " + name); if (superClass == null) throw new RuntimeException(type.getTypeName() + " : " + name);
Type elementType = ReflectionUtil.getField(superClass, name).getGenericType(); Type elementType = ReflectionUtil.getField(superClass, name).getGenericType();
try
{
migrated = migrate(elementType, element) | migrated;
} catch (RuntimeException e) {
System.out.println("FAILURE FOR FIELD: " + name);
throw e;
}
migrated = migrate(elementType, element) | migrated; migrated = migrate(elementType, element) | migrated;
} }