Skip to content

Commit 0309809

Browse files
committed
NAVAND-713: refresh unrecognized annotations
1 parent c1ee242 commit 0309809

File tree

2 files changed

+221
-38
lines changed

2 files changed

+221
-38
lines changed

libnavigation-base/src/main/java/com/mapbox/navigation/base/internal/route/AnnotationsRefresher.kt

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mapbox.navigation.base.internal.route
22

3+
import com.google.gson.JsonArray
4+
import com.google.gson.JsonElement
35
import com.mapbox.api.directions.v5.models.LegAnnotation
46
import com.mapbox.navigation.utils.internal.logE
57
import kotlin.math.min
@@ -46,8 +48,26 @@ internal object AnnotationsRefresher {
4648
newAnnotation,
4749
startingLegGeometryIndex,
4850
) { maxspeed() }
49-
// unrecognized properties migrate from new annotation
50-
return (newAnnotation?.toBuilder() ?: LegAnnotation.builder())
51+
val unrecognizedProperties = oldAnnotation.unrecognizedPropertiesNames
52+
.union(newAnnotation?.unrecognizedPropertiesNames ?: emptySet())
53+
.associateNonNullValuesWith { propertyName ->
54+
if (oldAnnotation.getUnrecognizedProperty(propertyName)?.isJsonArray == false ||
55+
newAnnotation?.getUnrecognizedProperty(propertyName)?.isJsonArray == false) {
56+
null
57+
} else {
58+
val extractor = { annotation: LegAnnotation ->
59+
annotation.getUnrecognizedProperty(propertyName)?.asJsonArray?.toList()
60+
}
61+
mergeAnnotationProperty(
62+
oldAnnotation,
63+
newAnnotation,
64+
startingLegGeometryIndex,
65+
extractor
66+
)?.toJsonArray()
67+
}
68+
}.ifEmpty { null }
69+
return LegAnnotation.builder()
70+
.unrecognizedJsonProperties(unrecognizedProperties)
5171
.congestion(congestion)
5272
.congestionNumeric(congestionNumeric)
5373
.maxspeed(maxSpeed)
@@ -84,4 +104,21 @@ internal object AnnotationsRefresher {
84104

85105
return result
86106
}
107+
108+
private fun List<JsonElement>.toJsonArray(): JsonArray {
109+
return JsonArray().also { array ->
110+
forEach { array.add(it) }
111+
}
112+
}
113+
114+
private fun <T, R> Iterable<T>.associateNonNullValuesWith(block: (T) -> R?): Map<T, R> {
115+
val map = hashMapOf<T, R>()
116+
forEach { key ->
117+
val value = block(key)
118+
if (value != null) {
119+
map[key] = value
120+
}
121+
}
122+
return map
123+
}
87124
}

libnavigation-base/src/test/java/com/mapbox/navigation/base/internal/route/AnnotationsRefresherTest.kt

Lines changed: 182 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.mapbox.navigation.base.internal.route
22

