Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularize SurveyConverter.toSurvey() #2939

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,41 @@
fun toSurvey(doc: DocumentSnapshot, jobs: List<Job> = listOf()): SurveyModel {
if (!doc.exists()) throw DataStoreException("Missing survey")

val surveyFromProto = SurveyProto::class.parseFrom(doc, 1)
val jobMap = jobs.associateBy { it.id }
val dataSharingTerms =
if (surveyFromProto.dataSharingTerms.type == Survey.DataSharingTerms.Type.TYPE_UNSPECIFIED) {
null
} else {
surveyFromProto.dataSharingTerms
}
return SurveyModel(
surveyFromProto.id.ifEmpty { doc.id },
surveyFromProto.name,
surveyFromProto.description,
jobMap.toPersistentMap(),
surveyFromProto.aclMap.entries.associate { it.key to it.value.toString() },
val surveyFromProto = parseSurveyFromDocument(doc)
val jobMap = convertJobsToMap(jobs)
val dataSharingTerms = getDataSharingTerms(surveyFromProto)

return createSurveyModel(doc, surveyFromProto, jobMap, dataSharingTerms)
}

/** Parse survey data from the DocumentSnapshot. */
private fun parseSurveyFromDocument(doc: DocumentSnapshot): SurveyProto =
SurveyProto::class.parseFrom(doc, 1)

/** Convert a list of jobs into a map for easy lookup. */
private fun convertJobsToMap(jobs: List<Job>): Map<String, Job> = jobs.associateBy { it.id }

/** Extract dataSharingTerms from survey. */
private fun getDataSharingTerms(surveyProto: SurveyProto): Survey.DataSharingTerms? =
if (surveyProto.dataSharingTerms.type == Survey.DataSharingTerms.Type.TYPE_UNSPECIFIED) {
null

Check warning on line 52 in ground/src/main/java/com/google/android/ground/persistence/remote/firebase/schema/SurveyConverter.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/persistence/remote/firebase/schema/SurveyConverter.kt#L52

Added line #L52 was not covered by tests
} else {
surveyProto.dataSharingTerms
}

/** Build SurveyModel from parsed data. */
private fun createSurveyModel(
doc: DocumentSnapshot,
surveyProto: SurveyProto,
jobMap: Map<String, Job>,
dataSharingTerms: Survey.DataSharingTerms?,
): SurveyModel =
SurveyModel(
id = surveyProto.id.ifEmpty { doc.id },
title = surveyProto.name,
description = surveyProto.description,
jobMap = jobMap.toPersistentMap(),
acl = surveyProto.aclMap.entries.associate { it.key to it.value.toString() },
dataSharingTerms = dataSharingTerms,
)
}
}
Loading