Skip to content

Commit

Permalink
fix LogTree (#794)
Browse files Browse the repository at this point in the history
* fix LogTree

* Change to LinkedHashMap for consistent field order
  • Loading branch information
Odomontois committed Oct 5, 2021
1 parent 01bf629 commit ac55b6e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ object LogTree extends LogBuilder[Json] {
}
type Top = LogDict

class LogDict(private val values: mutable.Map[String, LogTree]) extends LogTree {
class LogDict(private val values: mutable.LinkedHashMap[String, LogTree]) extends LogTree {
def add(name: String, tree: LogTree): Eval[Unit] = Eval.always(values.update(name, tree))
def getList: Eval[List[(String, LogTree)]] = Eval.always(values.toList)
}

final case class LogArr(values: Iterable[LogTree]) extends LogTree

private val newdict = Eval.always(mutable.Map.empty[String, LogTree]).map(new LogDict(_))
private val newdict = Eval.always(mutable.LinkedHashMap.empty[String, LogTree]).map(new LogDict(_))
private val newtree = Eval.always(new Value(NullValue))

val receiver: LogRenderer[LogDict, Value, Output, ValRes] = new LogRenderer[LogDict, Value, Output, ValRes] {
Expand Down Expand Up @@ -68,7 +68,7 @@ object LogTree extends LogBuilder[Json] {
_ <- input.set(LogArr(vs))
} yield true
def dict(input: Value)(receive: LogDict => Eval[Unit]): Eval[Boolean] =
newdict flatMap input.set as true
newdict.flatTap(receive).flatMap(input.set).as(true)
}

def buildJson(buildTree: LogDict => Output): Eval[Json] = newdict flatTap buildTree flatMap (_.json)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tofu.logging

import cats.syntax.semigroup._
import org.scalatest.funsuite.AnyFunSuite
import tofu.logging.{LogTree, TethysBuilder}
import tofu.syntax.logRenderer._
class LogTreeSuite extends AnyFunSuite {

case class Data(field1: String, field2: Int)

object Data {
implicit object loggable extends DictLoggable[Data] with ToStringLoggable[Data] {
def fields[I, V, R, S](a: Data, i: I)(implicit r: LogRenderer[I, V, R, S]): R =
i.addString("field1", a.field1) |+|
i.addInt("field2", a.field2.toLong)
}

}

val value = Map("aaaa" -> List(Data("lol", 2), Data("kek", -1)))

test("LogTree and tethys builder results are consistent") {
assert(TethysBuilder(value) === LogTree(value).noSpaces)
}
}

0 comments on commit ac55b6e

Please sign in to comment.