@@ -6,6 +6,8 @@ import com.segment.analytics.kotlin.core.utilities.getInt
66import com.segment.analytics.kotlin.core.utilities.getMapSet
77import com.segment.analytics.kotlin.core.utilities.getString
88import com.segment.analytics.kotlin.core.utilities.getStringSet
9+ import com.segment.analytics.kotlin.core.utilities.mapTransform
10+ import com.segment.analytics.kotlin.core.utilities.toContent
911import com.segment.analytics.kotlin.core.utilities.transformKeys
1012import com.segment.analytics.kotlin.core.utilities.transformValues
1113import kotlinx.serialization.json.*
@@ -337,4 +339,167 @@ class JSONTests {
337339 assertEquals(" M" , getString(" Mr. Freeze" ))
338340 }
339341 }
342+
343+ @Test
344+ fun `can map keys + nested keys using mapTransform` () {
345+ val keyMapper = mapOf (
346+ " item" to " \$ item" ,
347+ " phone" to " \$ phone" ,
348+ " name" to " \$ name" ,
349+ )
350+ val map = buildJsonObject {
351+ put(" company" , buildJsonObject {
352+ put(" phone" , " 123-456-7890" )
353+ put(" name" , " Wayne Industries" )
354+ })
355+ put(" family" , buildJsonArray {
356+ add(buildJsonObject { put(" name" , " Mary" ) })
357+ add(buildJsonObject { put(" name" , " Thomas" ) })
358+ })
359+ put(" name" , " Bruce" )
360+ put(" last_name" , " wayne" )
361+ put(" item" , " Grapple" )
362+ }
363+ val newMap = map.mapTransform(keyMapper)
364+ with (newMap) {
365+ assertTrue(containsKey(" \$ name" ))
366+ assertTrue(containsKey(" \$ item" ))
367+ assertTrue(containsKey(" last_name" ))
368+ with (get(" company" )!! .jsonObject) {
369+ assertTrue(containsKey(" \$ phone" ))
370+ assertTrue(containsKey(" \$ name" ))
371+ }
372+ with (get(" family" )!! .jsonArray) {
373+ assertTrue(get(0 ).jsonObject.containsKey(" \$ name" ))
374+ assertTrue(get(1 ).jsonObject.containsKey(" \$ name" ))
375+ }
376+ }
377+ }
378+
379+ @Test
380+ fun `can transform values using mapTransform` () {
381+ val map = buildJsonObject {
382+ put(" company" , buildJsonObject {
383+ put(" phone" , " 123-456-7890" )
384+ put(" name" , " Wayne Industries" )
385+ })
386+ put(" family" , buildJsonArray {
387+ add(buildJsonObject { put(" name" , " Mary" ) })
388+ add(buildJsonObject { put(" name" , " Thomas" ) })
389+ })
390+ put(" name" , " Bruce" )
391+ put(" last_name" , " wayne" )
392+ put(" item" , " Grapple" )
393+ }
394+ val newMap = map.mapTransform(emptyMap()) { newKey, value ->
395+ var newVal = value
396+ if (newKey == " phone" ) {
397+ val foo = value.jsonPrimitive.toContent()
398+ if (foo is String ) {
399+ newVal = JsonPrimitive (foo.replace(" -" , " " ))
400+ }
401+ }
402+ newVal
403+ }
404+ with (newMap) {
405+ with (get(" company" )!! .jsonObject) {
406+ assertEquals(" 1234567890" , getString(" phone" ))
407+ }
408+ }
409+ }
410+
411+ @Test
412+ fun `can map keys + transform values using mapTransform` () {
413+ val keyMapper = mapOf (
414+ " item" to " \$ item" ,
415+ " phone" to " \$ phone" ,
416+ " name" to " \$ name" ,
417+ )
418+ val map = buildJsonObject {
419+ put(" company" , buildJsonObject {
420+ put(" phone" , " 123-456-7890" )
421+ put(" name" , " Wayne Industries" )
422+ })
423+ put(" family" , buildJsonArray {
424+ add(buildJsonObject { put(" name" , " Mary" ) })
425+ add(buildJsonObject { put(" name" , " Thomas" ) })
426+ })
427+ put(" name" , " Bruce" )
428+ put(" last_name" , " wayne" )
429+ put(" item" , " Grapple" )
430+ }
431+ val newMap = map.mapTransform(keyMapper) { newKey, value ->
432+ var newVal = value
433+ if (newKey == " \$ phone" ) {
434+ val foo = value.jsonPrimitive.toContent()
435+ if (foo is String ) {
436+ newVal = JsonPrimitive (foo.replace(" -" , " " ))
437+ }
438+ }
439+ newVal
440+ }
441+ with (newMap) {
442+ assertTrue(containsKey(" \$ name" ))
443+ assertTrue(containsKey(" \$ item" ))
444+ assertTrue(containsKey(" last_name" ))
445+ with (get(" company" )!! .jsonObject) {
446+ assertTrue(containsKey(" \$ phone" ))
447+ assertTrue(containsKey(" \$ name" ))
448+ assertEquals(" 1234567890" , getString(" \$ phone" ))
449+ }
450+ with (get(" family" )!! .jsonArray) {
451+ assertTrue(get(0 ).jsonObject.containsKey(" \$ name" ))
452+ assertTrue(get(1 ).jsonObject.containsKey(" \$ name" ))
453+ }
454+ }
455+ }
456+
457+ @Test
458+ fun `can map keys + transform values using mapTransform on JsonArray` () {
459+ val keyMapper = mapOf (
460+ " item" to " \$ item" ,
461+ " phone" to " \$ phone" ,
462+ " name" to " \$ name" ,
463+ )
464+ val list = buildJsonArray {
465+ add(buildJsonObject {
466+ put(" phone" , " 123-456-7890" )
467+ put(" name" , " Wayne Industries" )
468+ })
469+ add(buildJsonArray {
470+ add(buildJsonObject { put(" name" , " Mary" ) })
471+ add(buildJsonObject { put(" name" , " Thomas" ) })
472+ })
473+ add(buildJsonObject {
474+ put(" name" , " Bruce" )
475+ put(" last_name" , " wayne" )
476+ put(" item" , " Grapple" )
477+ })
478+ }
479+ val newList = list.mapTransform(keyMapper) { newKey, value ->
480+ var newVal = value
481+ if (newKey == " \$ phone" ) {
482+ val foo = value.jsonPrimitive.toContent()
483+ if (foo is String ) {
484+ newVal = JsonPrimitive (foo.replace(" -" , " " ))
485+ }
486+ }
487+ newVal
488+ }
489+ with (newList) {
490+ get(0 ).jsonObject.let {
491+ assertTrue(it.containsKey(" \$ phone" ))
492+ assertTrue(it.containsKey(" \$ name" ))
493+ }
494+ get(1 ).jsonArray.let {
495+ assertEquals(buildJsonObject { put(" \$ name" , " Mary" ) }, it[0 ])
496+ assertEquals(buildJsonObject { put(" \$ name" , " Thomas" ) }, it[1 ])
497+ }
498+ get(2 ).jsonObject.let {
499+ assertTrue(it.containsKey(" \$ name" ))
500+ assertTrue(it.containsKey(" \$ item" ))
501+ assertTrue(it.containsKey(" last_name" ))
502+ }
503+ }
504+ }
340505}
0 commit comments