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

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