-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jbwheatley
committed
Aug 11, 2021
1 parent
1f13142
commit a9daba1
Showing
16 changed files
with
605 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
scalapact-circe-0-14/src/main/scala/com/itv/scalapact/circe14/JsonConversionFunctions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.itv.scalapact.circe14 | ||
|
||
import com.itv.scalapact.shared.utils.ColourOutput._ | ||
import com.itv.scalapact.shared.json.IJsonConversionFunctions | ||
import com.itv.scalapact.shared.matchir.IrNodePath.IrNodePathEmpty | ||
import com.itv.scalapact.shared.matchir.MatchIrConstants.{rootNodeLabel, unnamedNodeLabel} | ||
import com.itv.scalapact.shared.matchir._ | ||
import com.itv.scalapact.shared.utils.PactLogger | ||
import io.circe._ | ||
import io.circe.parser._ | ||
|
||
object JsonConversionFunctions extends IJsonConversionFunctions { | ||
|
||
def fromJSON(jsonString: String): Option[IrNode] = | ||
parse(jsonString).toOption.flatMap { json => | ||
jsonRootToIrNode(json, IrNodePathEmpty) | ||
} | ||
|
||
private def jsonToIrNode(label: String, json: Json, pathToParent: IrNodePath): IrNode = | ||
json match { | ||
case j: Json if j.isArray => | ||
IrNode(label, jsonArrayToIrNodeList(label, j, pathToParent)).withPath(pathToParent).markAsArray | ||
|
||
case j: Json if j.isObject => | ||
IrNode(label, jsonObjectToIrNodeList(j, pathToParent)).withPath(pathToParent) | ||
|
||
case j: Json if j.isNumber => | ||
IrNode(label, j.asNumber.map(_.toDouble).map(d => IrNumberNode(d))).withPath(pathToParent) | ||
|
||
case j: Json if j.isBoolean => | ||
IrNode(label, j.asBoolean.map(IrBooleanNode)).withPath(pathToParent) | ||
|
||
case j: Json if j.isString => | ||
IrNode(label, j.asString.map(IrStringNode)).withPath(pathToParent) | ||
|
||
case _ => | ||
IrNode(label, IrNullNode).withPath(pathToParent) | ||
} | ||
|
||
private def jsonObjectToIrNodeList(json: Json, pathToParent: IrNodePath): List[IrNode] = | ||
json.hcursor.keys | ||
.map(_.toSet) | ||
.map(_.toList) | ||
.getOrElse(Nil) | ||
.map(l => if (l.isEmpty) unnamedNodeLabel else l) | ||
.map { l => | ||
json.hcursor.downField(l).focus.map { q => | ||
jsonToIrNode(l, q, pathToParent <~ l) | ||
} | ||
} | ||
.collect { case Some(s) => s } | ||
|
||
private def jsonArrayToIrNodeList(parentLabel: String, json: Json, pathToParent: IrNodePath): List[IrNode] = | ||
json.asArray | ||
.map(_.toList) | ||
.getOrElse(Nil) | ||
.zipWithIndex | ||
.map(j => jsonToIrNode(parentLabel, j._1, pathToParent <~ j._2)) | ||
|
||
private def jsonRootToIrNode(json: Json, initialPath: IrNodePath): Option[IrNode] = | ||
json match { | ||
case j: Json if j.isArray => | ||
Option( | ||
IrNode(rootNodeLabel, jsonArrayToIrNodeList(unnamedNodeLabel, j, initialPath)) | ||
.withPath(initialPath) | ||
.markAsArray | ||
) | ||
|
||
case j: Json if j.isObject => | ||
Option( | ||
IrNode(rootNodeLabel, jsonObjectToIrNodeList(j, initialPath)) | ||
.withPath(initialPath) | ||
) | ||
|
||
case _ => | ||
PactLogger.error("JSON was not an object or an array".red) | ||
None | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
scalapact-circe-0-14/src/main/scala/com/itv/scalapact/circe14/JsonInstances.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.itv.scalapact.circe14 | ||
|
||
import com.itv.scalapact.shared.{JvmPact, Pact} | ||
import com.itv.scalapact.shared.json.{ContractDeserializer, IPactReader, IPactWriter} | ||
import io.circe.parser.parse | ||
|
||
trait JsonInstances { | ||
import PactImplicits._ | ||
|
||
implicit val pactReaderInstance: IPactReader = new PactReader | ||
|
||
implicit val pactWriterInstance: IPactWriter = new PactWriter | ||
|
||
implicit val pactDeserializer: ContractDeserializer[Pact] = (jsonString: String) => | ||
parse(jsonString).flatMap(_.as[Pact]) match { | ||
case Right(a) => Right(a) | ||
case Left(_) => Left(s"Could not read scala-pact pact from json: $jsonString") | ||
} | ||
|
||
implicit val jvmPactDeserializer: ContractDeserializer[JvmPact] = (jsonString: String) => | ||
parse(jsonString).flatMap(_.as[JvmPact]) match { | ||
case Right(a) => Right(a) | ||
case Left(_) => Left(s"Could not read jvm-pact pact from json: $jsonString") | ||
} | ||
} |
Oops, something went wrong.