Skip to content

Commit

Permalink
JsonPointer - define more appended functions
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-brm committed Dec 27, 2023
1 parent 3a03a07 commit 17eb63a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.vertx.lang.scala.json

import java.net.URI
import scala.annotation.tailrec
import scala.collection.immutable.{AbstractSeq, LinearSeq}

private type JJsonPointer = io.vertx.core.json.pointer.JsonPointer

Expand All @@ -25,7 +27,7 @@ final case class JsonPointer(private val internal: JJsonPointer):
/**
* Copy a `JsonPointer`
*/
def copy(): JsonPointer = JsonPointer(internal.copy())
def copy: JsonPointer = JsonPointer(internal.copy())

override def toString: String = internal.toString

Expand All @@ -40,12 +42,23 @@ final case class JsonPointer(private val internal: JJsonPointer):
def getURIWithoutFragment: URI = internal.getURIWithoutFragment

/**
* Append an unescaped token to this pointer
* This pointer appended with String tokens.
*
* @param token the unescaped reference token
* @return a reference to this, so the API can be used fluently
* @param tokens the tokens to append which must not contain escaped characters
*/
def append(token: String): JsonPointer = JsonPointer(internal.append(token))
def appended(tokens: String*): JsonPointer =
@tailrec def append(ptr: JsonPointer, tokens: String*): JsonPointer = tokens.headOption match
case Some(token) => append(JsonPointer(ptr.internal.append(token)), tokens.tail*)
case None => ptr
append(this.copy, tokens*)


/**
* Append the index as reference token to this JsonPointer.
*/
def appended(index: Int): JsonPointer = JsonPointer(internal.copy.append(index))



end JsonPointer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,25 @@ class JsonPointerSpec extends AnyFunSpec, Matchers, Inside:

describe("Appending to a JsonPointer") {
val pointer = JsonPointer("/foo/bar")

it("should append an unescaped String") {
val appended = pointer.append("baz")
// See RFC 6901, No. 5:
// Note that before processing a JSON string as a JSON Pointer,
// backslash escape sequences must be unescaped
val appended = pointer.appended("baz")
appended.toString should be("/foo/bar/baz")
pointer.isParent(appended) should be(true)
??? // isParent nachschauen im RFC
pointer.toString should be("/foo/bar")
}

it("should append an index") {
val appended = pointer.appended(23)
appended.toString should be("/foo/bar/23")
pointer.toString should be("/foo/bar")
}

it("should append a sequence of String tokens") {
val appended = pointer.appended("baz", "qux", "qax")
appended.toString should be("/foo/bar/baz/qux/qax")
pointer.toString should be("/foo/bar")
}
}

0 comments on commit 17eb63a

Please sign in to comment.