mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-02-19 05:13:07 +00:00
* Initial for liblogosdelivery library (static & dynamic) based on current state of API. * nix build support added. * logosdelivery_example * Added support for missing logLevel/logFormat in new API create_node * Added full JSON to NodeConfig support * Added ctx and ctx.myLib check to avoid uninitialzed calls and crash. Adjusted logosdelivery_example with proper error handling and JSON config format * target aware install phase * Fix base64 decode of payload
28 lines
854 B
Nim
28 lines
854 B
Nim
import std/[json, macros]
|
|
|
|
type JsonEvent*[T] = ref object
|
|
eventType*: string
|
|
payload*: T
|
|
|
|
macro toFlatJson*(event: JsonEvent): JsonNode =
|
|
## Serializes JsonEvent[T] to flat JSON with eventType first,
|
|
## followed by all fields from T's payload
|
|
result = quote:
|
|
var jsonObj = newJObject()
|
|
jsonObj["eventType"] = %`event`.eventType
|
|
|
|
# Serialize payload fields into the same object (flattening)
|
|
let payloadJson = %`event`.payload
|
|
for key, val in payloadJson.pairs:
|
|
jsonObj[key] = val
|
|
|
|
jsonObj
|
|
|
|
proc `$`*[T](event: JsonEvent[T]): string =
|
|
$toFlatJson(event)
|
|
|
|
proc newJsonEvent*[T](eventType: string, payload: T): JsonEvent[T] =
|
|
## Creates a new JsonEvent with the given eventType and payload.
|
|
## The payload's fields will be flattened into the JSON output.
|
|
JsonEvent[T](eventType: eventType, payload: payload)
|