Fabiana Cecin ce918b0819
chore: replace Option with Opt (#4035)
* change all usage of std.options.Option[T] to results.Opt[T]
* fix broken apps and examples (to validate refactor)
* removed all std/options code added for libp2p v2 migration
* add broker Opt codec to persistency/backend_comm.nim
* add a readValue overload for Opt[T] in tools/confutils/cli_args.nim
* keep Option where required (Presto, confutils' config_file.nim)
* Change generateRlnProof error handling
* Fix imports
2026-07-16 14:02:17 -03:00

48 lines
1.5 KiB
Nim

import results, std/options, typetraits, 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 Opt:
if value.isSome:
w.writeValue value.get
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.}