Skip to content

Commit

Permalink
deal with nested class better (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
kailuowang committed Nov 28, 2016
1 parent 20cdd58 commit 95d4db5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
4 changes: 2 additions & 2 deletions optional/src/main/scala/henkan/optional/ToOptional.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ trait MkToOptional extends MkToOptional0 {
implicit
genFrom: LabelledGeneric.Aux[From, FL],
genTo: LabelledGeneric.Aux[To, TL],
convertHList: ToOptional[FL, TL]
convertHList: Lazy[ToOptional[FL, TL]]
) = new ToOptional[From, To] {
def apply(from: From): To = {
genTo.from(convertHList(genFrom.to(from)))
genTo.from(convertHList.value(genFrom.to(from)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ trait MkValidateFromOptional extends MkValidateFromOptional0 {
implicit
genFrom: LabelledGeneric.Aux[From, FL],
genTo: LabelledGeneric.Aux[To, TL],
convertHList: ValidateFromOptional[FL, TL]
convertHList: Lazy[ValidateFromOptional[FL, TL]]
) = new ValidateFromOptional[From, To] {
def apply(from: From): ValidatedNel[RequiredFieldMissing, To] = {
convertHList(genFrom.to(from)).map(genTo.from)
convertHList.value(genFrom.to(from)).map(genTo.from)
}
}

Expand Down
14 changes: 14 additions & 0 deletions optional/src/test/scala/henkan/optional/TestDomain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ object TestDomain {
case class ListMessageDirect(a: Option[Double], children: List[Message])
case class ListDomain(a: Double, children: List[Domain])

case class ListMixedMessage(a: Option[Double], children: List[Message])
case class ListMixedDomain(a: Double, children: List[DomainWithOptionalB])

case class MessageMissingB(a: Option[String])
case class Domain(a: String, b: Int)
case class DomainWithOptionalB(a: String, b: Option[Int])
Expand All @@ -24,4 +27,15 @@ object TestDomain {
case class MessageWithRequiredField(a: Option[String], b: List[Int])
case class DomainWithAllFieldsRequired(a: String, b: List[Int])

case class MessageWithMixedField(a: Option[String], b: List[Int], c: Option[Double])
case class DomainWithMixedField(a: String, b: List[Int], c: Option[Double])

case class NestedGrand(name: String, child: NestedParent)
case class NestedParent(name: String, child: NestedChild)
case class NestedChild(name: String)

case class NestedGrandMsg(name: Option[String], child: Option[NestedParentMsg])
case class NestedParentMsg(name: Option[String], child: Option[NestedChildMsg])
case class NestedChildMsg(name: Option[String])

}
21 changes: 21 additions & 0 deletions optional/src/test/scala/henkan/optional/ToOptionalSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,37 @@ class ToOptionalSpec extends Specification {
Some(11d), Some(List(Message(Some("dfd"), Some(23))))
)
}

"convert domain with Functor fields direct" >> {
from(ListDomain(11d, List(Domain("dfd", 23))))
.toOptional[ListMessageDirect] must_== ListMessageDirect(
Some(11d), List(Message(Some("dfd"), Some(23)))
)
}

"convert domain with Functor fields with complex item type" >> {
from(ListMixedDomain(11d, List(DomainWithOptionalB("dfd", Some(23)))))
.toOptional[ListMixedMessage] must_== ListMixedMessage(
Some(11d), List(Message(Some("dfd"), Some(23)))
)
}

"convert to Message with required fields" >> {
from(DomainWithAllFieldsRequired("a", List(1))).toOptional[MessageWithRequiredField] must_==
MessageWithRequiredField(Some("a"), List(1))
}

"convert to Message with mixed fields" >> {
from(DomainWithMixedField("a", List(1), Some(1d))).toOptional[MessageWithMixedField] must_==
MessageWithMixedField(Some("a"), List(1), Some(1d))
}

"convert to Message with deep nested field" >> {
from(NestedGrand("a", NestedParent("b", NestedChild("c")))).toOptional[NestedGrandMsg] must_==
NestedGrandMsg(Some("a"), Some(
NestedParentMsg(Some("b"), Some(NestedChildMsg(Some("c"))))
))

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,12 @@ class ValidateFromOptionalSpec extends Specification {
validate(MessageWithRequiredA("a", Some(1))).to[Domain] must_==
Validated.Valid(Domain("a", 1))
}

"translate with deep nested side" >> {
validate(NestedGrandMsg(Some("a"), Some(
NestedParentMsg(Some("b"), Some(NestedChildMsg(Some("c"))))
))).to[NestedGrand] must_==
Validated.Valid(NestedGrand("a", NestedParent("b", NestedChild("c"))))
}
}

0 comments on commit 95d4db5

Please sign in to comment.