Skip to content

Commit c019983

Browse files
authored
Implement string conversion for DynamicRealmObject dicts and sets fields (#7482)
1 parent eb9611f commit c019983

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

realm/realm-library/src/androidTest/kotlin/io/realm/ManagedDictionaryTesters.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,25 @@ class ManagedDictionaryTester<T : Any>(
449449

450450
assertEquals(1, dynamicObject.get<RealmDictionary<T>>(dictionaryFieldName).size)
451451

452+
// Validate that dict is properly represented as a String
453+
validateToString(dynamicObject, dynamicDictionary)
454+
452455
dynamicRealm.close()
453456
}
454457

458+
private fun validateToString(dynamicObject: DynamicRealmObject, dynamicDictionary: RealmDictionary<*>) {
459+
val type = when (dictionaryFieldClass.simpleName) {
460+
"Byte", "Short", "Integer" -> "Long"
461+
else -> dictionaryFieldClass.simpleName
462+
}
463+
464+
val expectedDictionaryString = "${dictionaryFieldName}:RealmDictionary<$type>[${dynamicDictionary.size}]"
465+
assertTrue(
466+
dynamicObject.toString().contains(expectedDictionaryString),
467+
"DynamicRealmObject does not contain expected RealmDictionary string: $expectedDictionaryString"
468+
)
469+
}
470+
455471
private fun doObjectDynamicTest() {
456472
// Create a dictionary from a immutable schema context
457473
val dictionary = initAndAssert()
@@ -506,6 +522,9 @@ class ManagedDictionaryTester<T : Any>(
506522

507523
assertEquals(1, dynamicObject.get<RealmDictionary<T>>(dictionaryFieldName).size)
508524

525+
// Validate that dict is properly represented as a String
526+
validateToString(dynamicObject, dynamicDictionary)
527+
509528
dynamicRealm.close()
510529
}
511530

realm/realm-library/src/androidTest/kotlin/io/realm/ManagedSetTester.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,25 @@ class ManagedSetTester<T : Any>(
387387
assertSetContainsSet(listOf(notPresentValue), dynamicSet)
388388
assertEquals(1, dynamicObject.get<RealmSet<T>>(setFieldName).size)
389389

390+
// Validate that set is properly represented as a String
391+
validateToString(dynamicObject, dynamicSet)
392+
390393
dynamicRealm.close()
391394
}
392395

396+
fun validateToString(dynamicObject: DynamicRealmObject, dynamicSet: RealmSet<*>) {
397+
val type = when (setFieldClass.simpleName) {
398+
"Byte", "Short", "Integer" -> "Long"
399+
else -> setFieldClass.simpleName
400+
}
401+
402+
val expectedSetString = "${setFieldName}:RealmSet<$type>[${dynamicSet.size}]"
403+
assertTrue(
404+
dynamicObject.toString().contains(expectedSetString),
405+
"DynamicRealmObject does not contain expected RealmSet string: $expectedSetString"
406+
)
407+
}
408+
393409
override fun insert() {
394410
doInsertTest(initializedSet)
395411
}

realm/realm-library/src/androidTest/kotlin/io/realm/RealmModelManagedSetTester.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ class RealmModelManagedSetTester<T : Any>(
193193

194194
assertEquals(1, dynamicObject.getRealmSet(setFieldName, setFieldClass).size)
195195

196+
// Validate that set is properly represented as a String
197+
managedTester.validateToString(dynamicObject, dynamicSet)
198+
196199
dynamicRealm.close()
197200
}
198201

@@ -245,6 +248,9 @@ class RealmModelManagedSetTester<T : Any>(
245248

246249
assertEquals(1, dynamicObject.get<RealmSet<T>>(setFieldName).size)
247250

251+
// Validate that set is properly represented as a String
252+
managedTester.validateToString(dynamicObject, dynamicSet)
253+
248254
dynamicRealm.close()
249255
}
250256

realm/realm-library/src/main/java/io/realm/DynamicRealmObject.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1767,10 +1767,11 @@ public String toString() {
17671767
? "null"
17681768
: proxyState.getRow$realm().getTable().getLinkTarget(columnKey).getClassName());
17691769
break;
1770-
case LIST:
1770+
case LIST: {
17711771
String targetClassName = proxyState.getRow$realm().getTable().getLinkTarget(columnKey).getClassName();
17721772
sb.append(String.format(Locale.US, "RealmList<%s>[%s]", targetClassName, proxyState.getRow$realm().getModelList(columnKey).size()));
17731773
break;
1774+
}
17741775
case INTEGER_LIST:
17751776
sb.append(String.format(Locale.US, "RealmList<Long>[%s]", proxyState.getRow$realm().getValueList(columnKey, type).size()));
17761777
break;
@@ -1804,6 +1805,82 @@ public String toString() {
18041805
case MIXED_LIST:
18051806
sb.append(String.format(Locale.US, "RealmList<RealmAny>[%s]", proxyState.getRow$realm().getValueList(columnKey, type).size()));
18061807
break;
1808+
case STRING_TO_INTEGER_MAP:
1809+
sb.append(String.format(Locale.US, "RealmDictionary<Long>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1810+
break;
1811+
case STRING_TO_BOOLEAN_MAP:
1812+
sb.append(String.format(Locale.US, "RealmDictionary<Boolean>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1813+
break;
1814+
case STRING_TO_STRING_MAP:
1815+
sb.append(String.format(Locale.US, "RealmDictionary<String>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1816+
break;
1817+
case STRING_TO_BINARY_MAP:
1818+
sb.append(String.format(Locale.US, "RealmDictionary<byte[]>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1819+
break;
1820+
case STRING_TO_DATE_MAP:
1821+
sb.append(String.format(Locale.US, "RealmDictionary<Date>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1822+
break;
1823+
case STRING_TO_FLOAT_MAP:
1824+
sb.append(String.format(Locale.US, "RealmDictionary<Float>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1825+
break;
1826+
case STRING_TO_DOUBLE_MAP:
1827+
sb.append(String.format(Locale.US, "RealmDictionary<Double>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1828+
break;
1829+
case STRING_TO_DECIMAL128_MAP:
1830+
sb.append(String.format(Locale.US, "RealmDictionary<Decimal128>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1831+
break;
1832+
case STRING_TO_OBJECT_ID_MAP:
1833+
sb.append(String.format(Locale.US, "RealmDictionary<ObjectId>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1834+
break;
1835+
case STRING_TO_UUID_MAP:
1836+
sb.append(String.format(Locale.US, "RealmDictionary<UUID>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1837+
break;
1838+
case STRING_TO_MIXED_MAP:
1839+
sb.append(String.format(Locale.US, "RealmDictionary<RealmAny>[%s]", proxyState.getRow$realm().getValueMap(columnKey, type).size()));
1840+
break;
1841+
case STRING_TO_LINK_MAP: {
1842+
String targetClassName = proxyState.getRow$realm().getTable().getLinkTarget(columnKey).getClassName();
1843+
sb.append(String.format(Locale.US, "RealmDictionary<%s>[%s]", targetClassName, proxyState.getRow$realm().getModelMap(columnKey).size()));
1844+
break;
1845+
}
1846+
case INTEGER_SET:
1847+
sb.append(String.format(Locale.US, "RealmSet<Long>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1848+
break;
1849+
case BOOLEAN_SET:
1850+
sb.append(String.format(Locale.US, "RealmSet<Boolean>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1851+
break;
1852+
case STRING_SET:
1853+
sb.append(String.format(Locale.US, "RealmSet<String>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1854+
break;
1855+
case BINARY_SET:
1856+
sb.append(String.format(Locale.US, "RealmSet<byte[]>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1857+
break;
1858+
case DATE_SET:
1859+
sb.append(String.format(Locale.US, "RealmSet<Date>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1860+
break;
1861+
case FLOAT_SET:
1862+
sb.append(String.format(Locale.US, "RealmSet<Float>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1863+
break;
1864+
case DOUBLE_SET:
1865+
sb.append(String.format(Locale.US, "RealmSet<Double>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1866+
break;
1867+
case DECIMAL128_SET:
1868+
sb.append(String.format(Locale.US, "RealmSet<Decimal128>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1869+
break;
1870+
case OBJECT_ID_SET:
1871+
sb.append(String.format(Locale.US, "RealmSet<ObjectId>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1872+
break;
1873+
case UUID_SET:
1874+
sb.append(String.format(Locale.US, "RealmSet<UUID>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1875+
break;
1876+
case MIXED_SET:
1877+
sb.append(String.format(Locale.US, "RealmSet<RealmAny>[%s]", proxyState.getRow$realm().getValueSet(columnKey, type).size()));
1878+
break;
1879+
case LINK_SET: {
1880+
String targetClassName = proxyState.getRow$realm().getTable().getLinkTarget(columnKey).getClassName();
1881+
sb.append(String.format(Locale.US, "RealmSet<%s>[%s]", targetClassName, proxyState.getRow$realm().getModelSet(columnKey).size()));
1882+
break;
1883+
}
18071884
default:
18081885
sb.append("?");
18091886
break;

0 commit comments

Comments
 (0)