Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow functions to be passed to dispatch.New #7

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 13 additions & 7 deletions dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func New(opts ...Option) (*Dispatch, error) {
functions: map[string]dispatchproto.Function{},
}
for _, opt := range opts {
opt(d)
opt.configureDispatch(d)
}

// Prepare the endpoint URL.
Expand Down Expand Up @@ -123,14 +123,20 @@ func New(opts ...Option) (*Dispatch, error) {
}

// Option configures a Dispatch endpoint.
type Option func(*Dispatch)
type Option interface {
configureDispatch(*Dispatch)
}

type optionFunc func(*Dispatch)

func (f optionFunc) configureDispatch(d *Dispatch) { f(d) }

// EndpointUrl sets the URL of the Dispatch endpoint.
//
// It defaults to the value of the DISPATCH_ENDPOINT_URL environment
// variable.
func EndpointUrl(endpointUrl string) Option {
return func(d *Dispatch) { d.endpointUrl = endpointUrl }
return optionFunc(func(d *Dispatch) { d.endpointUrl = endpointUrl })
}

// VerificationKey sets the verification key to use when verifying
Expand All @@ -144,7 +150,7 @@ func EndpointUrl(endpointUrl string) Option {
// If a verification key is not provided, request signatures will
// not be validated.
func VerificationKey(verificationKey string) Option {
return func(d *Dispatch) { d.verificationKey = verificationKey }
return optionFunc(func(d *Dispatch) { d.verificationKey = verificationKey })
}

// ServeAddress sets the address that the Dispatch endpoint
Expand All @@ -157,15 +163,15 @@ func VerificationKey(verificationKey string) Option {
// variable, which is automatically set by the Dispatch CLI. If this
// is unset, it defaults to 127.0.0.1:8000.
func ServeAddress(addr string) Option {
return func(d *Dispatch) { d.serveAddr = addr }
return optionFunc(func(d *Dispatch) { d.serveAddr = addr })
}

// Env sets the environment variables that a Dispatch endpoint
// parses its default configuration from.
//
// It defaults to os.Environ().
func Env(env ...string) Option {
return func(d *Dispatch) { d.env = env }
return optionFunc(func(d *Dispatch) { d.env = env })
}

// Client sets the client to use when dispatching calls
Expand All @@ -177,7 +183,7 @@ func Env(env ...string) Option {
// control is required over client configuration, the custom
// client instance can be registered here and used instead.
func Client(client *dispatchclient.Client) Option {
return func(d *Dispatch) { d.client = client }
return optionFunc(func(d *Dispatch) { d.client = client })
}

// Register registers a function.
Expand Down
6 changes: 6 additions & 0 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ func (f *Function[I, O]) Gather(inputs []I, opts ...dispatchproto.CallOption) ([
return dispatchcoro.Gather[O](calls...)
}

func (f *Function[I, O]) configureDispatch(d *Dispatch) {
d.Register(f)
}

// AnyFunction is a Function[I, O] instance.
type AnyFunction interface {
Option

Register(*Dispatch) (string, dispatchproto.Function)
}
Loading