Skip to content

Commit 6764ae1

Browse files
Respond to PR comments
1 parent cb760d1 commit 6764ae1

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

nexus/operation.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import (
1515
// )}
1616
type NoValue *struct{}
1717

18-
// OperationOptions contains the general options for an operation, across different handlers.
18+
// HandlerInfo contains the general information for an operation invocation, across different handlers.
1919
//
2020
// NOTE: Experimental
21-
type OperationOptions struct {
22-
// ServiceName is the name of the service that contains the operation.
23-
ServiceName string
24-
// OperationName is the name of the operation.
25-
OperationName string
21+
type HandlerInfo struct {
22+
// Service is the name of the service that contains the operation.
23+
Service string
24+
// Operation is the name of the operation.
25+
Operation string
2626
// Header contains the request header fields either received by the server or to be sent by the client.
2727
//
2828
// Header will always be non empty in server methods and can be optionally set in the client API.
@@ -34,11 +34,11 @@ type OperationOptions struct {
3434
Header Header
3535
}
3636

37-
// MiddlewareFunc is a function which receives an OperationInvoker and returns another OperationInvoker.
37+
// MiddlewareFunc is a function which receives an OperationHandler and returns another OperationHandler.
3838
// If the middleware wants to stop the chain before any handler is called, it can return an error.
3939
//
4040
// NOTE: Experimental
41-
type MiddlewareFunc func(OperationOptions, OperationInvoker[any, any]) (OperationInvoker[any, any], error)
41+
type MiddlewareFunc func(HandlerInfo, OperationHandler[any, any]) (OperationHandler[any, any], error)
4242

4343
// OperationReference provides a typed interface for invoking operations. Every [Operation] is also an
4444
// [OperationReference]. Callers may create references using [NewOperationReference] when the implementation is not
@@ -95,10 +95,11 @@ type RegisterableOperation interface {
9595
type Operation[I, O any] interface {
9696
RegisterableOperation
9797
OperationReference[I, O]
98-
OperationInvoker[I, O]
98+
OperationHandler[I, O]
9999
}
100100

101-
type OperationInvoker[I, O any] interface {
101+
// OperationHandler is the interface for the core operation methods.
102+
type OperationHandler[I, O any] interface {
102103
// Start handles requests for starting an operation. Return [HandlerStartOperationResultSync] to respond
103104
// successfully - inline, or [HandlerStartOperationResultAsync] to indicate that an asynchronous operation was
104105
// started. Return an [OperationError] to indicate that an operation completed as failed or
@@ -274,17 +275,17 @@ type registryHandler struct {
274275
middlewares []MiddlewareFunc
275276
}
276277

277-
func (r *registryHandler) getOperation(options OperationOptions) (OperationInvoker[any, any], error) {
278-
s, ok := r.services[options.ServiceName]
278+
func (r *registryHandler) operationHandler(options HandlerInfo) (OperationHandler[any, any], error) {
279+
s, ok := r.services[options.Service]
279280
if !ok {
280-
return nil, HandlerErrorf(HandlerErrorTypeNotFound, "service %q not found", options.ServiceName)
281+
return nil, HandlerErrorf(HandlerErrorTypeNotFound, "service %q not found", options.Service)
281282
}
282-
h, ok := s.operations[options.OperationName]
283+
h, ok := s.operations[options.Operation]
283284
if !ok {
284-
return nil, HandlerErrorf(HandlerErrorTypeNotFound, "operation %q not found", options.OperationName)
285+
return nil, HandlerErrorf(HandlerErrorTypeNotFound, "operation %q not found", options.Operation)
285286
}
286287

287-
var handler OperationInvoker[any, any]
288+
var handler OperationHandler[any, any]
288289
handler = &rootOperationHandler{h: h}
289290
if h != nil && len(r.middlewares) > 0 {
290291
for i := len(r.middlewares) - 1; i >= 0; i-- {
@@ -345,40 +346,40 @@ func (r *rootOperationHandler) Start(ctx context.Context, input interface{}, opt
345346
return ret.(HandlerStartOperationResult[any]), nil
346347
}
347348

348-
var _ OperationInvoker[any, any] = &rootOperationHandler{}
349+
var _ OperationHandler[any, any] = &rootOperationHandler{}
349350

350351
// CancelOperation implements Handler.
351352
func (r *registryHandler) CancelOperation(ctx context.Context, service, operation string, operationID string, options CancelOperationOptions) error {
352-
h, err := r.getOperation(OperationOptions{
353-
ServiceName: service,
354-
OperationName: operation,
355-
Header: options.Header,
353+
h, err := r.operationHandler(HandlerInfo{
354+
Service: service,
355+
Operation: operation,
356+
Header: options.Header,
356357
})
357358
if err != nil {
358359
return err
359360
}
360361
return h.Cancel(ctx, operationID, options)
361362
}
362363

363-
// GetOperationInfo implements Handler.
364+
// operationHandlerInfo implements Handler.
364365
func (r *registryHandler) GetOperationInfo(ctx context.Context, service, operation string, operationID string, options GetOperationInfoOptions) (*OperationInfo, error) {
365-
h, err := r.getOperation(OperationOptions{
366-
ServiceName: service,
367-
OperationName: operation,
368-
Header: options.Header,
366+
h, err := r.operationHandler(HandlerInfo{
367+
Service: service,
368+
Operation: operation,
369+
Header: options.Header,
369370
})
370371
if err != nil {
371372
return nil, err
372373
}
373374
return h.GetInfo(ctx, operationID, options)
374375
}
375376

376-
// GetOperationResult implements Handler.
377+
// operationHandlerResult implements Handler.
377378
func (r *registryHandler) GetOperationResult(ctx context.Context, service, operation string, operationID string, options GetOperationResultOptions) (any, error) {
378-
h, err := r.getOperation(OperationOptions{
379-
ServiceName: service,
380-
OperationName: operation,
381-
Header: options.Header,
379+
h, err := r.operationHandler(HandlerInfo{
380+
Service: service,
381+
Operation: operation,
382+
Header: options.Header,
382383
})
383384
if err != nil {
384385
return nil, err
@@ -397,10 +398,10 @@ func (r *registryHandler) StartOperation(ctx context.Context, service, operation
397398
return nil, HandlerErrorf(HandlerErrorTypeNotFound, "operation %q not found", operation)
398399
}
399400

400-
h, err := r.getOperation(OperationOptions{
401-
ServiceName: service,
402-
OperationName: operation,
403-
Header: options.Header,
401+
h, err := r.operationHandler(HandlerInfo{
402+
Service: service,
403+
Operation: operation,
404+
Header: options.Header,
404405
})
405406
if err != nil {
406407
return nil, err

nexus/operation_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,16 @@ func TestOperationInterceptor(t *testing.T) {
306306
}
307307

308308
func newAuthMiddleware(authKey string) MiddlewareFunc {
309-
return func(oo OperationOptions, uo OperationInvoker[any, any]) (OperationInvoker[any, any], error) {
310-
if oo.Header.Get("authorization") != authKey {
309+
return func(hi HandlerInfo, uo OperationHandler[any, any]) (OperationHandler[any, any], error) {
310+
if hi.Header.Get("authorization") != authKey {
311311
return nil, HandlerErrorf(HandlerErrorTypeUnauthorized, "unauthorized")
312312
}
313313
return uo, nil
314314
}
315315
}
316316

317317
type loggingOperation struct {
318-
Operation OperationInvoker[any, any]
318+
Operation OperationHandler[any, any]
319319
name string
320320
output func(string)
321321
}
@@ -341,10 +341,10 @@ func (lo *loggingOperation) GetInfo(ctx context.Context, id string, options GetO
341341
}
342342

343343
func newLoggingMiddleware(output func(string)) MiddlewareFunc {
344-
return func(oo OperationOptions, uo OperationInvoker[any, any]) (OperationInvoker[any, any], error) {
344+
return func(hi HandlerInfo, uo OperationHandler[any, any]) (OperationHandler[any, any], error) {
345345
return &loggingOperation{
346346
uo,
347-
oo.OperationName,
347+
hi.Operation,
348348
output,
349349
}, nil
350350
}

0 commit comments

Comments
 (0)