trpc.group/trpc-go/trpc-go@v1.0.3/testdata/trpc/helloworld/helloworld.trpc.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  // Code generated by trpc-go/trpc-cmdline. DO NOT EDIT.
    15  // source: helloworld.proto
    16  
    17  package helloworld
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	_ "trpc.group/trpc-go/trpc-go"
    24  	_ "trpc.group/trpc-go/trpc-go/http"
    25  
    26  	"trpc.group/trpc-go/trpc-go/client"
    27  	"trpc.group/trpc-go/trpc-go/codec"
    28  	"trpc.group/trpc-go/trpc-go/server"
    29  )
    30  
    31  /* ************************************ Service Definition ************************************ */
    32  
    33  // GreeterService defines service
    34  type GreeterService interface {
    35  	SayHello(ctx context.Context, req *HelloRequest) (*HelloReply, error)
    36  	SayHi(ctx context.Context, req *HelloRequest) (*HelloReply, error)
    37  }
    38  
    39  func GreeterService_SayHello_Handler(svr interface{}, ctx context.Context, f server.FilterFunc) (rspBody interface{}, err error) {
    40  	req := &HelloRequest{}
    41  	filters, err := f(req)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	if len(filters) == 0 {
    46  		return svr.(GreeterService).SayHello(ctx, req)
    47  	}
    48  	handleFunc := func(ctx context.Context, reqBody interface{}) (interface{}, error) {
    49  		return svr.(GreeterService).SayHello(ctx, reqBody.(*HelloRequest))
    50  	}
    51  	return filters.Filter(ctx, req, handleFunc)
    52  }
    53  
    54  func GreeterService_SayHi_Handler(svr interface{}, ctx context.Context, f server.FilterFunc) (rspBody interface{}, err error) {
    55  	req := &HelloRequest{}
    56  	filters, err := f(req)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	handleFunc := func(ctx context.Context, reqBody interface{}) (interface{}, error) {
    61  		return svr.(GreeterService).SayHi(ctx, reqBody.(*HelloRequest))
    62  	}
    63  
    64  	return filters.Filter(ctx, req, handleFunc)
    65  }
    66  
    67  // GreeterServer_ServiceDesc descriptor for server.RegisterService
    68  var GreeterServer_ServiceDesc = server.ServiceDesc{
    69  	ServiceName: "trpc.test.helloworld.Greeter",
    70  	HandlerType: ((*GreeterService)(nil)),
    71  	Methods: []server.Method{
    72  		{
    73  			Name: "/trpc.test.helloworld.Greeter/SayHello",
    74  			Func: GreeterService_SayHello_Handler,
    75  		},
    76  		{
    77  			Name: "/trpc.test.helloworld.Greeter/SayHi",
    78  			Func: GreeterService_SayHi_Handler,
    79  		},
    80  	},
    81  }
    82  
    83  // RegisterGreeterService register service
    84  func RegisterGreeterService(s server.Service, svr GreeterService) {
    85  	if err := s.Register(&GreeterServer_ServiceDesc, svr); err != nil {
    86  		panic(fmt.Sprintf("Greeter register error:%v", err))
    87  	}
    88  
    89  }
    90  
    91  /* ************************************ Client Definition ************************************ */
    92  
    93  // GreeterClientProxy defines service client proxy
    94  type GreeterClientProxy interface {
    95  	SayHello(ctx context.Context, req *HelloRequest, opts ...client.Option) (rsp *HelloReply, err error)
    96  
    97  	SayHi(ctx context.Context, req *HelloRequest, opts ...client.Option) (rsp *HelloReply, err error)
    98  }
    99  
   100  type GreeterClientProxyImpl struct {
   101  	client client.Client
   102  	opts   []client.Option
   103  }
   104  
   105  var NewGreeterClientProxy = func(opts ...client.Option) GreeterClientProxy {
   106  	return &GreeterClientProxyImpl{client: client.DefaultClient, opts: opts}
   107  }
   108  
   109  func (c *GreeterClientProxyImpl) SayHello(ctx context.Context, req *HelloRequest, opts ...client.Option) (rsp *HelloReply, err error) {
   110  
   111  	ctx, msg := codec.WithCloneMessage(ctx)
   112  
   113  	msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello")
   114  	msg.WithCalleeServiceName(GreeterServer_ServiceDesc.ServiceName)
   115  	msg.WithCalleeApp("test")
   116  	msg.WithCalleeServer("helloworld")
   117  	msg.WithCalleeService("Greeter")
   118  	msg.WithCalleeMethod("SayHello")
   119  	msg.WithSerializationType(codec.SerializationTypePB)
   120  
   121  	callopts := make([]client.Option, 0, len(c.opts)+len(opts))
   122  	callopts = append(callopts, c.opts...)
   123  	callopts = append(callopts, opts...)
   124  
   125  	rsp = &HelloReply{}
   126  
   127  	err = c.client.Invoke(ctx, req, rsp, callopts...)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	codec.PutBackMessage(msg)
   132  
   133  	return rsp, nil
   134  }
   135  
   136  func (c *GreeterClientProxyImpl) SayHi(ctx context.Context, req *HelloRequest, opts ...client.Option) (rsp *HelloReply, err error) {
   137  
   138  	ctx, msg := codec.WithCloneMessage(ctx)
   139  
   140  	msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHi")
   141  	msg.WithCalleeServiceName(GreeterServer_ServiceDesc.ServiceName)
   142  	msg.WithCalleeApp("test")
   143  	msg.WithCalleeServer("helloworld")
   144  	msg.WithCalleeService("Greeter")
   145  	msg.WithCalleeMethod("SayHi")
   146  	msg.WithSerializationType(codec.SerializationTypePB)
   147  
   148  	callopts := make([]client.Option, 0, len(c.opts)+len(opts))
   149  	callopts = append(callopts, c.opts...)
   150  	callopts = append(callopts, opts...)
   151  
   152  	rsp = &HelloReply{}
   153  
   154  	err = c.client.Invoke(ctx, req, rsp, callopts...)
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  	codec.PutBackMessage(msg)
   159  
   160  	return rsp, nil
   161  }