go.uber.org/yarpc@v1.72.1/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/common/extendemptyserver/server.go (about)

     1  // Code generated by thriftrw-plugin-yarpc
     2  // @generated
     3  
     4  package extendemptyserver
     5  
     6  import (
     7  	context "context"
     8  	stream "go.uber.org/thriftrw/protocol/stream"
     9  	wire "go.uber.org/thriftrw/wire"
    10  	transport "go.uber.org/yarpc/api/transport"
    11  	thrift "go.uber.org/yarpc/encoding/thrift"
    12  	common "go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/common"
    13  	emptyserviceserver "go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/common/emptyserviceserver"
    14  	yarpcerrors "go.uber.org/yarpc/yarpcerrors"
    15  )
    16  
    17  // Interface is the server-side interface for the ExtendEmpty service.
    18  type Interface interface {
    19  	emptyserviceserver.Interface
    20  
    21  	Hello(
    22  		ctx context.Context,
    23  	) error
    24  }
    25  
    26  // New prepares an implementation of the ExtendEmpty service for
    27  // registration.
    28  //
    29  //	handler := ExtendEmptyHandler{}
    30  //	dispatcher.Register(extendemptyserver.New(handler))
    31  func New(impl Interface, opts ...thrift.RegisterOption) []transport.Procedure {
    32  	h := handler{impl}
    33  	service := thrift.Service{
    34  		Name: "ExtendEmpty",
    35  		Methods: []thrift.Method{
    36  
    37  			thrift.Method{
    38  				Name: "hello",
    39  				HandlerSpec: thrift.HandlerSpec{
    40  
    41  					Type:   transport.Unary,
    42  					Unary:  thrift.UnaryHandler(h.Hello),
    43  					NoWire: hello_NoWireHandler{impl},
    44  				},
    45  				Signature:    "Hello()",
    46  				ThriftModule: common.ThriftModule,
    47  			},
    48  		},
    49  	}
    50  
    51  	procedures := make([]transport.Procedure, 0, 1)
    52  
    53  	procedures = append(
    54  		procedures,
    55  		emptyserviceserver.New(
    56  			impl,
    57  			append(
    58  				opts,
    59  				thrift.Named("ExtendEmpty"),
    60  			)...,
    61  		)...,
    62  	)
    63  	procedures = append(procedures, thrift.BuildProcedures(service, opts...)...)
    64  	return procedures
    65  }
    66  
    67  type handler struct{ impl Interface }
    68  
    69  type yarpcErrorNamer interface{ YARPCErrorName() string }
    70  
    71  type yarpcErrorCoder interface{ YARPCErrorCode() *yarpcerrors.Code }
    72  
    73  func (h handler) Hello(ctx context.Context, body wire.Value) (thrift.Response, error) {
    74  	var args common.ExtendEmpty_Hello_Args
    75  	if err := args.FromWire(body); err != nil {
    76  		return thrift.Response{}, yarpcerrors.InvalidArgumentErrorf(
    77  			"could not decode Thrift request for service 'ExtendEmpty' procedure 'Hello': %w", err)
    78  	}
    79  
    80  	appErr := h.impl.Hello(ctx)
    81  
    82  	hadError := appErr != nil
    83  	result, err := common.ExtendEmpty_Hello_Helper.WrapResponse(appErr)
    84  
    85  	var response thrift.Response
    86  	if err == nil {
    87  		response.IsApplicationError = hadError
    88  		response.Body = result
    89  		if namer, ok := appErr.(yarpcErrorNamer); ok {
    90  			response.ApplicationErrorName = namer.YARPCErrorName()
    91  		}
    92  		if extractor, ok := appErr.(yarpcErrorCoder); ok {
    93  			response.ApplicationErrorCode = extractor.YARPCErrorCode()
    94  		}
    95  		if appErr != nil {
    96  			response.ApplicationErrorDetails = appErr.Error()
    97  		}
    98  	}
    99  
   100  	return response, err
   101  }
   102  
   103  type hello_NoWireHandler struct{ impl Interface }
   104  
   105  func (h hello_NoWireHandler) HandleNoWire(ctx context.Context, nwc *thrift.NoWireCall) (thrift.NoWireResponse, error) {
   106  	var (
   107  		args common.ExtendEmpty_Hello_Args
   108  		rw   stream.ResponseWriter
   109  		err  error
   110  	)
   111  
   112  	rw, err = nwc.RequestReader.ReadRequest(ctx, nwc.EnvelopeType, nwc.Reader, &args)
   113  	if err != nil {
   114  		return thrift.NoWireResponse{}, yarpcerrors.InvalidArgumentErrorf(
   115  			"could not decode (via no wire) Thrift request for service 'ExtendEmpty' procedure 'Hello': %w", err)
   116  	}
   117  
   118  	appErr := h.impl.Hello(ctx)
   119  
   120  	hadError := appErr != nil
   121  	result, err := common.ExtendEmpty_Hello_Helper.WrapResponse(appErr)
   122  	response := thrift.NoWireResponse{ResponseWriter: rw}
   123  	if err == nil {
   124  		response.IsApplicationError = hadError
   125  		response.Body = result
   126  		if namer, ok := appErr.(yarpcErrorNamer); ok {
   127  			response.ApplicationErrorName = namer.YARPCErrorName()
   128  		}
   129  		if extractor, ok := appErr.(yarpcErrorCoder); ok {
   130  			response.ApplicationErrorCode = extractor.YARPCErrorCode()
   131  		}
   132  		if appErr != nil {
   133  			response.ApplicationErrorDetails = appErr.Error()
   134  		}
   135  	}
   136  	return response, err
   137  
   138  }