github.com/cloudwego/kitex@v0.9.0/tool/internal_pkg/pluginmode/thriftgo/file_tpl.go (about)

     1  // Copyright 2021 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 thriftgo
    16  
    17  const file = `
    18  {{define "file"}}
    19  // Code generated by Kitex {{Version}}. DO NOT EDIT.
    20  
    21  package {{.PkgName}}
    22  
    23  import (
    24  	"fmt"
    25  	"bytes"
    26  	"strings"
    27  	"reflect"
    28  
    29  	"github.com/apache/thrift/lib/go/thrift"
    30  
    31  	{{if GenerateDeepCopyAPIs -}}
    32  	kutils "github.com/cloudwego/kitex/pkg/utils"
    33  	{{- end}}
    34  	{{if GenerateFastAPIs}}
    35  	"{{ImportPathTo "pkg/protocol/bthrift"}}"
    36  	{{- end}}
    37  	{{if GenerateArgsResultTypes}}
    38  		{{if Features.KeepUnknownFields}}
    39  			{{- if ne (len .Scope.Services) 0}}
    40  				unknown "github.com/cloudwego/thriftgo/generator/golang/extension/unknown"
    41  			{{- end}}
    42  		{{- end}}
    43  	{{- end}}
    44  	{{- range $path, $alias := .Imports}}
    45  	{{$alias }}"{{$path}}"
    46  	{{- end}}
    47  )
    48  
    49  {{InsertionPoint "KitexUnusedProtection"}}
    50  
    51  // unused protection
    52  var (
    53  	_ = fmt.Formatter(nil)
    54  	_ = (*bytes.Buffer)(nil)
    55  	_ = (*strings.Builder)(nil)
    56  	_ = reflect.Type(nil)
    57  	_ = thrift.TProtocol(nil)
    58  	{{- if GenerateFastAPIs}}
    59  	_ = bthrift.BinaryWriter(nil)
    60  	{{- end}}
    61  	{{- range .Imports | ToPackageNames}}
    62  	_ = {{.}}.KitexUnusedProtection
    63  	{{- end}}
    64  )
    65  
    66  {{template "body" .}}
    67  
    68  {{end}}{{/* define "file" */}}
    69  `
    70  
    71  const body = `
    72  {{define "body"}}
    73  
    74  {{- range .Scope.StructLikes}}
    75  {{template "StructLikeCodec" .}}
    76  {{- end}}
    77  
    78  {{- range .Scope.Services}}
    79  {{template "Processor" .}}
    80  {{- end}}
    81  
    82  {{template "ArgsAndResult" .}}
    83  
    84  {{template "ExtraTemplates" .}}
    85  {{- end}}{{/* define "body" */}}
    86  `
    87  
    88  const patchArgsAndResult = `
    89  {{define "ArgsAndResult"}}
    90  {{range $svc := .Scope.Services}}
    91  {{range .Functions}}
    92  
    93  {{$argType := .ArgType}}
    94  {{$resType := .ResType}}
    95  
    96  
    97  {{- if GenerateArgsResultTypes}}
    98  {{template "StructLike" $argType}}
    99  {{- end}}{{/* if GenerateArgsResultTypes */}}
   100  func (p *{{$argType.GoName}}) GetFirstArgument() interface{} {
   101  	return {{if .Arguments}}p.{{(index $argType.Fields 0).GoName}}{{else}}nil{{end}}
   102  }
   103  
   104  {{if not .Oneway}}
   105  {{- if GenerateArgsResultTypes}}
   106  {{template "StructLike" $resType}}
   107  {{- end}}{{/* if GenerateArgsResultTypes */}}
   108  func (p *{{$resType.GoName}}) GetResult() interface{} {
   109  	return {{if .Void}}nil{{else}}p.Success{{end}}
   110  }
   111  
   112  {{- end}}{{/* if not .Oneway */}}
   113  {{- end}}{{/* range Functions */}}
   114  {{- end}}{{/* range .Scope.Service */}}
   115  {{- end}}{{/* define "FirstResult" */}}
   116  `
   117  
   118  var basicTemplates = []string{
   119  	patchArgsAndResult,
   120  	file,
   121  	body,
   122  	registerHessian,
   123  }