Skip to content

Commit 1a18941

Browse files
committed
PANA-8500: Validate captured composition trees
1 parent adc5eee commit 1a18941

4 files changed

Lines changed: 1396 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2016-Present Datadog, Inc.
5+
*/
6+
7+
package com.datadog.android.sessionreplay.internal.composition
8+
9+
@Suppress("TooManyFunctions") // Each function validates one independent mutation invariant.
10+
internal class CapturedMutationValidation(
11+
private val mutation: CapturedMutationSet,
12+
private val base: CapturedFullSnapshot
13+
) {
14+
private val failures = mutableListOf<CaptureValidationFailure>()
15+
private val adds = mutation.adds.valueOrEmpty()
16+
private val removes = mutation.removes.valueOrEmpty()
17+
private val updates = mutation.updates.valueOrEmpty()
18+
private val baseLayers = base.layers.associateBy { it.identity.wireId }
19+
20+
fun validate(): CaptureValidationResult {
21+
validateScope()
22+
validateOperationDuplicates()
23+
validateContradictions()
24+
validateTargets()
25+
val replacementRoot = validateReplacementRoot()
26+
27+
if (failures.isNotEmpty()) return CaptureValidationResult.Invalid(failures)
28+
return CapturedSnapshotValidation(
29+
snapshot = effectiveSnapshot(replacementRoot),
30+
validateWireframeDefinitions = false
31+
).validate()
32+
}
33+
34+
private fun validateScope() {
35+
if (mutation.scope != base.scope) {
36+
failures += validationFailure(CaptureValidationErrorCode.WRONG_IDENTITY_SCOPE)
37+
}
38+
}
39+
40+
private fun validateOperationDuplicates() {
41+
validateOperationDuplicates(adds.map { it.identity })
42+
validateOperationDuplicates(removes)
43+
validateOperationDuplicates(updates.map { it.identity })
44+
}
45+
46+
private fun validateOperationDuplicates(identities: List<CapturedIdentity>) {
47+
identities.groupBy(CapturedIdentity::wireId).values.forEach { operations ->
48+
if (operations.size > 1) {
49+
operations.firstOrNull()?.let {
50+
failures += validationFailure(CaptureValidationErrorCode.DUPLICATE_MUTATION_OPERATION, it)
51+
}
52+
}
53+
}
54+
}
55+
56+
private fun validateContradictions() {
57+
val addIds = adds.map { it.identity.wireId }.toSet()
58+
val removeIds = removes.map(CapturedIdentity::wireId).toSet()
59+
val updateIds = updates.map { it.identity.wireId }.toSet()
60+
(addIds intersect removeIds).forEach { reportContradiction(it) }
61+
(addIds intersect updateIds).forEach { reportContradiction(it) }
62+
(removeIds intersect updateIds).forEach { reportContradiction(it) }
63+
}
64+
65+
private fun reportContradiction(wireId: Long) {
66+
val identity = adds.firstOrNull { it.identity.wireId == wireId }?.identity
67+
?: removes.firstOrNull { it.wireId == wireId }
68+
?: updates.firstOrNull { it.identity.wireId == wireId }?.identity
69+
failures += validationFailure(CaptureValidationErrorCode.CONTRADICTORY_MUTATION, identity)
70+
}
71+
72+
private fun validateTargets() {
73+
removes.forEach(::validateRemoval)
74+
updates.forEach(::validateUpdate)
75+
adds.forEach(::validateAddition)
76+
}
77+
78+
private fun validateRemoval(identity: CapturedIdentity) {
79+
if (identity.scope != mutation.scope) {
80+
failures += validationFailure(CaptureValidationErrorCode.WRONG_IDENTITY_SCOPE, identity)
81+
}
82+
validateExistingTarget(identity)
83+
}
84+
85+
private fun validateUpdate(update: CapturedLayerUpdate) {
86+
if (update.identity.scope != mutation.scope) {
87+
failures += validationFailure(CaptureValidationErrorCode.WRONG_IDENTITY_SCOPE, update.identity)
88+
}
89+
validateExistingTarget(update.identity)
90+
}
91+
92+
private fun validateExistingTarget(identity: CapturedIdentity) {
93+
val target = baseLayers[identity.wireId]
94+
when {
95+
target == null -> failures += validationFailure(
96+
CaptureValidationErrorCode.UNKNOWN_MUTATION_TARGET,
97+
identity
98+
)
99+
100+
target.identity != identity -> failures += validationFailure(
101+
CaptureValidationErrorCode.REFERENCE_IDENTITY_MISMATCH,
102+
identity
103+
)
104+
}
105+
}
106+
107+
private fun validateAddition(layer: CapturedLayer) {
108+
if (layer.identity.scope != mutation.scope) {
109+
failures += validationFailure(CaptureValidationErrorCode.WRONG_IDENTITY_SCOPE, layer.identity)
110+
}
111+
val identityAlreadyExists = baseLayers[layer.identity.wireId] != null ||
112+
base.root?.identity?.wireId == layer.identity.wireId
113+
if (identityAlreadyExists) {
114+
failures += validationFailure(CaptureValidationErrorCode.CONTRADICTORY_MUTATION, layer.identity)
115+
}
116+
}
117+
118+
private fun validateReplacementRoot(): CapturedLayer? {
119+
val replacement = (mutation.root as? CapturedChange.Set)?.value ?: return null
120+
val isValid = replacement.kind == CapturedLayerKind.SYNTHETIC_SCREEN_ROOT &&
121+
replacement.identity.kind == CapturedIdentityKind.SCREEN_ROOT &&
122+
replacement.identity.scope == mutation.scope &&
123+
(base.root == null || replacement.identity == base.root.identity)
124+
if (!isValid) {
125+
failures += validationFailure(CaptureValidationErrorCode.INVALID_ROOT_REPLACEMENT, replacement.identity)
126+
}
127+
return replacement
128+
}
129+
130+
// The map is a mutable copy owned by this validation operation.
131+
@Suppress("UnsafeThirdPartyFunctionCall")
132+
private fun effectiveSnapshot(replacementRoot: CapturedLayer?): CapturedFullSnapshot {
133+
val effectiveLayers = baseLayers.toMutableMap()
134+
removes.forEach { effectiveLayers.remove(it.wireId) }
135+
adds.forEach { effectiveLayers[it.identity.wireId] = it }
136+
updates.forEach { update ->
137+
effectiveLayers[update.identity.wireId]?.let {
138+
effectiveLayers[update.identity.wireId] = it.apply(update)
139+
}
140+
}
141+
return base.copy(
142+
timestamp = mutation.timestamp,
143+
root = replacementRoot ?: base.root,
144+
layers = effectiveLayers.values.toList()
145+
)
146+
}
147+
148+
private fun CapturedLayer.apply(update: CapturedLayerUpdate): CapturedLayer = copy(
149+
bounds = bounds.copy(
150+
x = update.x.valueOr(bounds.x),
151+
y = update.y.valueOr(bounds.y),
152+
width = update.width.valueOr(bounds.width),
153+
height = update.height.valueOr(bounds.height)
154+
),
155+
children = update.children.valueOr(children),
156+
modifiers = update.modifiers.valueOr(modifiers),
157+
compositeOperation = when (val operation = update.compositeOperation) {
158+
is CapturedChange.Set -> operation.value
159+
CapturedChange.Unchanged -> compositeOperation
160+
}
161+
)
162+
163+
private fun <T> CapturedChange<List<T>>.valueOrEmpty(): List<T> =
164+
(this as? CapturedChange.Set)?.value.orEmpty()
165+
166+
private fun <T> CapturedChange<T>.valueOr(default: T): T =
167+
(this as? CapturedChange.Set)?.value ?: default
168+
}

0 commit comments

Comments
 (0)