Skip to content

Commit

Permalink
Add Analytics Strands verification
Browse files Browse the repository at this point in the history
  • Loading branch information
jguerinet committed Dec 3, 2018
1 parent 5dde3cb commit 9b41dff
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions src/main/java/Weave.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ open class Weave {
} else {
verifyAnalyticsConfigInfo(analyticsConfig)
val downloadedStrands = downloadAllAnalyticStrands(analyticsConfig)
val verifiedStrings = verifyKeys(downloadedStrands)
writeAnalyticStrands(analyticsConfig, verifiedStrings)
val verifiedIds = verifyKeys(downloadedStrands)
val verifiedStrands = verifyAnalyticsStrands(verifiedIds)
writeAnalyticStrands(analyticsConfig, verifiedStrands)
println("Analytics parsing complete")
}
} catch (e: IOException) {
Expand Down Expand Up @@ -397,14 +398,15 @@ open class Weave {
* that don't have all translations
*/
open fun verifyStringStrands(config: StringsConfig, strands: List<BaseStrand>): List<BaseStrand> {
val stringStrands = strands.mapNotNull { it as? LanguageStrand }
val toRemove = mutableListOf<BaseStrand>()

// Check if there are any duplicates
for (i in strands.indices) {
val strand1 = strands[i]
for (i in stringStrands.indices) {
val strand1 = stringStrands[i]

for (j in i + 1 until strands.size) {
val strand2 = strands[j]
for (j in i + 1 until stringStrands.size) {
val strand2 = stringStrands[j]

// If the keys are the same and it's not a header, show a warning and remove the older one
if (strand1.key == strand2.key) {
Expand All @@ -418,8 +420,7 @@ open class Weave {
verifiedStrands.removeAll(toRemove)
toRemove.clear()

strands
.mapNotNull { it as? LanguageStrand }
stringStrands
.forEach {
val lineNumber = it.lineNumber
val sourceName = it.sourceName
Expand Down Expand Up @@ -659,6 +660,36 @@ open class Weave {
}
}

/**
* Verifies the analytics [strands] by ensuring that there are no duplicates (same type and same key)
*/
open fun verifyAnalyticsStrands(strands: List<BaseStrand>): List<BaseStrand> {
val analyticsStrands = strands.mapNotNull { it as? AnalyticsStrand }
val toRemove = mutableListOf<BaseStrand>()

// Check if there are any duplicates
for (i in analyticsStrands.indices) {
val strand1 = analyticsStrands[i]

for (j in i + 1 until analyticsStrands.size) {
val strand2 = analyticsStrands[j]

// If the keys are the same and the type is the same, show a warning and remove the older one
if (strand1.key == strand2.key && strand1.type == strand2.type) {
warning(
"${getLog(strand1)} and ${getLog(strand2)} have the same key and type. " +
"The second one will be used"
)
toRemove.add(strand1)
}
}
}

val verifiedStrands = strands.toMutableList()
verifiedStrands.removeAll(toRemove)
return verifiedStrands
}

/**
* Writes the analytics Strings using the [config] data
*/
Expand Down

0 comments on commit 9b41dff

Please sign in to comment.