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

     1  // Code generated by thriftrw-plugin-yarpc
     2  // @generated
     3  
     4  package storeserver
     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  	readonlystoreserver "go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/atomic/readonlystoreserver"
    14  	yarpcerrors "go.uber.org/yarpc/yarpcerrors"
    15  )
    16  
    17  // Interface is the server-side interface for the Store service.
    18  type Interface interface {
    19  	readonlystoreserver.Interface
    20  
    21  	CompareAndSwap(
    22  		ctx context.Context,
    23  		Request *atomic.CompareAndSwap,
    24  	) error
    25  
    26  	Forget(
    27  		ctx context.Context,
    28  		Key *string,
    29  	) error
    30  
    31  	Increment(
    32  		ctx context.Context,
    33  		Key *string,
    34  		Value *int64,
    35  	) error
    36  }
    37  
    38  // New prepares an implementation of the Store service for
    39  // registration.
    40  //
    41  //	handler := StoreHandler{}
    42  //	dispatcher.Register(storeserver.New(handler))
    43  func New(impl Interface, opts ...thrift.RegisterOption) []transport.Procedure {
    44  	h := handler{impl}
    45  	service := thrift.Service{
    46  		Name: "Store",
    47  		Methods: []thrift.Method{
    48  
    49  			thrift.Method{
    50  				Name: "compareAndSwap",
    51  				HandlerSpec: thrift.HandlerSpec{
    52  
    53  					Type:   transport.Unary,
    54  					Unary:  thrift.UnaryHandler(h.CompareAndSwap),
    55  					NoWire: compareandswap_NoWireHandler{impl},
    56  				},
    57  				Signature:    "CompareAndSwap(Request *atomic.CompareAndSwap)",
    58  				ThriftModule: atomic.ThriftModule,
    59  			},
    60  
    61  			thrift.Method{
    62  				Name: "forget",
    63  				HandlerSpec: thrift.HandlerSpec{
    64  
    65  					Type:   transport.Oneway,
    66  					Oneway: thrift.OnewayHandler(h.Forget),
    67  					NoWire: forget_NoWireHandler{impl},
    68  				},
    69  				Signature:    "Forget(Key *string)",
    70  				ThriftModule: atomic.ThriftModule,
    71  			},
    72  
    73  			thrift.Method{
    74  				Name: "increment",
    75  				HandlerSpec: thrift.HandlerSpec{
    76  
    77  					Type:   transport.Unary,
    78  					Unary:  thrift.UnaryHandler(h.Increment),
    79  					NoWire: increment_NoWireHandler{impl},
    80  				},
    81  				Signature:    "Increment(Key *string, Value *int64)",
    82  				ThriftModule: atomic.ThriftModule,
    83  			},
    84  		},
    85  	}
    86  
    87  	procedures := make([]transport.Procedure, 0, 3)
    88  
    89  	procedures = append(
    90  		procedures,
    91  		readonlystoreserver.New(
    92  			impl,
    93  			append(
    94  				opts,
    95  				thrift.Named("Store"),
    96  			)...,
    97  		)...,
    98  	)
    99  	procedures = append(procedures, thrift.BuildProcedures(service, opts...)...)
   100  	return procedures
   101  }
   102  
   103  type handler struct{ impl Interface }
   104  
   105  type yarpcErrorNamer interface{ YARPCErrorName() string }
   106  
   107  type yarpcErrorCoder interface{ YARPCErrorCode() *yarpcerrors.Code }
   108  
   109  func (h handler) CompareAndSwap(ctx context.Context, body wire.Value) (thrift.Response, error) {
   110  	var args atomic.Store_CompareAndSwap_Args
   111  	if err := args.FromWire(body); err != nil {
   112  		return thrift.Response{}, yarpcerrors.InvalidArgumentErrorf(
   113  			"could not decode Thrift request for service 'Store' procedure 'CompareAndSwap': %w", err)
   114  	}
   115  
   116  	appErr := h.impl.CompareAndSwap(ctx, args.Request)
   117  
   118  	hadError := appErr != nil
   119  	result, err := atomic.Store_CompareAndSwap_Helper.WrapResponse(appErr)
   120  
   121  	var response thrift.Response
   122  	if err == nil {
   123  		response.IsApplicationError = hadError
   124  		response.Body = result
   125  		if namer, ok := appErr.(yarpcErrorNamer); ok {
   126  			response.ApplicationErrorName = namer.YARPCErrorName()
   127  		}
   128  		if extractor, ok := appErr.(yarpcErrorCoder); ok {
   129  			response.ApplicationErrorCode = extractor.YARPCErrorCode()
   130  		}
   131  		if appErr != nil {
   132  			response.ApplicationErrorDetails = appErr.Error()
   133  		}
   134  	}
   135  
   136  	return response, err
   137  }
   138  
   139  func (h handler) Forget(ctx context.Context, body wire.Value) error {
   140  	var args atomic.Store_Forget_Args
   141  	if err := args.FromWire(body); err != nil {
   142  		return err
   143  	}
   144  
   145  	return h.impl.Forget(ctx, args.Key)
   146  }
   147  
   148  func (h handler) Increment(ctx context.Context, body wire.Value) (thrift.Response, error) {
   149  	var args atomic.Store_Increment_Args
   150  	if err := args.FromWire(body); err != nil {
   151  		return thrift.Response{}, yarpcerrors.InvalidArgumentErrorf(
   152  			"could not decode Thrift request for service 'Store' procedure 'Increment': %w", err)
   153  	}
   154  
   155  	appErr := h.impl.Increment(ctx, args.Key, args.Value)
   156  
   157  	hadError := appErr != nil
   158  	result, err := atomic.Store_Increment_Helper.WrapResponse(appErr)
   159  
   160  	var response thrift.Response
   161  	if err == nil {
   162  		response.IsApplicationError = hadError
   163  		response.Body = result
   164  		if namer, ok := appErr.(yarpcErrorNamer); ok {
   165  			response.ApplicationErrorName = namer.YARPCErrorName()
   166  		}
   167  		if extractor, ok := appErr.(yarpcErrorCoder); ok {
   168  			response.ApplicationErrorCode = extractor.YARPCErrorCode()
   169  		}
   170  		if appErr != nil {
   171  			response.ApplicationErrorDetails = appErr.Error()
   172  		}
   173  	}
   174  
   175  	return response, err
   176  }
   177  
   178  type compareandswap_NoWireHandler struct{ impl Interface }
   179  
   180  func (h compareandswap_NoWireHandler) HandleNoWire(ctx context.Context, nwc *thrift.NoWireCall) (thrift.NoWireResponse, error) {
   181  	var (
   182  		args atomic.Store_CompareAndSwap_Args
   183  		rw   stream.ResponseWriter
   184  		err  error
   185  	)
   186  
   187  	rw, err = nwc.RequestReader.ReadRequest(ctx, nwc.EnvelopeType, nwc.Reader, &args)
   188  	if err != nil {
   189  		return thrift.NoWireResponse{}, yarpcerrors.InvalidArgumentErrorf(
   190  			"could not decode (via no wire) Thrift request for service 'Store' procedure 'CompareAndSwap': %w", err)
   191  	}
   192  
   193  	appErr := h.impl.CompareAndSwap(ctx, args.Request)
   194  
   195  	hadError := appErr != nil
   196  	result, err := atomic.Store_CompareAndSwap_Helper.WrapResponse(appErr)
   197  	response := thrift.NoWireResponse{ResponseWriter: rw}
   198  	if err == nil {
   199  		response.IsApplicationError = hadError
   200  		response.Body = result
   201  		if namer, ok := appErr.(yarpcErrorNamer); ok {
   202  			response.ApplicationErrorName = namer.YARPCErrorName()
   203  		}
   204  		if extractor, ok := appErr.(yarpcErrorCoder); ok {
   205  			response.ApplicationErrorCode = extractor.YARPCErrorCode()
   206  		}
   207  		if appErr != nil {
   208  			response.ApplicationErrorDetails = appErr.Error()
   209  		}
   210  	}
   211  	return response, err
   212  
   213  }
   214  
   215  type forget_NoWireHandler struct{ impl Interface }
   216  
   217  func (h forget_NoWireHandler) HandleNoWire(ctx context.Context, nwc *thrift.NoWireCall) (thrift.NoWireResponse, error) {
   218  	var (
   219  		args atomic.Store_Forget_Args
   220  
   221  		err error
   222  	)
   223  
   224  	if _, err = nwc.RequestReader.ReadRequest(ctx, nwc.EnvelopeType, nwc.Reader, &args); err != nil {
   225  		return thrift.NoWireResponse{}, yarpcerrors.InvalidArgumentErrorf(
   226  			"could not decode (via no wire) Thrift request for service 'Store' procedure 'Forget': %w", err)
   227  	}
   228  
   229  	return thrift.NoWireResponse{}, h.impl.Forget(ctx, args.Key)
   230  
   231  }
   232  
   233  type increment_NoWireHandler struct{ impl Interface }
   234  
   235  func (h increment_NoWireHandler) HandleNoWire(ctx context.Context, nwc *thrift.NoWireCall) (thrift.NoWireResponse, error) {
   236  	var (
   237  		args atomic.Store_Increment_Args
   238  		rw   stream.ResponseWriter
   239  		err  error
   240  	)
   241  
   242  	rw, err = nwc.RequestReader.ReadRequest(ctx, nwc.EnvelopeType, nwc.Reader, &args)
   243  	if err != nil {
   244  		return thrift.NoWireResponse{}, yarpcerrors.InvalidArgumentErrorf(
   245  			"could not decode (via no wire) Thrift request for service 'Store' procedure 'Increment': %w", err)
   246  	}
   247  
   248  	appErr := h.impl.Increment(ctx, args.Key, args.Value)
   249  
   250  	hadError := appErr != nil
   251  	result, err := atomic.Store_Increment_Helper.WrapResponse(appErr)
   252  	response := thrift.NoWireResponse{ResponseWriter: rw}
   253  	if err == nil {
   254  		response.IsApplicationError = hadError
   255  		response.Body = result
   256  		if namer, ok := appErr.(yarpcErrorNamer); ok {
   257  			response.ApplicationErrorName = namer.YARPCErrorName()
   258  		}
   259  		if extractor, ok := appErr.(yarpcErrorCoder); ok {
   260  			response.ApplicationErrorCode = extractor.YARPCErrorCode()
   261  		}
   262  		if appErr != nil {
   263  			response.ApplicationErrorDetails = appErr.Error()
   264  		}
   265  	}
   266  	return response, err
   267  
   268  }