Skip to content

Commit

Permalink
Cursor ops based getAll implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Feb 8, 2024
1 parent b1be79c commit 5350b00
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.tinkoff.tcb.utils.circe.optics

import scala.annotation.nowarn

import io.circe.ACursor
import io.circe.Json

@nowarn("cat=scala3-migration")
Expand Down Expand Up @@ -34,13 +35,20 @@ final case class JsonOptic private[optics] (private val jsonPath: Seq[PathPart])
def get: Json => Json = getOpt.andThen(_.getOrElse(Json.Null))

def getAll: Json => Vector[Json] = { json =>
jsonPath.foldLeft(Vector(json))((j, p) =>
p.fold(
f => j.flatMap(_.hcursor.downField(f).focus),
i => j.flatMap(_.hcursor.downN(i).focus),
j.flatMap(_.asArray.toVector.flatten)
jsonPath
.foldLeft(Vector[ACursor](json.hcursor))((c, p) =>
p.fold(
f => c.map(_.downField(f)),
i => c.map(_.downN(i)),
c.flatMap(cx =>
cx.values match {
case Some(value) => Vector.tabulate(value.size)(cx.downN)
case None => Vector.empty
}
)
)
)
)
.flatMap(_.focus)
}

def prune: Json => Json = { json =>
Expand Down

0 comments on commit 5350b00

Please sign in to comment.