Skip to content

Commit 429102d

Browse files
author
j
committed
implicit conversions for attributes and values
1 parent 076b2f7 commit 429102d

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.zalando.jsonapi.model.implicits
2+
3+
import scala.language.implicitConversions
4+
5+
import org.zalando.jsonapi.model.Attribute
6+
import org.zalando.jsonapi.model.implicits.JsonApiObjectValueConversions._
7+
8+
object AttributeConversions {
9+
implicit def convertToStringAttribute(nameString: (String, String)) = Attribute(nameString._1, nameString._2)
10+
implicit def convertToIntAttribute(nameInt: (String, Int)) = Attribute(nameInt._1, nameInt._2)
11+
implicit def convertToLongAttribute(nameLong: (String, Long)) = Attribute(nameLong._1, nameLong._2)
12+
implicit def convertToDoubleAttribute(nameDouble: (String, Double)) = Attribute(nameDouble._1, nameDouble._2)
13+
implicit def convertToFloatAttribute(nameFloat: (String, Float)) = Attribute(nameFloat._1, nameFloat._2)
14+
implicit def convertToBooleanAttribute(nameInt: (String, Boolean)) = Attribute(nameInt._1, nameInt._2)
15+
16+
implicit def convertToOptionalStringAttribute(nameString: (String, Option[String])) = nameString._2.map(nameString._1 -> _:Attribute)
17+
implicit def convertToOptionalIntAttribute(nameInt: (String, Option[Int])) = nameInt._2.map(nameInt._1 -> _:Attribute)
18+
implicit def convertToOptionalLongAttribute(nameLong: (String, Option[Long])) = nameLong._2.map(nameLong._1 -> _:Attribute)
19+
implicit def convertToOptionalDoubleAttribute(nameDouble: (String, Option[Double])) = nameDouble._2.map(nameDouble._1 -> _:Attribute)
20+
implicit def convertToOptionalFloatAttribute(nameFloat: (String, Option[Float])) = nameFloat._2.map(nameFloat._1 -> _:Attribute)
21+
implicit def convertToOptionalBooleanAttribute(nameInt: (String, Option[Boolean])) = nameInt._2.map(nameInt._1 -> _:Attribute)
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.zalando.jsonapi.model.implicits
2+
3+
import scala.language.implicitConversions
4+
5+
import org.zalando.jsonapi.model.JsonApiObject._
6+
7+
object JsonApiObjectValueConversions {
8+
implicit def convertStringToStringValue(string: String) = StringValue(string)
9+
implicit def convertIntToNumberValue(int: Int) = NumberValue(int)
10+
implicit def convertLongToNumberValue(long: Long) = NumberValue(long)
11+
implicit def convertDoubleToNumberValue(double: Double) = NumberValue(double)
12+
implicit def convertFloatToNumberValue(float: Float) = NumberValue(float)
13+
implicit def convertBooleanToBooleanValue(boolean: Boolean) = BooleanValue(boolean)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.zalando.jsonapi.model.implicits
2+
3+
import org.scalatest.{Matchers, WordSpec}
4+
import org.zalando.jsonapi.model.Attribute
5+
import org.zalando.jsonapi.model.JsonApiObject._
6+
import org.zalando.jsonapi.model.implicits.AttributeConversions._
7+
8+
class AttributeConversionsSpec extends WordSpec with Matchers {
9+
"scala tuples" should {
10+
"be converted to string attributes" in {
11+
convertToStringAttribute("name" -> "string") should be(Attribute("name", StringValue("string")))
12+
}
13+
"be converted to number attributes" in {
14+
convertToIntAttribute("name" -> 42) should be(Attribute("name", NumberValue(42)))
15+
convertToLongAttribute("name" -> 42l) should be(Attribute("name", NumberValue(42)))
16+
convertToFloatAttribute("name" -> 42f) should be(Attribute("name", NumberValue(42)))
17+
convertToDoubleAttribute("name" -> 42d) should be(Attribute("name", NumberValue(42)))
18+
}
19+
"be converted to boolean attributes" in {
20+
convertToBooleanAttribute("name" -> true) should be(Attribute("name", BooleanValue(true)))
21+
convertToBooleanAttribute("name" -> false) should be(Attribute("name", BooleanValue(false)))
22+
}
23+
24+
"be converted to optional string attributes" in {
25+
convertToOptionalStringAttribute("name" -> Option("string")) should be(Option(Attribute("name", StringValue("string"))))
26+
}
27+
"be converted to optional number attributes" in {
28+
convertToOptionalIntAttribute("name" -> Option(42)) should be(Option(Attribute("name", NumberValue(42))))
29+
convertToOptionalLongAttribute("name" -> Option(42l)) should be(Option(Attribute("name", NumberValue(42))))
30+
convertToOptionalFloatAttribute("name" -> Option(42f)) should be(Option(Attribute("name", NumberValue(42))))
31+
convertToOptionalDoubleAttribute("name" -> Option(42d)) should be(Option(Attribute("name", NumberValue(42))))
32+
}
33+
"be converted to optional boolean attributes" in {
34+
convertToOptionalBooleanAttribute("name" -> Option(true)) should be(Option(Attribute("name", BooleanValue(true))))
35+
convertToOptionalBooleanAttribute("name" -> Option(false)) should be(Option(Attribute("name", BooleanValue(false))))
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
package org.zalando.jsonapi.model.implicits
3+
4+
import org.scalatest.{Matchers, WordSpec}
5+
import org.zalando.jsonapi.model.JsonApiObject._
6+
import org.zalando.jsonapi.model.implicits.JsonApiObjectValueConversions._
7+
8+
class JsonApiObjectValueConversionsSpec extends WordSpec with Matchers {
9+
"scala values" should {
10+
"be converted to string values" in {
11+
convertStringToStringValue("string") should be(StringValue("string"))
12+
}
13+
"be converted to number values" in {
14+
convertIntToNumberValue(42) should be(NumberValue(42))
15+
convertLongToNumberValue(42l) should be(NumberValue(42))
16+
convertFloatToNumberValue(42f) should be(NumberValue(42))
17+
convertDoubleToNumberValue(42d) should be(NumberValue(42))
18+
}
19+
"be converted to boolean values" in {
20+
convertBooleanToBooleanValue(true) should be(BooleanValue(true))
21+
convertBooleanToBooleanValue(false) should be(BooleanValue(false))
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)