t22478.nim 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. discard """
  2. matrix: "-d:nimNoLentIterators --mm:arc"
  3. output: '''PUSH DATA: {"test.message":{"test":{"nested":"v1"}}}'''
  4. joinable: false
  5. """
  6. # bug #22748
  7. import std/[json, typetraits, times]
  8. # publish
  9. proc publish*[T](payload: T) =
  10. discard
  11. type MetricsPoint* = JsonNode
  12. proc push*(stat: string, data: JsonNode, usec: int64 = 0) =
  13. let payload = newJObject()
  14. # this results in a infinite recursion unless we deepCopy()
  15. payload[stat] = data #.deepCopy
  16. echo "PUSH DATA: ", payload
  17. publish[MetricsPoint](payload)
  18. var scopes {.threadvar.}: seq[JsonNode]
  19. type WithTimeCallback*[T] = proc(data: var JsonNode): T
  20. proc pushScoped*[T](metric: string, blk: WithTimeCallback[T]): T {.gcsafe.} =
  21. scopes.add newJObject()
  22. defer: discard scopes.pop()
  23. let stc = (cpuTime() * 1000_000).int64
  24. result = blk(scopes[^1])
  25. let dfc = (cpuTime() * 1000_000).int64 - stc
  26. push(metric, scopes[^1], dfc)
  27. # demo code
  28. discard pushScoped[int]("test.message") do (data: var JsonNode) -> int:
  29. data["test"] = %*{
  30. "nested": "v1"
  31. }