Skip to content

Commit

Permalink
Merge pull request #7 from dispatchrun/registration-improvement
Browse files Browse the repository at this point in the history
Allow functions to be passed to dispatch.New
  • Loading branch information
chriso authored Jun 24, 2024
2 parents 8f29874 + c679a41 commit 71183d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
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)
}

0 comments on commit 71183d8

Please sign in to comment.