Skip to content

Commit

Permalink
Merge pull request #415 from Countly/fix_array_removal
Browse files Browse the repository at this point in the history
fix: move list and json array unsupported removals to ios way
  • Loading branch information
turtledreams authored Nov 7, 2024
2 parents 96b8b1d + b7655e9 commit 4e2415c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,16 @@ public void removeUnsupportedDataTypes_lists() {

Assert.assertTrue(UtilsInternalLimits.removeUnsupportedDataTypes(segmentation, Mockito.mock(ModuleLog.class)));

Assert.assertEquals(6, segmentation.size());
Assert.assertEquals(9, segmentation.size());
Assert.assertEquals(aa1, segmentation.get("aa1"));
Assert.assertEquals(aa2, segmentation.get("aa2"));
Assert.assertEquals(aa3, segmentation.get("aa3"));
Assert.assertEquals(aa4, segmentation.get("aa4"));
Assert.assertEquals(aa5, segmentation.get("aa5"));
Assert.assertEquals(aa6, segmentation.get("aa6"));
Assert.assertEquals(Arrays.asList(1, 2, "ABC", true, 3.3d, 4.4f, 5L), segmentation.get("aa7"));
Assert.assertEquals(new ArrayList<>(), segmentation.get("aa8"));
Assert.assertEquals(new ArrayList<>(), segmentation.get("aa9"));
}

@Test
Expand Down Expand Up @@ -425,7 +428,7 @@ public void removeUnsupportedDataTypes_jsonArray() {
Assert.assertEquals(9, segmentation.size());

Assert.assertTrue(UtilsInternalLimits.removeUnsupportedDataTypes(segmentation, Mockito.mock(ModuleLog.class)));
Assert.assertEquals(8, segmentation.size());
Assert.assertEquals(9, segmentation.size());
Assert.assertEquals(empty, segmentation.get("empty"));
Assert.assertEquals(arrInt, segmentation.get("arrInt"));
Assert.assertEquals(arrStr, segmentation.get("arrStr"));
Expand All @@ -434,6 +437,7 @@ public void removeUnsupportedDataTypes_jsonArray() {
Assert.assertEquals(arrFloat, segmentation.get("arrFloat"));
Assert.assertEquals(arrLong, segmentation.get("arrLong"));
Assert.assertEquals(arrObj, segmentation.get("arrObj"));
Assert.assertEquals(new JSONArray(), segmentation.get("arrObjUltra"));
}

@Test
Expand Down
47 changes: 31 additions & 16 deletions sdk/src/main/java/ly/count/android/sdk/UtilsInternalLimits.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ static boolean removeUnsupportedDataTypes(@NonNull Map<String, Object> data, @No
assert data != null;

StringBuilder removedKeys = new StringBuilder();
Map<String, Object> gonnaReplace = new ConcurrentHashMap<>();

for (Iterator<Map.Entry<String, Object>> it = data.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, Object> entry = it.next();
Expand All @@ -307,9 +308,37 @@ static boolean removeUnsupportedDataTypes(@NonNull Map<String, Object> data, @No
//found unsupported data type or null key or value, removing
it.remove();
removedKeys.append("key:[").append(key).append("] value:[").append(value).append("] type:[").append(value == null ? "null" : value.getClass().getSimpleName()).append("] ,");
} else if (value instanceof List) {
List<?> list = (List<?>) value;
list = new ArrayList<>(list);
int a = list.size();
for (int i = 0; i < a; i++) {
Object element = list.get(i);
if (!isSupportedDataTypeBasic(element)) {
removedKeys.append("from_list").append(list).append("index:[").append(i).append("] value:[").append(element).append("] type:[").append(element == null ? "null" : element.getClass().getSimpleName()).append("] ,");
list.remove(i);
i--;
a--;
}
}
gonnaReplace.put(key, list);
} else if (value instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) value;
int a = jsonArray.length();
for (int i = 0; i < a; i++) {
Object element = jsonArray.opt(i);
if (!isSupportedDataTypeBasic(element)) {
removedKeys.append("from_list").append(jsonArray).append("index:[").append(i).append("] value:[").append(element).append("] type:[").append(element == null ? "null" : element.getClass().getSimpleName()).append("] ,");
jsonArray.remove(i);
i--;
a--;
}
}
gonnaReplace.put(key, jsonArray);
}
}
String removedKeysStr = removedKeys.toString();
data.putAll(gonnaReplace);

if (!removedKeysStr.isEmpty()) {
L.w("[UtilsInternalLimits] removeUnsupportedDataTypes, removed " + removedKeysStr + " from the provided data map.");
Expand Down Expand Up @@ -359,34 +388,20 @@ private static boolean isSupportedDataTypeBasic(@Nullable Object value) {
* - User profile custom properties
* - User profile custom properties modifiers
* - Feedback widgets' results
* This function also removes unsupported data types inside the collections
*
* @param value to check
* @return true if the value is a supported data type
*/
static boolean isSupportedDataType(@Nullable Object value) {
if (isSupportedDataTypeBasic(value)) {
return true;
} else if (value instanceof List) {
List<?> list = (List<?>) value;
for (Object element : list) {
if (!isSupportedDataTypeBasic(element)) {
return false;
}
}
} else if (value instanceof List || value instanceof JSONArray) {
return true;
} else if (value != null && value.getClass().isArray()) {
Class<?> componentType = value.getClass().getComponentType();
return componentType == String.class || componentType == Integer.class || componentType == Double.class || componentType == Boolean.class || componentType == Float.class || componentType == Long.class
|| componentType == int.class || componentType == double.class || componentType == boolean.class || componentType == float.class || componentType == long.class;
} else if (value instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) value;
for (int i = 0; i < jsonArray.length(); i++) {
Object element = jsonArray.opt(i);
if (!isSupportedDataTypeBasic(element)) {
return false;
}
}
return true;
}
return false;
}
Expand Down

0 comments on commit 4e2415c

Please sign in to comment.