From f82829c2e356f6f976be8fb0b1437d9d8370bd24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Ulf=20J=C3=B8rgensen?= Date: Sat, 14 Apr 2018 19:30:00 +0200 Subject: [PATCH] Migrate complex maps properly --- .../massivecraft/massivecore/store/Coll.java | 8 ++- .../store/migrator/MigratorUtil.java | 50 +++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/com/massivecraft/massivecore/store/Coll.java b/src/com/massivecraft/massivecore/store/Coll.java index c910e11d..a647a017 100644 --- a/src/com/massivecraft/massivecore/store/Coll.java +++ b/src/com/massivecraft/massivecore/store/Coll.java @@ -293,7 +293,13 @@ public class Coll> extends CollAbstract } // 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. E temp; diff --git a/src/com/massivecraft/massivecore/store/migrator/MigratorUtil.java b/src/com/massivecraft/massivecore/store/migrator/MigratorUtil.java index baf62003..7af783b7 100644 --- a/src/com/massivecraft/massivecore/store/migrator/MigratorUtil.java +++ b/src/com/massivecraft/massivecore/store/migrator/MigratorUtil.java @@ -5,6 +5,7 @@ import com.massivecraft.massivecore.collections.MassiveMap; import com.massivecraft.massivecore.store.EntityInternalMap; import com.massivecraft.massivecore.util.ReflectionUtil; 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.JsonObject; import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName; @@ -169,9 +170,11 @@ public class MigratorUtil if (jsonElement.isJsonPrimitive()) return false; Type jsonType = getJsonRepresentation(realType); - + + // JsonObject is if it is an object or a map if (jsonElement.isJsonObject()) { + // For maps we loop over all the content and migrate the values if (jsonType != null && Map.class.isAssignableFrom(getClassType(jsonType))) { ParameterizedType parameterizedType = (ParameterizedType) jsonType; @@ -187,7 +190,8 @@ public class MigratorUtil } return migrated; } - + + // For objects we update the object itself and its fields boolean migrated = false; JsonObject object = jsonElement.getAsJsonObject(); Type classType = jsonType != null ? jsonType : realType; @@ -196,20 +200,53 @@ public class MigratorUtil return migrated; } + // Arrays are for arrays, collections and maps where the key is complex if (jsonElement.isJsonArray()) { + + if (jsonType == null) throw new RuntimeException("jsonType is null"); 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; if (clazz.isArray()) { elementType = clazz.getComponentType(); + if (elementType == null) throw new RuntimeException("elementType is null"); } else if (Collection.class.isAssignableFrom(clazz)) { ParameterizedType parameterizedType = (ParameterizedType) jsonType; elementType = parameterizedType.getActualTypeArguments()[0]; + if (elementType == null) throw new RuntimeException("elementType is null"); + } + else + { + throw new RuntimeException("no elementType specified"); } boolean migrated = false; @@ -279,6 +316,13 @@ public class MigratorUtil } if (superClass == null) throw new RuntimeException(type.getTypeName() + " : " + name); 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; }