go.uber.org/yarpc@v1.72.1/encoding/thrift/internal/observabilitytest/test/testserviceserver/server.go (about) 1 // Code generated by thriftrw-plugin-yarpc 2 // @generated 3 4 // Copyright (c) 2022 Uber Technologies, Inc. 5 // 6 // Permission is hereby granted, free of charge, to any person obtaining a copy 7 // of this software and associated documentation files (the "Software"), to deal 8 // in the Software without restriction, including without limitation the rights 9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 // copies of the Software, and to permit persons to whom the Software is 11 // furnished to do so, subject to the following conditions: 12 // 13 // The above copyright notice and this permission notice shall be included in 14 // all copies or substantial portions of the Software. 15 // 16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 // THE SOFTWARE. 23 24 package testserviceserver 25 26 import ( 27 context "context" 28 stream "go.uber.org/thriftrw/protocol/stream" 29 wire "go.uber.org/thriftrw/wire" 30 transport "go.uber.org/yarpc/api/transport" 31 thrift "go.uber.org/yarpc/encoding/thrift" 32 test "go.uber.org/yarpc/encoding/thrift/internal/observabilitytest/test" 33 yarpcerrors "go.uber.org/yarpc/yarpcerrors" 34 ) 35 36 // Interface is the server-side interface for the TestService service. 37 type Interface interface { 38 Call( 39 ctx context.Context, 40 Key string, 41 ) (string, error) 42 } 43 44 // New prepares an implementation of the TestService service for 45 // registration. 46 // 47 // handler := TestServiceHandler{} 48 // dispatcher.Register(testserviceserver.New(handler)) 49 func New(impl Interface, opts ...thrift.RegisterOption) []transport.Procedure { 50 h := handler{impl} 51 service := thrift.Service{ 52 Name: "TestService", 53 Methods: []thrift.Method{ 54 55 thrift.Method{ 56 Name: "Call", 57 HandlerSpec: thrift.HandlerSpec{ 58 59 Type: transport.Unary, 60 Unary: thrift.UnaryHandler(h.Call), 61 NoWire: call_NoWireHandler{impl}, 62 }, 63 Signature: "Call(Key string) (string)", 64 ThriftModule: test.ThriftModule, 65 }, 66 }, 67 } 68 69 procedures := make([]transport.Procedure, 0, 1) 70 procedures = append(procedures, thrift.BuildProcedures(service, opts...)...) 71 return procedures 72 } 73 74 type handler struct{ impl Interface } 75 76 type yarpcErrorNamer interface{ YARPCErrorName() string } 77 78 type yarpcErrorCoder interface{ YARPCErrorCode() *yarpcerrors.Code } 79 80 func (h handler) Call(ctx context.Context, body wire.Value) (thrift.Response, error) { 81 var args test.TestService_Call_Args 82 if err := args.FromWire(body); err != nil { 83 return thrift.Response{}, yarpcerrors.InvalidArgumentErrorf( 84 "could not decode Thrift request for service 'TestService' procedure 'Call': %w", err) 85 } 86 87 success, appErr := h.impl.Call(ctx, args.Key) 88 89 hadError := appErr != nil 90 result, err := test.TestService_Call_Helper.WrapResponse(success, appErr) 91 92 var response thrift.Response 93 if err == nil { 94 response.IsApplicationError = hadError 95 response.Body = result 96 if namer, ok := appErr.(yarpcErrorNamer); ok { 97 response.ApplicationErrorName = namer.YARPCErrorName() 98 } 99 if extractor, ok := appErr.(yarpcErrorCoder); ok { 100 response.ApplicationErrorCode = extractor.YARPCErrorCode() 101 } 102 if appErr != nil { 103 response.ApplicationErrorDetails = appErr.Error() 104 } 105 } 106 107 return response, err 108 } 109 110 type call_NoWireHandler struct{ impl Interface } 111 112 func (h call_NoWireHandler) HandleNoWire(ctx context.Context, nwc *thrift.NoWireCall) (thrift.NoWireResponse, error) { 113 var ( 114 args test.TestService_Call_Args 115 rw stream.ResponseWriter 116 err error 117 ) 118 119 rw, err = nwc.RequestReader.ReadRequest(ctx, nwc.EnvelopeType, nwc.Reader, &args) 120 if err != nil { 121 return thrift.NoWireResponse{}, yarpcerrors.InvalidArgumentErrorf( 122 "could not decode (via no wire) Thrift request for service 'TestService' procedure 'Call': %w", err) 123 } 124 125 success, appErr := h.impl.Call(ctx, args.Key) 126 127 hadError := appErr != nil 128 result, err := test.TestService_Call_Helper.WrapResponse(success, appErr) 129 response := thrift.NoWireResponse{ResponseWriter: rw} 130 if err == nil { 131 response.IsApplicationError = hadError 132 response.Body = result 133 if namer, ok := appErr.(yarpcErrorNamer); ok { 134 response.ApplicationErrorName = namer.YARPCErrorName() 135 } 136 if extractor, ok := appErr.(yarpcErrorCoder); ok { 137 response.ApplicationErrorCode = extractor.YARPCErrorCode() 138 } 139 if appErr != nil { 140 response.ApplicationErrorDetails = appErr.Error() 141 } 142 } 143 return response, err 144 145 }