go.uber.org/yarpc@v1.72.1/x/yarpctest/api/handler_unary.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package api 22 23 import ( 24 "context" 25 "testing" 26 27 "go.uber.org/yarpc/api/middleware" 28 "go.uber.org/yarpc/api/transport" 29 ) 30 31 // UnaryHandler is a wrapper around the transport.UnaryInbound and Lifecycle 32 // interfaces. 33 type UnaryHandler interface { 34 Lifecycle 35 36 transport.UnaryHandler 37 } 38 39 // UnaryHandlerFunc converts a function into a transport.UnaryHandler. 40 type UnaryHandlerFunc func(context.Context, *transport.Request, transport.ResponseWriter) error 41 42 // Handle implements yarpc/api/transport#UnaryHandler. 43 func (f UnaryHandlerFunc) Handle(ctx context.Context, req *transport.Request, resw transport.ResponseWriter) error { 44 return f(ctx, req, resw) 45 } 46 47 // Start is a noop for wrapped functions. 48 func (f UnaryHandlerFunc) Start(testing.TB) error { return nil } 49 50 // Stop is a noop for wrapped functions. 51 func (f UnaryHandlerFunc) Stop(testing.TB) error { return nil } 52 53 // UnaryInboundMiddleware is a wrapper around the middleware.UnaryInbound and 54 // Lifecycle interfaces. 55 type UnaryInboundMiddleware interface { 56 Lifecycle 57 58 middleware.UnaryInbound 59 } 60 61 // UnaryInboundMiddlewareFunc converts a function into a transport.UnaryInboundMiddleware. 62 type UnaryInboundMiddlewareFunc func(context.Context, *transport.Request, transport.ResponseWriter, transport.UnaryHandler) error 63 64 // Handle implements yarpc/api/transport#UnaryInboundMiddleware. 65 func (f UnaryInboundMiddlewareFunc) Handle(ctx context.Context, req *transport.Request, resw transport.ResponseWriter, h transport.UnaryHandler) error { 66 return f(ctx, req, resw, h) 67 } 68 69 // Start is a noop for wrapped functions. 70 func (f UnaryInboundMiddlewareFunc) Start(testing.TB) error { return nil } 71 72 // Stop is a noop for wrapped functions. 73 func (f UnaryInboundMiddlewareFunc) Stop(testing.TB) error { return nil } 74 75 // HandlerOpts are configuration options for a series of handlers. 76 type HandlerOpts struct { 77 Handlers []UnaryHandler 78 } 79 80 // HandlerOption defines options that can be passed into a handler. 81 type HandlerOption interface { 82 Lifecycle 83 84 ApplyHandler(opts *HandlerOpts) 85 } 86 87 // HandlerOptionFunc converts a function into a HandlerOption. 88 type HandlerOptionFunc func(opts *HandlerOpts) 89 90 // ApplyHandler implements HandlerOption. 91 func (f HandlerOptionFunc) ApplyHandler(opts *HandlerOpts) { f(opts) }