Skip to content

Commit

Permalink
refactor(backend): make MetadataType to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasKampmann authored and fengelniederhammer committed Dec 11, 2023
1 parent 6e6c524 commit 6977159
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
32 changes: 30 additions & 2 deletions backend/src/main/kotlin/org/pathoplexus/backend/config/Config.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.pathoplexus.backend.config

import com.fasterxml.jackson.annotation.JsonProperty
import org.apache.commons.lang3.StringUtils.lowerCase
import org.pathoplexus.backend.api.Organism

data class BackendConfig(
Expand All @@ -20,9 +22,35 @@ data class Schema(
val metadata: List<Metadata>,
)

enum class MetadataType {
@JsonProperty("string")
STRING,

@JsonProperty("integer")
INTEGER,

@JsonProperty("float")
FLOAT,

@JsonProperty("double")
DOUBLE,

@JsonProperty("number")
NUMBER,

@JsonProperty("date")
DATE,

@JsonProperty("pango_lineage")
PANGO_LINEAGE,

;

override fun toString(): String = lowerCase(name)
}

data class Metadata(
val name: String,
// TODO(#538) make this an enum
val type: String,
val type: MetadataType,
val required: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.pathoplexus.backend.api.Organism
import org.pathoplexus.backend.api.SubmittedProcessedData
import org.pathoplexus.backend.config.BackendConfig
import org.pathoplexus.backend.config.Metadata
import org.pathoplexus.backend.config.MetadataType
import org.pathoplexus.backend.config.ReferenceGenome
import org.pathoplexus.backend.config.ReferenceSequence
import org.pathoplexus.backend.config.Schema
Expand Down Expand Up @@ -131,7 +132,7 @@ class SequenceValidator(
}

when (metadata.type) {
"date" -> {
MetadataType.DATE -> {
if (!isValidDate(fieldValue.asText())) {
throw ProcessingValidationException(
"Expected type 'date' in format '$DATE_FORMAT' for field '${metadata.name}', " +
Expand All @@ -141,7 +142,7 @@ class SequenceValidator(
return
}

"pango_lineage" -> {
MetadataType.PANGO_LINEAGE -> {
if (!isValidPangoLineage(fieldValue.asText())) {
throw ProcessingValidationException(
"Expected type 'pango_lineage' for field '${metadata.name}', " +
Expand All @@ -151,14 +152,16 @@ class SequenceValidator(
}
return
}

else -> {}
}

val isOfCorrectPrimitiveType = when (metadata.type) {
"string" -> fieldValue.isTextual
"integer" -> fieldValue.isInt
"float" -> fieldValue.isFloat
"double" -> fieldValue.isDouble
"number" -> fieldValue.isNumber
MetadataType.STRING -> fieldValue.isTextual
MetadataType.INTEGER -> fieldValue.isInt
MetadataType.FLOAT -> fieldValue.isFloat
MetadataType.DOUBLE -> fieldValue.isDouble
MetadataType.NUMBER -> fieldValue.isNumber
else -> false
}

Expand Down

0 comments on commit 6977159

Please sign in to comment.