github.com/cloudwego/kitex@v0.9.0/tool/internal_pkg/tpl/client.go (about)

     1  // Copyright 2022 CloudWeGo Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tpl
    16  
    17  // ClientTpl is the template for generating client.go.
    18  var ClientTpl string = `// Code generated by Kitex {{.Version}}. DO NOT EDIT.
    19  
    20  package {{ToLower .ServiceName}}
    21  import (
    22  	{{- range $path, $aliases := .Imports}}
    23  		{{- if not $aliases}}
    24  			"{{$path}}"
    25  		{{- else}}
    26  			{{- range $alias, $is := $aliases}}
    27  				{{$alias}} "{{$path}}"
    28  			{{- end}}
    29  		{{- end}}
    30  	{{- end}}
    31  	{{- if .HasStreaming}}
    32  	"github.com/cloudwego/kitex/client/streamclient"
    33  	"github.com/cloudwego/kitex/client/callopt/streamcall"
    34      {{- end}}
    35  )
    36  // Client is designed to provide IDL-compatible methods with call-option parameter for kitex framework.
    37  type Client interface {
    38  {{- range .AllMethods}}
    39  {{- if and (eq $.Codec "protobuf") (or .ClientStreaming .ServerStreaming)}}{{/* protobuf: generate streaming calls in Client, to keep compatibility */}}
    40  	{{.Name}}(ctx context.Context {{if not .ClientStreaming}}{{range .Args}}, {{.RawName}} {{.Type}}{{end}}{{end}}, callOptions ...callopt.Option ) (stream {{.ServiceName}}_{{.RawName}}Client, err error)
    41  {{- else}}
    42  	{{- if or (eq $.Codec "protobuf") (eq .Streaming.Mode "")}}
    43  	{{.Name}}(ctx context.Context {{range .Args}}, {{.RawName}} {{.Type}}{{end}}, callOptions ...callopt.Option ) ({{if not .Void}}r {{.Resp.Type}}, {{end}}err error)
    44  	{{- end}}
    45  {{- end}}
    46  {{- end}}
    47  }
    48  
    49  {{- if .HasStreaming}}
    50  // StreamClient is designed to provide Interface for Streaming APIs.
    51  type StreamClient interface {
    52  {{- range .AllMethods}}
    53  {{- if or .ClientStreaming .ServerStreaming}}
    54  	{{.Name}}(ctx context.Context {{if not .ClientStreaming}}{{range .Args}}, {{.RawName}} {{.Type}}{{end}}{{end}}, callOptions ...streamcall.Option ) (stream {{.ServiceName}}_{{.RawName}}Client, err error)
    55  {{- else if .Streaming.Unary}}
    56  	{{.Name}}(ctx context.Context {{range .Args}}, {{.RawName}} {{.Type}}{{end}}, callOptions ...streamcall.Option ) ({{if not .Void}}r {{.Resp.Type}}, {{end}}err error)
    57  {{- end}}
    58  {{- end}}
    59  }
    60  {{- end}}
    61  
    62  {{range .AllMethods}}
    63  {{- if or .ClientStreaming .ServerStreaming}}
    64  type {{.ServiceName}}_{{.RawName}}Client interface {
    65  	streaming.Stream
    66  	{{- if .ClientStreaming}}
    67  	Send({{range .Args}}{{.Type}}{{end}}) error
    68  	{{- end}}
    69  	{{- if .ServerStreaming}}
    70  	Recv() ({{.Resp.Type}}, error)
    71  	{{- end}}
    72  	{{- if and .ClientStreaming (not .ServerStreaming)}}
    73  	CloseAndRecv() ({{.Resp.Type}}, error)
    74  	{{- end}}
    75  }
    76  {{- end}}
    77  {{end}}
    78  
    79  // NewClient creates a client for the service defined in IDL.
    80  func NewClient(destService string, opts ...client.Option) (Client, error) {
    81  	var options []client.Option
    82  	options = append(options, client.WithDestService(destService))
    83  
    84      {{template "@client.go-NewClient-option" .}}
    85  	{{if and (eq $.Codec "protobuf") .HasStreaming}}{{/* Thrift Streaming only in StreamClient */}}
    86  	options = append(options, client.WithTransportProtocol(transport.GRPC))
    87  	{{end}}
    88  	options = append(options, opts...)
    89  
    90  	kc, err := client.NewClient(
    91  		{{- if eq $.Codec "protobuf"}}serviceInfo(){{else}}serviceInfoForClient(){{end -}}
    92  		, options...)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	{{- if .FrugalPretouch}}
    97  	pretouch()
    98  	{{- end}}{{/* if .FrugalPretouch */}}
    99  	return &k{{.ServiceName}}Client{
   100  		kClient: newServiceClient(kc),
   101  	}, nil
   102  }
   103  
   104  // MustNewClient creates a client for the service defined in IDL. It panics if any error occurs.
   105  func MustNewClient(destService string, opts ...client.Option) Client {
   106  	kc, err := NewClient(destService, opts...)
   107  	if err != nil {
   108  		panic(err)
   109  	}
   110  	return kc
   111  }
   112  
   113  type k{{.ServiceName}}Client struct {
   114  	*kClient
   115  }
   116  
   117  {{range .AllMethods}}
   118  {{- /* Thrift Client only support non-streaming methods */}}
   119  {{- if or .ClientStreaming .ServerStreaming}}
   120  {{- if eq $.Codec "protobuf"}}
   121  func (p *k{{$.ServiceName}}Client) {{.Name}}(ctx context.Context {{if not .ClientStreaming}}{{range .Args}}, {{.RawName}} {{.Type}}{{end}}{{end}}, callOptions ...callopt.Option ) (stream {{.ServiceName}}_{{.RawName}}Client, err error) {
   122  	ctx = client.NewCtxWithCallOptions(ctx, callOptions)
   123  	return p.kClient.{{.Name}}(ctx{{if not .ClientStreaming}}{{range .Args}}, {{.RawName}}{{end}}{{end}})
   124  }
   125  {{- end}}
   126  {{- else}}
   127  {{- if or (eq $.Codec "protobuf") (eq .Streaming.Mode "")}}
   128  func (p *k{{$.ServiceName}}Client) {{.Name}}(ctx context.Context {{range .Args}}, {{.RawName}} {{.Type}}{{end}}, callOptions ...callopt.Option ) ({{if not .Void}}r {{.Resp.Type}}, {{end}}err error) {
   129  	ctx = client.NewCtxWithCallOptions(ctx, callOptions)
   130  	return p.kClient.{{.Name}}(ctx{{range .Args}}, {{.RawName}}{{end}})
   131  }
   132  {{- end}}
   133  {{- end}}
   134  {{end}}
   135  
   136  {{- if .HasStreaming}}
   137  // NewStreamClient creates a stream client for the service's streaming APIs defined in IDL.
   138  func NewStreamClient(destService string, opts ...streamclient.Option) (StreamClient, error) {
   139  	var options []client.Option
   140  	options = append(options, client.WithDestService(destService))
   141      {{- template "@client.go-NewStreamClient-option" .}}
   142  	options = append(options, client.WithTransportProtocol(transport.GRPC))
   143      options = append(options, streamclient.GetClientOptions(opts)...)
   144  
   145  	kc, err := client.NewClient(serviceInfoForStreamClient(), options...)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	{{- if .FrugalPretouch}}
   150  	pretouch()
   151  	{{- end}}{{/* if .FrugalPretouch */}}
   152  	return &k{{.ServiceName}}StreamClient{
   153  		kClient: newServiceClient(kc),
   154  	}, nil
   155  }
   156  
   157  // MustNewStreamClient creates a stream client for the service's streaming APIs defined in IDL.
   158  // It panics if any error occurs.
   159  func MustNewStreamClient(destService string, opts ...streamclient.Option) StreamClient {
   160  	kc, err := NewStreamClient(destService, opts...)
   161  	if err != nil {
   162  		panic(err)
   163  	}
   164  	return kc
   165  }
   166  
   167  type k{{.ServiceName}}StreamClient struct {
   168  	*kClient
   169  }
   170  {{- range .AllMethods}}
   171  {{if or .ClientStreaming .ServerStreaming}}
   172  func (p *k{{$.ServiceName}}StreamClient) {{.Name}}(ctx context.Context {{if not .ClientStreaming}}{{range .Args}}, {{.RawName}} {{.Type}}{{end}}{{end}}, callOptions ...streamcall.Option ) (stream {{.ServiceName}}_{{.RawName}}Client, err error) {
   173  	ctx = client.NewCtxWithCallOptions(ctx, streamcall.GetCallOptions(callOptions))
   174  	return p.kClient.{{.Name}}(ctx{{if not .ClientStreaming}}{{range .Args}}, {{.RawName}}{{end}}{{end}})
   175  }
   176  {{else if .Streaming.Unary}}
   177  func (p *k{{$.ServiceName}}StreamClient) {{.Name}}(ctx context.Context {{range .Args}}, {{.RawName}} {{.Type}}{{end}}, callOptions ...streamcall.Option ) ({{if not .Void}}r {{.Resp.Type}}, {{end}}err error) {
   178  	ctx = client.NewCtxWithCallOptions(ctx, streamcall.GetCallOptions(callOptions))
   179  	return p.kClient.{{.Name}}(ctx{{range .Args}}, {{.RawName}}{{end}})
   180  }
   181  {{- end}}
   182  {{end}}
   183  {{- end}}
   184  {{template "@client.go-EOF" .}}
   185  `