Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v5
- name: Setup JDK
uses: actions/setup-java@v4
uses: actions/setup-java@v5
with:
distribution: temurin
java-version: 21
cache: 'sbt'
- name: Build and Test
run: sbt +clean scalafmt::test test:scalafmt::test sbt:scalafmt::test coverage +compile +test:compile +test coverageReport && sbt coverageAggregate
- name: Upload code coverage
run: bash <(curl -s https://codecov.io/bash)
run: bash <(curl -s https://codecov.io/bash)
57 changes: 45 additions & 12 deletions core/src/main/scala/com/qvantel/jsonapi/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,32 +199,65 @@ package object jsonapi {
/** Reads a collection of jsonapi entities. Due to no includes path being provided includes are ignored.
*/
def readCollection[T](json: JsObject)(implicit reader: JsonApiReader[T]): Iterable[T] = {
val dataJson = json.fields
.get("data")
.filterNot(_.isInstanceOf[JsNull.type])
.map(_.convertTo[Set[JsObject]])
.getOrElse(Set.empty)
val dataJson = getDataSet(json)

dataJson.map(x => reader.read(x, Map.empty[(String, String), JsObject], Set.empty[String], ""))
}

/** Reads a collection of jsonapi entities. Due to include paths being provided includes will be handled.
*/
*/
def readCollection[T](json: JsObject, includes: Set[String])(implicit reader: JsonApiReader[T]): Iterable[T] = {
val dataJson = json.fields
val dataJson = getDataSet(json)
val includedJson = getIncludedSet(json)

val includedWithData = includedJson ++ dataJson.filter(_.fields.contains("id"))

dataJson.map(x => reader.read(x, includedWithData, fillIncludes(includes), ""))
}

/** Reads an ordered collection of jsonapi entities. Due to no includes path being provided includes are ignored.
*/
def readOrderedCollection[T](json: JsObject)(implicit reader: JsonApiReader[T]): Iterable[T] = {
val dataJson = getDataList(json)

dataJson.map(x => reader.read(x, Map.empty[(String, String), JsObject], Set.empty[String], ""))
}

/** Reads an ordered collection of jsonapi entities. Due to include paths being provided includes will be handled.
*/
def readOrderedCollection[T](json: JsObject, includes: Set[String])(implicit reader: JsonApiReader[T]): Iterable[T] = {
val dataJson = getDataList(json)
val includedJson = getIncludedSet(json)

val includedWithData = includedJson ++ dataJson.filter(_.fields.contains("id"))

dataJson.map(x => reader.read(x, includedWithData, fillIncludes(includes), ""))
}

private def getData(json: JsObject): Option[JsValue] = {
json.fields
.get("data")
.filterNot(_.isInstanceOf[JsNull.type])
}

private def getDataSet[T](json: JsObject): Set[JsObject] = {
getData(json)
.map(_.convertTo[Set[JsObject]])
.getOrElse(Set.empty)
val includedJson = json.fields
}

private def getDataList(json: JsObject): List[JsObject] = {
getData(json)
.map(_.convertTo[List[JsObject]])
.getOrElse(List.empty)
}

private def getIncludedSet(json: JsObject): Set[JsObject] = {
json.fields
.get("included")
.filterNot(_.isInstanceOf[JsNull.type])
.map(_.convertTo[Set[JsObject]])
.getOrElse(Set.empty)

val includedWithData = includedJson ++ dataJson.filter(_.fields.contains("id"))

dataJson.map(x => reader.read(x, includedWithData, fillIncludes(includes), ""))
}

private[this] def fillIncludes(includes: Set[String]): Set[String] = {
Expand Down
20 changes: 20 additions & 0 deletions core/src/test/scala/com/qvantel/jsonapi/ReadmeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,24 @@ class ReadmeSpec extends Specification {
"parsed must equal to acme" >> {
parsed must be equalTo acme
}

val companies = List(
Company("1", "first", ToMany.reference),
Company("2", "second", ToMany.reference),
Company("3", "third", ToMany.reference),
Company("4", "fourth", ToMany.reference),
Company("5", "fifth", ToMany.reference),
Company("6", "sixth", ToMany.reference),
Company("7", "seventh", ToMany.reference),
)

val jsonCollection = rawCollection(companies)
val expectedJsonStr = jsonCollection.prettyPrint

val parsedCollection = readOrderedCollection[Company](jsonCollection)
val parsedJsonStr = rawCollection(parsedCollection).prettyPrint

"parsed collection must equal to initial collection and order of elements shall be preserved" >> {
parsedJsonStr must be equalTo expectedJsonStr
}
}
Loading