3+
import com.google.gson.JsonArray
4+
import com.google.gson.JsonElement
5+
import com.google.gson.JsonPrimitive
36
import com.mapbox.api.directions.v5.models.LegAnnotation
47
import com.mapbox.api.directions.v5.models.MaxSpeed
58
import com.mapbox.navigation.testing.LoggingFrontendTestRule
@@ -203,6 +206,14 @@ class AnnotationsRefresherTest(
203206
),
204207
arrayOf(
205208
LegAnnotation.builder()
209+
.unrecognizedJsonProperties(
210+
mapOf(
211+
"state_of_charge" to listOf(79, 78, 77, 76, 75)
212+
.toJsonArray(::JsonPrimitive),
213+
"custom_annotation" to listOf("a", "b", "c", "d", "e")
214+
.toJsonArray(::JsonPrimitive),
215+
)
216+
)
206217
.congestionNumeric(listOf(1, 2, 3, 4, 5))
207218
.congestion(listOf("c1", "c2", "c3", "c4", "c5"))
208219
.distance(listOf(1.2, 3.4, 5.6, 7.8, 9.0))
@@ -211,6 +222,14 @@ class AnnotationsRefresherTest(
211222
.maxspeed(List(5) { MaxSpeed.builder().speed(it * 10).unit("mph").build() })
212223
.build(),
213224
LegAnnotation.builder()
225+
.unrecognizedJsonProperties(
226+
mapOf(
227+
"state_of_charge" to listOf(70, 69, 68, 67, 66)
228+
.toJsonArray(::JsonPrimitive),
229+
"custom_annotation" to listOf("f", "g", "h", "i", "j")
230+
.toJsonArray(::JsonPrimitive),
231+
)
232+
)
214233
.congestionNumeric(listOf(6, 7, 8, 9, 10))
215234
.congestion(listOf("c6", "c7", "c8", "c9", "c10"))
216235
.distance(listOf(2.1, 4.3, 6.5, 8.7, 0.9))
@@ -224,6 +243,14 @@ class AnnotationsRefresherTest(
224243
.build(),
225244
0,
226245
LegAnnotation.builder()
246+
.unrecognizedJsonProperties(
247+
mapOf(
248+
"state_of_charge" to listOf(70, 69, 68, 67, 66)
249+
.toJsonArray(::JsonPrimitive),
250+
"custom_annotation" to listOf("f", "g", "h", "i", "j")
251+
.toJsonArray(::JsonPrimitive),
252+
)
253+
)
227254
.congestionNumeric(listOf(6, 7, 8, 9, 10))
228255
.congestion(listOf("c6", "c7", "c8", "c9", "c10"))
229256
.distance(listOf(2.1, 4.3, 6.5, 8.7, 0.9))
@@ -239,6 +266,14 @@ class AnnotationsRefresherTest(
239266
),
240267
arrayOf(
241268
LegAnnotation.builder()
269+
.unrecognizedJsonProperties(
270+
mapOf(
271+
"state_of_charge" to listOf(79, 78, 77, 76, 75)
272+
.toJsonArray(::JsonPrimitive),
273+
"custom_annotation" to listOf("a", "b", "c", "d", "e")
274+
.toJsonArray(::JsonPrimitive),
275+
)
276+
)
242277
.congestionNumeric(listOf(1, 2, 3, 4, 5))
243278
.congestion(listOf("c1", "c2", "c3", "c4", "c5"))
244279
.distance(listOf(1.2, 3.4, 5.6, 7.8, 9.0))
@@ -251,6 +286,14 @@ class AnnotationsRefresherTest(
251286
)
252287
.build(),
253288
LegAnnotation.builder()
289+
.unrecognizedJsonProperties(
290+
mapOf(
291+
"state_of_charge" to listOf(67, 66)
292+
.toJsonArray(::JsonPrimitive),
293+
"custom_annotation" to listOf("i", "j")
294+
.toJsonArray(::JsonPrimitive),
295+
)
296+
)
254297
.congestionNumeric(listOf(9, 10))
255298
.congestion(listOf("c9", "c10"))
256299
.distance(listOf(8.7, 0.9))
@@ -264,6 +307,14 @@ class AnnotationsRefresherTest(
264307
.build(),
265308
3,
266309
LegAnnotation.builder()
310+
.unrecognizedJsonProperties(
311+
mapOf(
312+
"state_of_charge" to listOf(79, 78, 77, 67, 66)
313+
.toJsonArray(::JsonPrimitive),
314+
"custom_annotation" to listOf("a", "b", "c", "i", "j")
315+
.toJsonArray(::JsonPrimitive),
316+
)
317+
)
267318
.congestionNumeric(listOf(1, 2, 3, 9, 10))
268319
.congestion(listOf("c1", "c2", "c3", "c9", "c10"))
269320
.distance(listOf(1.2, 3.4, 5.6, 8.7, 0.9))
@@ -283,6 +334,14 @@ class AnnotationsRefresherTest(
283334
),
284335
arrayOf(
285336
LegAnnotation.builder()
337+
.unrecognizedJsonProperties(
338+
mapOf(
339+
"state_of_charge" to listOf(79, 78, 77, 76, 75)
340+
.toJsonArray(::JsonPrimitive),
341+
"custom_annotation" to listOf("a", "b", "c", "d", "e")
342+
.toJsonArray(::JsonPrimitive),
343+
)
344+
)
286345
.congestionNumeric(listOf(1, 2, 3, 4, 5))
287346
.congestion(listOf("c1", "c2", "c3", "c4", "c5"))
288347
.distance(listOf(1.2, 3.4, 5.6, 7.8, 9.0))
@@ -295,6 +354,7 @@ class AnnotationsRefresherTest(
295354
)
296355
.build(),
297356
LegAnnotation.builder()
357+
.unrecognizedJsonProperties(emptyMap())
298358
.congestionNumeric(emptyList())
299359
.congestion(emptyList())
300360
.distance(emptyList())
@@ -304,6 +364,14 @@ class AnnotationsRefresherTest(
304364
.build(),
305365
5,
306366
LegAnnotation.builder()
367+
.unrecognizedJsonProperties(
368+
mapOf(
369+
"state_of_charge" to listOf(79, 78, 77, 76, 75)
370+
.toJsonArray(::JsonPrimitive),
371+
"custom_annotation" to listOf("a", "b", "c", "d", "e")
372+
.toJsonArray(::JsonPrimitive),
373+
)
374+
)
307375
.congestionNumeric(listOf(1, 2, 3, 4, 5))
308376
.congestion(listOf("c1", "c2", "c3", "c4", "c5"))
309377
.distance(listOf(1.2, 3.4, 5.6, 7.8, 9.0))
@@ -320,6 +388,14 @@ class AnnotationsRefresherTest(
320388
),
321389
arrayOf(
322390
LegAnnotation.builder()
391+
.unrecognizedJsonProperties(
392+
mapOf(
393+
"state_of_charge" to listOf(79, 78, 77, 76, 75)
394+
.toJsonArray(::JsonPrimitive),
395+
"custom_annotation" to listOf("a", "b", "c", "d", "e")
396+
.toJsonArray(::JsonPrimitive),
397+
)
398+
)
323399
.congestionNumeric(listOf(1, 2, 3, 4, 5))
324400
.congestion(listOf("c1", "c2", "c3", "c4", "c5"))
325401
.distance(listOf(1.2, 3.4, 5.6, 7.8, 9.0))
@@ -334,6 +410,14 @@ class AnnotationsRefresherTest(
334410
defaultAnnotation,
335411
5,
336412
LegAnnotation.builder()
413+
.unrecognizedJsonProperties(
414+
mapOf(
415+
"state_of_charge" to listOf(79, 78, 77, 76, 75)
416+
.toJsonArray(::JsonPrimitive),
417+
"custom_annotation" to listOf("a", "b", "c", "d", "e")
418+
.toJsonArray(::JsonPrimitive),
419+
)
420+
)
337421
.congestionNumeric(listOf(1, 2, 3, 4, 5))
338422
.congestion(listOf("c1", "c2", "c3", "c4", "c5"))
339423
.distance(listOf(1.2, 3.4, 5.6, 7.8, 9.0))
@@ -349,50 +433,109 @@ class AnnotationsRefresherTest(
349433
"Old annotation properties are used before current index."
350434
),
351435
arrayOf(
352-
LegAnnotation.fromJson("{ \"my_key1\": \"my_value1\" }"),
353-
LegAnnotation.fromJson("{ \"my_key2\": \"my_value2\" }"),
354-
3,
355-
LegAnnotation.fromJson("{ \"my_key2\": \"my_value2\" }"),
356-
"Unrecognized properties migrate from new annotation."
436+
LegAnnotation.builder()
437+
.unrecognizedJsonProperties(
438+
mapOf(
439+
"state_of_charge" to JsonPrimitive("aaa"),
440+
"custom_annotation" to JsonPrimitive("bbb"),
441+
)
442+
)
443+
.build(),
444+
LegAnnotation.builder()
445+
.unrecognizedJsonProperties(
446+
mapOf(
447+
"state_of_charge" to listOf(79, 78, 77, 76, 75)
448+
.toJsonArray(::JsonPrimitive),
449+
"custom_annotation" to listOf("a", "b", "c", "d", "e")
450+
.toJsonArray(::JsonPrimitive),
451+
)
452+
)
453+
.build(),
454+
0,
455+
LegAnnotation.builder().build(),
456+
"Non list old annotation unrecognized properties are ignored."
357457
),
358458
arrayOf(
359-
LegAnnotation.fromJson("{ \"my_key1\": \"my_value1\" }"),
360-
LegAnnotation.fromJson(
361-
"{ \"my_key2\": \"my_value2\", " +
362-
"\"my_key3\": \"my_value3\" }"
363-
),
364-
3,
365-
LegAnnotation.fromJson(
366-
"{ \"my_key2\": \"my_value2\", " +
367-
"\"my_key3\": \"my_value3\" }"
368-
),
369-
"Old annotation has less unrecognized properties. " +
370-
"Unrecognized properties migrate from new annotation."
459+
LegAnnotation.builder()
460+
.unrecognizedJsonProperties(
461+
mapOf(
462+
"state_of_charge" to listOf(79, 78, 77, 76, 75)
463+
.toJsonArray(::JsonPrimitive),
464+
"custom_annotation" to listOf("a", "b", "c", "d", "e")
465+
.toJsonArray(::JsonPrimitive),
466+
)
467+
)
468+
.build(),
469+
LegAnnotation.builder()
470+
.unrecognizedJsonProperties(
471+
mapOf(
472+
"state_of_charge" to JsonPrimitive("aaa"),
473+
"custom_annotation" to JsonPrimitive("bbb"),
474+
)
475+
)
476+
.build(),
477+
5,
478+
LegAnnotation.builder().build(),
479+
"Non list new annotation unrecognized properties are ignored."
371480
),
372481
arrayOf(
373-
LegAnnotation.builder().build(),
374-
LegAnnotation.fromJson(
375-
"{ \"my_key2\": \"my_value2\", " +
376-
"\"my_key3\": \"my_value3\" }"
377-
),
482+
LegAnnotation.builder()
483+
.unrecognizedJsonProperties(
484+
mapOf(
485+
"state_of_charge" to listOf(10, 11, 12, 13, 14)
486+
.toJsonArray(::JsonPrimitive),
487+
"custom_annotation" to listOf("z", "y", "x", "w", "v")
488+
.toJsonArray(::JsonPrimitive),
489+
)
490+
)
491+
.build(),
492+
LegAnnotation.builder()
493+
.unrecognizedJsonProperties(
494+
mapOf(
495+
"state_of_charge2" to listOf(79, 78, 77, 76, 75)
496+
.toJsonArray(::JsonPrimitive),
497+
"custom_annotation2" to listOf("a", "b", "c", "d", "e")
498+
.toJsonArray(::JsonPrimitive),
499+
)
500+
)
501+
.build(),
378502
3,
379-
LegAnnotation.fromJson(
380-
"{ \"my_key2\": \"my_value2\", " +
381-
"\"my_key3\": \"my_value3\" }"
382-
),
383-
"Old annotation has no unrecognized properties. " +
384-
"Unrecognized properties migrate from new annotation."
503+
LegAnnotation.builder()
504+
.unrecognizedJsonProperties(
505+
mapOf(
506+
"state_of_charge" to listOf(10, 11, 12, 13, 14)
507+
.toJsonArray(::JsonPrimitive),
508+
"custom_annotation" to listOf("z", "y", "x", "w", "v")
509+
.toJsonArray(::JsonPrimitive),
510+
)
511+
)
512+
.build(),
513+
"Unrecognized properties keys don't match: old values are used."
385514
),
386515
arrayOf(
387-
LegAnnotation.fromJson(
388-
"{ \"my_key2\": \"my_value2\", " +
389-
"\"my_key3\": \"my_value3\" }"
390-
),
391-
LegAnnotation.fromJson("{ \"my_key1\": \"my_value1\" }"),
516+
LegAnnotation.builder()
517+
.unrecognizedJsonProperties(
518+
mapOf(
519+
"state_of_charge" to listOf(10, 11, 12, 13, 14)
520+
.toJsonArray(::JsonPrimitive),
521+
"custom_annotation" to listOf("z", "y", "x", "w", "v")
522+
.toJsonArray(::JsonPrimitive),
523+
)
524+
)
525+
.build(),
526+
null,
392527
3,
393-
LegAnnotation.fromJson("{ \"my_key1\": \"my_value1\" }"),
394-
"Old annotation has more unrecognized properties. " +
395-
"Unrecognized properties migrate from new annotation."
528+
LegAnnotation.builder()
529+
.unrecognizedJsonProperties(
530+
mapOf(
531+
"state_of_charge" to listOf(10, 11, 12, 13, 14)
532+
.toJsonArray(::JsonPrimitive),
533+
"custom_annotation" to listOf("z", "y", "x", "w", "v")
534+
.toJsonArray(::JsonPrimitive),
535+
)
536+
)
537+
.build(),
538+
"Unrecognized properties migrate from old annotation if new annotation is null"
396539
),
397540
)
398541
}
@@ -408,3 +551,6 @@ class AnnotationsRefresherTest(
408551
assertEquals(expectedMergedAnnotation, actual)
409552
}
410553
}
554+
555+
private fun <T> List<T>.toJsonArray(toJsonElement: (T) -> JsonElement): JsonArray =
556+
JsonArray().also { array -> forEach { array.add(toJsonElement(it)) } }

0 commit comments

Comments
 (0)