trpc.group/trpc-go/trpc-go@v1.0.3/testdata/restful/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 v2.0.13. 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/client"
    25  	"trpc.group/trpc-go/trpc-go/codec"
    26  	_ "trpc.group/trpc-go/trpc-go/http"
    27  	"trpc.group/trpc-go/trpc-go/restful"
    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  }
    37  
    38  func GreeterService_SayHello_Handler(svr interface{}, ctx context.Context, f server.FilterFunc) (interface{}, error) {
    39  	req := &HelloRequest{}
    40  	filters, err := f(req)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	handleFunc := func(ctx context.Context, reqBody interface{}) (interface{}, error) {
    45  		return svr.(GreeterService).SayHello(ctx, reqBody.(*HelloRequest))
    46  	}
    47  
    48  	var rsp interface{}
    49  	rsp, err = filters.Filter(ctx, req, handleFunc)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return rsp, nil
    54  }
    55  
    56  // requestBodyGreeterServiceSayHelloRESTfulPath0 POST: /v1/foobar
    57  type requestBodyGreeterServiceSayHelloRESTfulPath0 struct{}
    58  
    59  func (requestBodyGreeterServiceSayHelloRESTfulPath0) Locate(message restful.ProtoMessage) interface{} {
    60  	x := message.(*HelloRequest)
    61  	return x
    62  }
    63  
    64  func (requestBodyGreeterServiceSayHelloRESTfulPath0) Body() string {
    65  	return "*"
    66  }
    67  
    68  // GreeterServer_ServiceDesc descriptor for server.RegisterService
    69  var GreeterServer_ServiceDesc = server.ServiceDesc{
    70  	ServiceName: "trpc.examples.restful.helloworld.Greeter",
    71  	HandlerType: ((*GreeterService)(nil)),
    72  	Methods: []server.Method{
    73  		{
    74  			Name: "/trpc.examples.restful.helloworld.Greeter/SayHello",
    75  			Func: GreeterService_SayHello_Handler,
    76  			Bindings: []*restful.Binding{{
    77  				Name:  "/trpc.examples.restful.helloworld.Greeter/SayHello",
    78  				Input: func() restful.ProtoMessage { return new(HelloRequest) },
    79  				Filter: func(svc interface{}, ctx context.Context, reqBody interface{}) (interface{}, error) {
    80  					return svc.(GreeterService).SayHello(ctx, reqBody.(*HelloRequest))
    81  				},
    82  				HTTPMethod:   "POST",
    83  				Pattern:      restful.Enforce("/v1/foobar"),
    84  				Body:         requestBodyGreeterServiceSayHelloRESTfulPath0{},
    85  				ResponseBody: nil,
    86  			}, {
    87  				Name:  "/trpc.examples.restful.helloworld.Greeter/SayHello",
    88  				Input: func() restful.ProtoMessage { return new(HelloRequest) },
    89  				Filter: func(svc interface{}, ctx context.Context, reqBody interface{}) (interface{}, error) {
    90  					return svc.(GreeterService).SayHello(ctx, reqBody.(*HelloRequest))
    91  				},
    92  				HTTPMethod:   "POST",
    93  				Pattern:      restful.Enforce("/v1/foo/{name}"),
    94  				Body:         nil,
    95  				ResponseBody: nil,
    96  			}, {
    97  				Name:  "/trpc.examples.restful.helloworld.Greeter/SayHello",
    98  				Input: func() restful.ProtoMessage { return new(HelloRequest) },
    99  				Filter: func(svc interface{}, ctx context.Context, reqBody interface{}) (interface{}, error) {
   100  					return svc.(GreeterService).SayHello(ctx, reqBody.(*HelloRequest))
   101  				},
   102  				HTTPMethod:   "GET",
   103  				Pattern:      restful.Enforce("/v2/bar"),
   104  				Body:         nil,
   105  				ResponseBody: nil,
   106  			}},
   107  		},
   108  	},
   109  }
   110  
   111  // RegisterGreeterService register service
   112  func RegisterGreeterService(s server.Service, svr GreeterService) {
   113  	if err := s.Register(&GreeterServer_ServiceDesc, svr); err != nil {
   114  		panic(fmt.Sprintf("Greeter register error:%v", err))
   115  	}
   116  }
   117  
   118  /* ************************************ Client Definition ************************************ */
   119  
   120  // GreeterClientProxy defines service client proxy
   121  type GreeterClientProxy interface {
   122  	SayHello(ctx context.Context, req *HelloRequest, opts ...client.Option) (rsp *HelloReply, err error)
   123  }
   124  
   125  type GreeterClientProxyImpl struct {
   126  	client client.Client
   127  	opts   []client.Option
   128  }
   129  
   130  var NewGreeterClientProxy = func(opts ...client.Option) GreeterClientProxy {
   131  	return &GreeterClientProxyImpl{client: client.DefaultClient, opts: opts}
   132  }
   133  
   134  func (c *GreeterClientProxyImpl) SayHello(ctx context.Context, req *HelloRequest, opts ...client.Option) (*HelloReply, error) {
   135  	ctx, msg := codec.WithCloneMessage(ctx)
   136  	defer codec.PutBackMessage(msg)
   137  	msg.WithClientRPCName("/trpc.examples.restful.helloworld.Greeter/SayHello")
   138  	msg.WithCalleeServiceName(GreeterServer_ServiceDesc.ServiceName)
   139  	msg.WithCalleeApp("")
   140  	msg.WithCalleeServer("")
   141  	msg.WithCalleeService("Greeter")
   142  	msg.WithCalleeMethod("SayHello")
   143  	msg.WithSerializationType(codec.SerializationTypePB)
   144  	callopts := make([]client.Option, 0, len(c.opts)+len(opts))
   145  	callopts = append(callopts, c.opts...)
   146  	callopts = append(callopts, opts...)
   147  	rsp := &HelloReply{}
   148  	if err := c.client.Invoke(ctx, req, rsp, callopts...); err != nil {
   149  		return nil, err
   150  	}
   151  	return rsp, nil
   152  }