github.com/cloudwego/kitex@v0.9.0/tool/internal_pkg/pluginmode/thriftgo/register_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 registerHessian = `
    18  {{- define "register"}}
    19  package {{ .PkgName}}
    20  
    21  import (
    22      "fmt"
    23  
    24  	"github.com/pkg/errors"
    25  	"github.com/kitex-contrib/codec-dubbo/pkg/hessian2"
    26  	codec "github.com/kitex-contrib/codec-dubbo/pkg/iface"
    27  )
    28  
    29  var objects{{ .IDLName}} = []interface{}{ 
    30  {{- range .Scope.StructLikes}}
    31      &{{ .GoName}}{},
    32  {{- end}} 
    33  }
    34  
    35  func init() {
    36  	hessian2.Register(objects{{ .IDLName}} )
    37  }
    38  
    39  {{range .Scope.Services }}
    40  func Get{{.GoName}}IDLAnnotations() map[string][]string {
    41  	return map[string][]string {
    42          {{- range .Annotations}}
    43  		"{{.Key}}": { {{range .Values}}"{{.}}", {{- end}}},
    44          {{- end}}
    45  	}
    46  }
    47  {{- end}}
    48  
    49  {{- range .Scope.StructLikes}}
    50  {{template "StructLikeProtocol" .}}
    51  {{- end}}
    52  
    53  {{- range .Scope.Services}}
    54  {{- range .Functions}}
    55  
    56  {{$argType := .ArgType}}
    57  {{$resType := .ResType}}
    58  
    59  func (p *{{$argType.GoName}}) Encode(e codec.Encoder) error {
    60  {{- if gt (len $argType.Fields) 0}}
    61  	var err error
    62  {{- end}}
    63  {{- range $argType.Fields}}
    64  {{- $FieldName := .GoName}}
    65  	err = e.Encode(p.{{$FieldName}})
    66      if err != nil {
    67      	return err
    68  	}
    69  {{end}}{{/* range .Fields */}}
    70  	return nil
    71  }
    72  
    73  func (p *{{$argType.GoName}}) Decode(d codec.Decoder) error {
    74  {{- if gt (len $argType.Fields) 0}}
    75      var (
    76  		err error
    77  		v interface{}
    78  	)
    79  {{- end}}
    80  {{- range $argType.Fields}}
    81  {{- $Type := .Type }}
    82  {{- $FieldName := .GoName}}
    83  {{- $FieldTypeName := .GoTypeName}}
    84  	v, err = d.Decode()
    85      if err != nil {
    86      	return err
    87  	}
    88      err = hessian2.ReflectResponse(v, &p.{{$FieldName}})
    89  	if err != nil {
    90  		return errors.Wrap(err, fmt.Sprintf("invalid data type: %T", v))
    91  	}
    92  {{end}}{{/* range .Fields */}}
    93  	return nil
    94  } {{/* encode decode */}}
    95  
    96  func (p *{{$resType.GoName}}) Encode(e codec.Encoder) error {
    97  {{- if gt (len $resType.Fields) 0}}
    98      var err error
    99  {{- end}}
   100  {{- range $resType.Fields}}
   101  {{- $FieldName := .GoName}}
   102  	err = e.Encode(p.{{$FieldName}})
   103      if err != nil {
   104      	return err
   105  	}
   106  {{end}}{{/* range .Fields */}}
   107  	return nil
   108  }
   109  
   110  func (p *{{$resType.GoName}}) Decode(d codec.Decoder) error {
   111  {{- if gt (len $resType.Fields) 0}}
   112      var (
   113  		err error
   114  		v interface{}
   115  	)
   116  {{- end}}
   117  {{- range $resType.Fields}}
   118  {{- $Type := .Type }}
   119  {{- $FieldName := .GoName}}
   120  {{- $FieldTypeName := .GoTypeName}}
   121  	v, err = d.Decode()
   122      if err != nil {
   123      	return err
   124  	}
   125      err = hessian2.ReflectResponse(v, &p.{{$FieldName}})
   126  	if err != nil {
   127  		return errors.Wrap(err, fmt.Sprintf("invalid data type: %T", v))
   128  	}
   129  {{end}}{{/* range .Fields */}}
   130  	return nil
   131  } {{/* encode decode */}}
   132  
   133  {{- end}}{{/* range Functions */}}
   134  {{- end}}{{/* range .Scope.Service */}}
   135  
   136  {{- end}}{{/* define RegisterHessian*/}}
   137  `
   138  
   139  const structLikeProtocol = `
   140  {{define "StructLikeProtocol"}}
   141  {{- $TypeName := .GoName}}
   142  func (p *{{$TypeName}}) Encode(e codec.Encoder) error {
   143  {{- if gt (len .Fields) 0}}
   144      var err error
   145  {{- end}}
   146  {{- range .Fields}}
   147  {{- $FieldName := .GoName}}
   148  	err = e.Encode(p.{{$FieldName}})
   149      if err != nil {
   150      	return err
   151  	}
   152  {{end}}{{/* range .Fields */}}
   153  	return nil
   154  }
   155  
   156  func (p *{{$TypeName}}) Decode(d codec.Decoder) error {
   157  {{- if gt (len .Fields) 0}}
   158  	var (
   159  		err error
   160  		v interface{}
   161  	)
   162  {{- end}}
   163  {{- range .Fields}}
   164  {{- $Type := .Type }}
   165  {{- $FieldName := .GoName}}
   166  {{- $FieldTypeName := .GoTypeName}}
   167  	v, err = d.Decode()
   168      if err != nil {
   169      	return err
   170  	}
   171      err = hessian2.ReflectResponse(v, &p.{{$FieldName}})
   172  	if err != nil {
   173  		return errors.Wrap(err, fmt.Sprintf("invalid data type: %T", v))
   174  	}
   175  {{end}}{{/* range .Fields */}}
   176  	return nil
   177  } {{/* encode decode */}}
   178  
   179  {{template "JavaClassName" .}}
   180  {{- end}}{{/* define "StructLikeProtocol" */}}
   181  `
   182  
   183  const javaClassName = `
   184  {{define "JavaClassName"}}
   185  {{- $TypeName := .GoName}}
   186  {{- $anno := .Annotations }}
   187  {{- $value := $anno.ILocValueByKey "JavaClassName" 0}}
   188  {{- if ne "" $value}}
   189  func (p *{{$TypeName}}) JavaClassName() string {
   190  	return "{{$value}}"
   191  }
   192  {{- end}}{{/* end if */}}
   193  {{- end}}{{/* end JavaClassName */}}
   194  `