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

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