mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-02 05:53:11 +00:00
* introduce createNode # Conflicts: # apps/wakunode2/cli_args.nim * remove confutils dependency on the library * test: remove websocket in default test config * update to latest specs * test: cli_args * align to spec changes (sovereign, message conf, entrypoints * accept enr, entree and multiaddr as entry points * post rebase * format * change from "sovereign" to "core" * add example * get example to continue running * nitpicks * idiomatic constructors * fix enum naming * replace procs with consts * remove messageConfirmation * use pure enum * rename example file
45 lines
1.5 KiB
Nim
45 lines
1.5 KiB
Nim
import typetraits, options, tables, os, serialization, ./utils
|
|
|
|
type EnvvarWriter* = object
|
|
prefix: string
|
|
key: seq[string]
|
|
|
|
proc init*(T: type EnvvarWriter, prefix: string): T =
|
|
result.prefix = prefix
|
|
|
|
proc writeValue*(w: var EnvvarWriter, value: auto) =
|
|
mixin enumInstanceSerializedFields, writeValue, writeFieldIMPL
|
|
# TODO: reduce allocation
|
|
|
|
when value is string:
|
|
let key = constructKey(w.prefix, w.key)
|
|
os.putEnv(key, value)
|
|
elif value is (SomePrimitives or range):
|
|
let key = constructKey(w.prefix, w.key)
|
|
setValue(key, value)
|
|
elif value is Option:
|
|
if value.isSome:
|
|
w.writeValue value.get
|
|
elif value is (seq or array or openArray):
|
|
when uTypeIsPrimitives(type value):
|
|
let key = constructKey(w.prefix, w.key)
|
|
setValue(key, value)
|
|
elif uTypeIsRecord(type value):
|
|
let key = w.key[^1]
|
|
for i in 0 ..< value.len:
|
|
w.key[^1] = key & $i
|
|
w.writeValue(value[i])
|
|
else:
|
|
const typeName = typetraits.name(value.type)
|
|
{.fatal: "Failed to convert to Envvar array an unsupported type: " & typeName.}
|
|
elif value is (object or tuple):
|
|
type RecordType = type value
|
|
w.key.add ""
|
|
value.enumInstanceSerializedFields(fieldName, field):
|
|
w.key[^1] = fieldName
|
|
w.writeFieldIMPL(FieldTag[RecordType, fieldName], field, value)
|
|
discard w.key.pop()
|
|
else:
|
|
const typeName = typetraits.name(value.type)
|
|
{.fatal: "Failed to convert to Envvar an unsupported type: " & typeName.}
|