Skip to content

Commit 4e2415c

Browse files
authored
Merge pull request #415 from Countly/fix_array_removal
fix: move list and json array unsupported removals to ios way
2 parents 96b8b1d + b7655e9 commit 4e2415c

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

sdk/src/androidTest/java/ly/count/android/sdk/UtilsInternalLimitsTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,16 @@ public void removeUnsupportedDataTypes_lists() {
352352

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

355-
Assert.assertEquals(6, segmentation.size());
355+
Assert.assertEquals(9, segmentation.size());
356356
Assert.assertEquals(aa1, segmentation.get("aa1"));
357357
Assert.assertEquals(aa2, segmentation.get("aa2"));
358358
Assert.assertEquals(aa3, segmentation.get("aa3"));
359359
Assert.assertEquals(aa4, segmentation.get("aa4"));
360360
Assert.assertEquals(aa5, segmentation.get("aa5"));
361361
Assert.assertEquals(aa6, segmentation.get("aa6"));
362+
Assert.assertEquals(Arrays.asList(1, 2, "ABC", true, 3.3d, 4.4f, 5L), segmentation.get("aa7"));
363+
Assert.assertEquals(new ArrayList<>(), segmentation.get("aa8"));
364+
Assert.assertEquals(new ArrayList<>(), segmentation.get("aa9"));
362365
}
363366

364367
@Test
@@ -425,7 +428,7 @@ public void removeUnsupportedDataTypes_jsonArray() {
425428
Assert.assertEquals(9, segmentation.size());
426429

427430
Assert.assertTrue(UtilsInternalLimits.removeUnsupportedDataTypes(segmentation, Mockito.mock(ModuleLog.class)));
428-
Assert.assertEquals(8, segmentation.size());
431+
Assert.assertEquals(9, segmentation.size());
429432
Assert.assertEquals(empty, segmentation.get("empty"));
430433
Assert.assertEquals(arrInt, segmentation.get("arrInt"));
431434
Assert.assertEquals(arrStr, segmentation.get("arrStr"));
@@ -434,6 +437,7 @@ public void removeUnsupportedDataTypes_jsonArray() {
434437
Assert.assertEquals(arrFloat, segmentation.get("arrFloat"));
435438
Assert.assertEquals(arrLong, segmentation.get("arrLong"));
436439
Assert.assertEquals(arrObj, segmentation.get("arrObj"));
440+
Assert.assertEquals(new JSONArray(), segmentation.get("arrObjUltra"));
437441
}
438442

439443
@Test

sdk/src/main/java/ly/count/android/sdk/UtilsInternalLimits.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ static boolean removeUnsupportedDataTypes(@NonNull Map<String, Object> data, @No
297297
assert data != null;
298298

299299
StringBuilder removedKeys = new StringBuilder();
300+
Map<String, Object> gonnaReplace = new ConcurrentHashMap<>();
300301

301302
for (Iterator<Map.Entry<String, Object>> it = data.entrySet().iterator(); it.hasNext(); ) {
302303
Map.Entry<String, Object> entry = it.next();
@@ -307,9 +308,37 @@ static boolean removeUnsupportedDataTypes(@NonNull Map<String, Object> data, @No
307308
//found unsupported data type or null key or value, removing
308309
it.remove();
309310
removedKeys.append("key:[").append(key).append("] value:[").append(value).append("] type:[").append(value == null ? "null" : value.getClass().getSimpleName()).append("] ,");
311+
} else if (value instanceof List) {
312+
List<?> list = (List<?>) value;
313+
list = new ArrayList<>(list);
314+
int a = list.size();
315+
for (int i = 0; i < a; i++) {
316+
Object element = list.get(i);
317+
if (!isSupportedDataTypeBasic(element)) {
318+
removedKeys.append("from_list").append(list).append("index:[").append(i).append("] value:[").append(element).append("] type:[").append(element == null ? "null" : element.getClass().getSimpleName()).append("] ,");
319+
list.remove(i);
320+
i--;
321+
a--;
322+
}
323+
}
324+
gonnaReplace.put(key, list);
325+
} else if (value instanceof JSONArray) {
326+
JSONArray jsonArray = (JSONArray) value;
327+
int a = jsonArray.length();
328+
for (int i = 0; i < a; i++) {
329+
Object element = jsonArray.opt(i);
330+
if (!isSupportedDataTypeBasic(element)) {
331+
removedKeys.append("from_list").append(jsonArray).append("index:[").append(i).append("] value:[").append(element).append("] type:[").append(element == null ? "null" : element.getClass().getSimpleName()).append("] ,");
332+
jsonArray.remove(i);
333+
i--;
334+
a--;
335+
}
336+
}
337+
gonnaReplace.put(key, jsonArray);
310338
}
311339
}
312340
String removedKeysStr = removedKeys.toString();
341+
data.putAll(gonnaReplace);
313342

314343
if (!removedKeysStr.isEmpty()) {
315344
L.w("[UtilsInternalLimits] removeUnsupportedDataTypes, removed " + removedKeysStr + " from the provided data map.");
@@ -359,34 +388,20 @@ private static boolean isSupportedDataTypeBasic(@Nullable Object value) {
359388
* - User profile custom properties
360389
* - User profile custom properties modifiers
361390
* - Feedback widgets' results
391+
* This function also removes unsupported data types inside the collections
362392
*
363393
* @param value to check
364394
* @return true if the value is a supported data type
365395
*/
366396
static boolean isSupportedDataType(@Nullable Object value) {
367397
if (isSupportedDataTypeBasic(value)) {
368398
return true;
369-
} else if (value instanceof List) {
370-
List<?> list = (List<?>) value;
371-
for (Object element : list) {
372-
if (!isSupportedDataTypeBasic(element)) {
373-
return false;
374-
}
375-
}
399+
} else if (value instanceof List || value instanceof JSONArray) {
376400
return true;
377401
} else if (value != null && value.getClass().isArray()) {
378402
Class<?> componentType = value.getClass().getComponentType();
379403
return componentType == String.class || componentType == Integer.class || componentType == Double.class || componentType == Boolean.class || componentType == Float.class || componentType == Long.class
380404
|| componentType == int.class || componentType == double.class || componentType == boolean.class || componentType == float.class || componentType == long.class;
381-
} else if (value instanceof JSONArray) {
382-
JSONArray jsonArray = (JSONArray) value;
383-
for (int i = 0; i < jsonArray.length(); i++) {
384-
Object element = jsonArray.opt(i);
385-
if (!isSupportedDataTypeBasic(element)) {
386-
return false;
387-
}
388-
}
389-
return true;
390405
}
391406
return false;
392407
}

0 commit comments

Comments
 (0)