little macro

This commit is contained in:
fryorcraken 2025-04-07 16:32:26 +10:00
parent 5bd5b72706
commit 23abf3466b
3 changed files with 30 additions and 15 deletions

View File

@ -105,7 +105,7 @@ proc networkConfiguration*(conf: WakuConf, clientId: string): NetConfigResult =
# Resolve and use DNS domain IP
if conf.dns4DomainName.isSome() and extIp.isNone():
try:
let dnsRes = waitFor dnsResolve(conf.dns4DomainName, conf)
let dnsRes = waitFor dnsResolve(conf.dns4DomainName.get(), conf)
if dnsRes.isErr():
return err($dnsRes.error) # Pass error down the stack

View File

@ -48,6 +48,7 @@ type WakuConf* = ref object
filter*: bool
lightPush*: bool
peerExchange*: bool
storeSync*: bool
discv5Conf*: Option[Discv5Conf]

View File

@ -1,7 +1,7 @@
import
libp2p/crypto/crypto,
libp2p/multiaddress,
std/[net, options, sequtils],
std/[macros, net, options, sequtils, strutils],
chronicles,
results
@ -174,6 +174,7 @@ type WakuConfBuilder* = ref object
filter: Option[bool]
lightPush: Option[bool]
peerExchange: Option[bool]
storeSync: Option[bool]
clusterConf: Option[ClusterConf]
@ -198,23 +199,29 @@ proc init*(T: type WakuConfBuilder): WakuConfBuilder =
rlnRelayConf: RlnRelayConfBuilder.init(), discv5Conf: Discv5ConfBuilder.init()
)
proc withClusterConf*(builder: var WakuConfBuilder, clusterConf: ClusterConf) =
builder.clusterConf = some(clusterConf)
macro confWith(argName: untyped, argType: untyped) =
argName.expectKind nnkIdent
argType.expectKind nnkIdent
proc withNodeKey*(builder: var WakuConfBuilder, nodeKey: PrivateKey) =
builder.nodeKey = some(nodeKey)
result = newStmtList()
proc withClusterId*(builder: var WakuConfBuilder, clusterId: uint16) =
builder.clusterid = some(clusterId)
let procName = ident("with" & capitalizeAscii($argName))
let builderIdent = ident("builder")
let builderVar = newDotExpr(builderIdent, ident($argName))
let resVar = ident($argName)
proc withShards*(builder: var WakuConfBuilder, shards: seq[uint16]) =
builder.shards = some(shards)
result.add quote do:
proc `procName`*(`builderIdent`: var WakuConfBuilder, `resVar`: `argType`) =
`builderVar` = some(`argName`)
proc withRelay*(builder: var WakuConfBuilder, relay: bool) =
builder.relay = some(relay)
proc withMaxMessageSizeBytes*(builder: var WakuConfBuilder, maxMessageSizeBytes: int) =
builder.maxMessageSizeBytes = some(maxMessageSizeBytes)
confWith(clusterConf, ClusterConf)
confWith(nodeKey, PrivateKey)
confWith(clusterId, uint16)
confWith(shards, seq[uint16])
confWith(relay, bool)
confWith(filter, bool)
confWith(storeSync, bool)
confWith(maxMessageSizeBytes, int)
proc withDiscv5*(builder: var WakuConfBuilder, discv5: bool) =
builder.discv5Conf.withDiscv5(discv5)
@ -283,6 +290,13 @@ proc build*(
warn "whether to mount peerExchange is not specified, defaulting to not mounting"
false
let storeSync =
if builder.storeSync.isSome:
builder.storeSync.get()
else:
warn "whether to mount storeSync is not specified, defaulting to not mounting"
false
# Apply cluster conf - values passed manually override cluster conf
# Should be applied **first**, before individual values are pulled
if builder.clusterConf.isSome: