-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate json-path in presentation definition (#1311)
Signed-off-by: Pat Losoponkul <pat.losoponkul@iohk.io>
- Loading branch information
patlo-iog
authored
Aug 30, 2024
1 parent
3a7984b
commit 9ef6b09
Showing
10 changed files
with
245 additions
and
20 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
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
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
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
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
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
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
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
53 changes: 53 additions & 0 deletions
53
shared/json/src/main/scala/org/hyperledger/identus/shared/json/JsonPath.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,53 @@ | ||
package org.hyperledger.identus.shared.json | ||
|
||
import com.jayway.jsonpath.{InvalidPathException, JsonPath as JaywayJsonPath, PathNotFoundException} | ||
import com.jayway.jsonpath.spi.json.JacksonJsonProvider | ||
import org.hyperledger.identus.shared.models.{Failure, StatusCode} | ||
import zio.* | ||
import zio.json.* | ||
import zio.json.ast.Json | ||
|
||
sealed trait JsonPathError extends Failure { | ||
override def namespace: String = "JsonPathError" | ||
} | ||
|
||
object JsonPathError { | ||
final case class InvalidPathInput(msg: String) extends JsonPathError { | ||
override def statusCode: StatusCode = StatusCode.BadRequest | ||
override def userFacingMessage: String = s"The JsonPath input is not valid: $msg" | ||
} | ||
|
||
final case class PathNotFound(path: String) extends JsonPathError { | ||
override def statusCode: StatusCode = StatusCode.BadRequest | ||
override def userFacingMessage: String = s"The json path '$path' cannot be found in a json" | ||
} | ||
} | ||
|
||
opaque type JsonPath = JaywayJsonPath | ||
|
||
object JsonPath { | ||
def compile(path: String): IO[JsonPathError, JsonPath] = { | ||
ZIO | ||
.attempt(JaywayJsonPath.compile(path)) | ||
.refineOrDie { | ||
case e: IllegalArgumentException => JsonPathError.InvalidPathInput(e.getMessage()) | ||
case e: InvalidPathException => JsonPathError.InvalidPathInput(e.getMessage()) | ||
} | ||
} | ||
|
||
extension (jsonPath: JsonPath) { | ||
def read(json: Json): IO[JsonPathError, Json] = { | ||
val jsonProvider = JacksonJsonProvider() | ||
val document = JaywayJsonPath.parse(json.toString()) | ||
for { | ||
queriedObj <- ZIO | ||
.attempt(document.read[java.lang.Object](jsonPath)) | ||
.refineOrDie { case e: PathNotFoundException => | ||
JsonPathError.PathNotFound(jsonPath.getPath()) | ||
} | ||
queriedJsonStr = jsonProvider.toJson(queriedObj) | ||
queriedJson <- ZIO.fromEither(queriedJsonStr.fromJson[Json]).orDieWith(Exception(_)) | ||
} yield queriedJson | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
shared/json/src/test/scala/org/hyperledger/identus/shared/json/JsonPathSpec.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,111 @@ | ||
package org.hyperledger.identus.shared.json | ||
|
||
import org.hyperledger.identus.shared.json.JsonPathError.{InvalidPathInput, PathNotFound} | ||
import zio.* | ||
import zio.json.* | ||
import zio.json.ast.Json | ||
import zio.test.* | ||
import zio.test.Assertion.* | ||
|
||
object JsonPathSpec extends ZIOSpecDefault { | ||
|
||
override def spec = suite("JsonPathSpec")( | ||
test("sucessfully compile a valid json path") { | ||
val paths = Seq( | ||
"$.store.book[*].author", | ||
"$..author", | ||
"$.store.*", | ||
"$.store..price", | ||
"$..book[2]", | ||
"$..book[-1:]", | ||
"$..book[0,1]", | ||
"$..book[:2]", | ||
"$..book[?(@.isbn)]", | ||
"$..book[?(@.price<10)]", | ||
"$..book[?(@.price==8.95)]", | ||
"$..book[?(@.price<30 && @.category==\"fiction\")]", | ||
"$..*", | ||
"$['foo']['bar']" | ||
) | ||
ZIO | ||
.foreach(paths)(JsonPath.compile) | ||
.as(assertCompletes) | ||
}, | ||
test("do not accept invalid json path") { | ||
val paths = Seq( | ||
"", | ||
" ", | ||
" $ ", | ||
"$$", | ||
"hello world", | ||
) | ||
ZIO | ||
.foreach(paths)(p => JsonPath.compile(p).flip) | ||
.map { errors => | ||
assert(errors)(forall(isSubtype[InvalidPathInput](anything))) | ||
} | ||
}, | ||
test("query valid path inside json structure") { | ||
val jsonStr = | ||
"""{ | ||
| "vc": { | ||
| "name": "alice", | ||
| "age": 42, | ||
| "degree": null, | ||
| "pets": ["dog", "cat"], | ||
| "isEmployed": false, | ||
| "languages": {"english": "native", "chinese": "fluent"} | ||
| } | ||
|} | ||
""".stripMargin | ||
for { | ||
json <- ZIO.fromEither(jsonStr.fromJson[Json]) | ||
namePath <- JsonPath.compile("$.vc.name") | ||
agePath <- JsonPath.compile("$.vc.age") | ||
degreePath <- JsonPath.compile("$.vc.degree") | ||
petPath <- JsonPath.compile("$.vc.pets") | ||
firstPetPath <- JsonPath.compile("$.vc.pets[0]") | ||
isEmployedPath <- JsonPath.compile("$.vc.isEmployed") | ||
languagesPath <- JsonPath.compile("$.vc.languages") | ||
name <- namePath.read(json) | ||
age <- agePath.read(json) | ||
degree <- degreePath.read(json) | ||
pet <- petPath.read(json) | ||
firstPet <- firstPetPath.read(json) | ||
isEmployed <- isEmployedPath.read(json) | ||
languages <- languagesPath.read(json) | ||
} yield assert(name.asString)(isSome(equalTo("alice"))) | ||
&& assert(age.asNumber)(isSome(equalTo(Json.Num(42)))) | ||
&& assert(degree.asNull)(isSome(anything)) | ||
&& assert(pet.asArray)(isSome(hasSize((equalTo(2))))) | ||
&& assert(firstPet.asString)(isSome(equalTo("dog"))) | ||
&& assert(isEmployed.asBoolean)(isSome(isFalse)) | ||
&& assert(languages.asObject)(isSome(anything)) | ||
}, | ||
test("query invalid path inside json structure") { | ||
val jsonStr = | ||
"""{ | ||
| "vc": { | ||
| "name": "alice", | ||
| "pets": ["dog", "cat"] | ||
| } | ||
|} | ||
""".stripMargin | ||
for { | ||
json <- ZIO.fromEither(jsonStr.fromJson[Json]) | ||
nonExistingPath <- JsonPath.compile("$.vc2.name") | ||
invalidTypeArrayPath <- JsonPath.compile("$.vc.name[0]") | ||
outOfBoundArrayPath <- JsonPath.compile("$.vc.name[5]") | ||
outOfBoundSlicePath <- JsonPath.compile("$.vc.name[1:4]") | ||
exit1 <- nonExistingPath.read(json).exit | ||
exit2 <- invalidTypeArrayPath.read(json).exit | ||
exit3 <- outOfBoundArrayPath.read(json).exit | ||
exit4 <- outOfBoundSlicePath.read(json).exit | ||
} yield assert(exit1)(failsWithA[PathNotFound]) | ||
&& assert(exit2)(failsWithA[PathNotFound]) | ||
&& assert(exit3)(failsWithA[PathNotFound]) | ||
&& assert(exit4)(failsWithA[PathNotFound]) | ||
} | ||
) | ||
|
||
} |