isle/go/daemon/ctx.go

32 lines
982 B
Go

package daemon
import (
"context"
"isle/daemon/jsonrpc2"
"github.com/stretchr/testify/mock"
)
const metaKeyNetworkSearchStr = "daemon.networkSearchStr"
// WithNetwork returns the Context so that, when used against a daemon RPC
// endpoint, the endpoint knows which network is being targetted for the call.
// The network can be identified by its ID, name, or domain.
func WithNetwork(ctx context.Context, searchStr string) context.Context {
return jsonrpc2.WithMeta(ctx, metaKeyNetworkSearchStr, searchStr)
}
func getNetworkSearchStr(ctx context.Context) string {
v, _ := jsonrpc2.GetMeta(ctx)[metaKeyNetworkSearchStr].(string)
return v
}
// MockContextWithNetwork returns a value which can be used with the
// tesstify/mock package to match a context which has a search string added to
// it by WithNetwork.
func MockContextWithNetwork(searchStr string) any {
return mock.MatchedBy(func(ctx context.Context) bool {
return getNetworkSearchStr(ctx) == searchStr
})
}