Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rewrite.rules = [
prefercurlyfors
]

runner.dialect = scala213
runner.dialect = scala213source3
fileOverride {
"glob:**/src/*/scala-3/**" {
runner.dialect = scala3
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/scala/diffson/jsonpointer/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

package diffson

import cats._
import cats.syntax.all._
import cats.data.Chain

import scala.util.Try
import cats.syntax.all.*
import cats.{MonadError, Show}

import scala.collection.compat.immutable.ArraySeq
import scala.util.Try

package object jsonpointer {

Expand All @@ -37,19 +36,20 @@ package object jsonpointer {

def evaluate[F[_], Json](json: Json)(implicit F: MonadError[F, Throwable], Json: Jsony[Json]): F[Json] =
F.tailRecM((json, Pointer(parts), Pointer.Root)) {
case (JsObject(obj), Inner(Left(elem), tl), parent) =>
F.pure(Left((obj.getOrElse(elem, Json.Null), tl, parent / elem)))
case (JsObject(obj), Inner(elem, tl), parent) =>
val fieldName = elem.fold(identity, Integer.toString(_))
F.pure(Left((obj.getOrElse(fieldName, Json.Null), tl, parent / fieldName)))
case (JsArray(arr), Inner(Right(idx), tl), parent) =>
if (idx >= arr.size)
// we know (by construction) that the index is greater or equal to zero
F.raiseError(new PointerException(show"element $idx does not exist at path $parent"))
else
F.pure(Left((arr(idx), tl, parent / idx)))
case (value, Pointer.Root, _) =>
F.pure(Right(value))
case (_, Inner(elem, _), parent) =>
val elems = elem.fold(identity, _.toString)
F.raiseError(new PointerException(show"element $elems does not exist at path $parent"))
case (value, _, _) =>
F.pure(Right(value))
}

}
Expand Down
6 changes: 5 additions & 1 deletion testkit/shared/src/main/scala/diffson/TestJsonPointer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package diffson
package jsonpointer

import cats.implicits._
import cats.syntax.all._

import org.scalatest.flatspec.AnyFlatSpec

Expand Down Expand Up @@ -100,4 +100,8 @@ abstract class TestJsonPointer[Json](implicit Json: Jsony[Json])
parsePointer("/123456789012") should be(Pointer.Root / "123456789012")
}

it should "access the object field if applied to an obkect" in {
parsePointer("/0").evaluate[Try, Json](parseJson("""{"0": "value"}""")).get should be("value": Json)
}

}
Loading