Skip to content

Commit

Permalink
Unwrap seq fields in getValidationTypeFromCaseClass (#214)
Browse files Browse the repository at this point in the history
Co-authored-by: Jack Dingilian <[email protected]>
  • Loading branch information
jackdingilian and Jack Dingilian authored Feb 23, 2022
1 parent 6316fc5 commit dadb722
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,18 @@ object ElitzurMetrics {

val isOption = classOf[Option[_]].equals(firstFieldClass)
val isWrapped = classOf[ValidationStatus[_]].isAssignableFrom(firstFieldClass)
val isSeq = classOf[Seq[_]].isAssignableFrom(firstFieldClass)

fieldNames match {
case Seq(_) if isWrapped =>
unwrapValidationStatus(firstFieldGenericType)
case Seq(_) if isOption =>
case Seq(_) if isOption || isSeq =>
// remove one layer of parameterization only
getParameterizedInnerType(firstFieldGenericType).asInstanceOf[Class[_]]
case Seq(_) =>
// no parameterization
firstFieldClass
case Seq(_, tail@_*) if isOption =>
case Seq(_, tail@_*) if isOption || isSeq =>
getValidationTypeFromCaseClass(unwrapOptionType(firstFieldGenericType), tail)
case Seq(_, tail@_*) if isWrapped =>
getValidationTypeFromCaseClass(unwrapValidationStatus(firstFieldGenericType), tail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.spotify.elitzur.scio

import com.spotify.elitzur.CountryCodeTesting
import com.spotify.elitzur.scio.ElitzurMetrics.getValidationTypeFromCaseClass
import com.spotify.elitzur.validators.ValidationStatus
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
Expand All @@ -41,6 +42,8 @@ class ElitzurMetricsTest extends AnyFlatSpec with PrivateMethodTester with Match
case class HasMixedWrapping(outer: ValidationStatus[HasWrappedValidationTypes],
outerOption: ValidationStatus[Option[HasValidationTypes]])

case class RepeatedTest(repeated: List[CountryCodeTesting], nested: List[HasValidationTypes])

"getValidationTypeFromCaseClass" should "return unqualified validation type name" in {
val getValidationTypeFromCaseClass = PrivateMethod[String]('getValidationTypeFromCaseClass)
val countryCodeName = ElitzurMetrics invokePrivate
Expand Down Expand Up @@ -76,5 +79,17 @@ class ElitzurMetricsTest extends AnyFlatSpec with PrivateMethodTester with Match
"work for records where some inner fields are wrapped and some aren't" in {
testGetValidationTypeFromCaseClass(classOf[HasMixedWrapping])
}

"getValidationTypeFromCaseClass" should "work for repeated fields" in {
val name = ElitzurMetrics
.getValidationTypeFromCaseClass(classOf[RepeatedTest], "repeated")
name shouldBe "CountryCodeTesting"
}

"getValidationTypeFromCaseClass" should "work for repeated records" in {
val name = ElitzurMetrics
.getValidationTypeFromCaseClass(classOf[RepeatedTest], "nested.inner")
name shouldBe "CountryCodeTesting"
}
}
//scalastyle:on no.whitespace.before.left.bracket

0 comments on commit dadb722

Please sign in to comment.