Skip to content

Commit

Permalink
Make fromFullPaths tail recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaamfr committed Nov 20, 2024
1 parent dea8a72 commit 81c70b7
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions circe/src/main/scala/com/kevel/apso/circe/Implicits.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kevel.apso.circe

import scala.annotation.tailrec
import scala.util.Try

import io.circe._
Expand Down Expand Up @@ -110,10 +111,13 @@ object Implicits {
* the sequence of dot-separated (or other separator) paths
* @param separatorRegex
* regex to use to separate fields
* @param acc
* accumulator for the resulting Json object
* @return
* the resulting Json object
*/
def fromFullPaths(paths: Seq[(String, Json)], separatorRegex: String = "\\."): Json = {
@tailrec
def fromFullPaths(paths: Seq[(String, Json)], separatorRegex: String = "\\.", acc: Json = Json.obj()): Json = {
def createJson(keys: Seq[String], value: Json): Json = {
keys match {
case Nil => value
Expand All @@ -122,9 +126,10 @@ object Implicits {
}

paths match {
case Nil => Json.obj()
case Nil => acc
case (path, value) :: rem =>
createJson(path.split(separatorRegex).toList, value).deepMerge(fromFullPaths(rem, separatorRegex))
val newAcc = acc.deepMerge(createJson(path.split(separatorRegex).toList, value))
fromFullPaths(rem, separatorRegex, newAcc)
}
}

Expand Down

0 comments on commit 81c70b7

Please sign in to comment.