Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions waku/common/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package common

type ExtraOptions map[string]interface{}

type WakuConfig struct {
Host string `json:"host,omitempty"`
Nodekey string `json:"nodekey,omitempty"`
Expand Down
27 changes: 24 additions & 3 deletions waku/nwaku.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,26 +346,47 @@ func GoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) {
type WakuNode struct {
wakuCtx unsafe.Pointer
config *common.WakuConfig
extraOptions common.ExtraOptions
MsgChan chan common.Envelope
TopicHealthChan chan topicHealth
ConnectionChangeChan chan connectionChange
nodeName string
}

func NewWakuNode(config *common.WakuConfig, nodeName string) (*WakuNode, error) {
func NewWakuNode(config *common.WakuConfig, nodeName string, opts ...WakuNodeOption) (*WakuNode, error) {
Debug("Creating new WakuNode: %v", nodeName)
n := &WakuNode{
config: config,
nodeName: nodeName,
}

for _, o := range opts {
o(n)
}
Comment on lines +363 to +365
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do? not sure I follow it

Copy link
Member Author

@vpavlin vpavlin Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is why I provided a link to the article in the description: https://golang.cafe/blog/golang-functional-options-pattern.html

TL;DR: It allows you to define and passing functions that get called automatically which adds more flexibility to configuration than a custom struct - same approach has been used in go-waku (https://github.com/waku-org/go-waku/blob/master/waku/v2/node/wakuoptions.go#L135), so then you end up with something like:

NewNode(something,
  WithTimeout(..),
  WithRLN(...),
  WIthGabrielsBrain(...),
  ...
 )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh makes sense! Thanks so much! 🤩


wg := sync.WaitGroup{}

jsonConfig, err := json.Marshal(config)
if err != nil {
return nil, err
}

if len(n.extraOptions) > 0 {
configMap := make(common.ExtraOptions)
err = json.Unmarshal(jsonConfig, &configMap)
if err != nil {
return nil, err
}

for k, v := range n.extraOptions {
configMap[k] = v
}
jsonConfig, err = json.Marshal(configMap)
if err != nil {
return nil, err
}
}

var cJsonConfig = C.CString(string(jsonConfig))
var resp = C.allocResp(unsafe.Pointer(&wg))

Expand Down Expand Up @@ -1314,7 +1335,7 @@ func GetFreePortIfNeeded(tcpPort int, discV5UDPPort int) (int, int, error) {
}

// Create & start node
func StartWakuNode(nodeName string, customCfg *common.WakuConfig) (*WakuNode, error) {
func StartWakuNode(nodeName string, customCfg *common.WakuConfig, opts ...WakuNodeOption) (*WakuNode, error) {

Debug("Initializing %s", nodeName)

Expand All @@ -1339,7 +1360,7 @@ func StartWakuNode(nodeName string, customCfg *common.WakuConfig) (*WakuNode, er
}

Debug("Creating %s", nodeName)
node, err := NewWakuNode(&nodeCfg, nodeName)
node, err := NewWakuNode(&nodeCfg, nodeName, opts...)
if err != nil {
Error("Failed to create %s: %v", nodeName, err)
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions waku/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package waku

import "github.com/waku-org/waku-go-bindings/waku/common"

type WakuNodeOption func(*WakuNode)

// This allows you to pass arbitrary valid config options to Nwaku baed on https://github.com/waku-org/nwaku/blob/master/waku/factory/external_config.nim
// It is mostly for development and experimental purposes and will be removed in the future.
func WithExtraOptions(extraOptions common.ExtraOptions) WakuNodeOption {
return func(wn *WakuNode) {
wn.extraOptions = extraOptions
}
}