diff --git a/generator/golang/imports.go b/generator/golang/imports.go index b4f8ed5..9c7932e 100644 --- a/generator/golang/imports.go +++ b/generator/golang/imports.go @@ -92,6 +92,7 @@ func (im *importManager) init(cu *CodeUtils, ast *parser.Thrift) { "streaming": KitexStreamingLib, "thrift_option": ThriftOptionLib, "apache_warning": ApacheWarningLib, + "apache_adaptor": ApacheAdaptor, } for pkg, path := range std { ns.Add(pkg, path) diff --git a/generator/golang/option.go b/generator/golang/option.go index 4c52ab0..5d5021a 100644 --- a/generator/golang/option.go +++ b/generator/golang/option.go @@ -71,6 +71,7 @@ type Features struct { NoProcessor bool `no_processor:" Do not generate default thrift processor and client. Later this feature will be a default feature."` GetEnumAnnotation bool `get_enum_annotation:"Generate GetAnnotation method for enum types."` ApacheWarning bool `apache_warning:"Generate Apache Codec with warning on the first line."` + ApacheAdaptor bool `apache_adaptor:"Generate adaptor for apache codec to kitex fast codec."` } var defaultFeatures = Features{ diff --git a/generator/golang/templates/struct.go b/generator/golang/templates/struct.go index 3ef99a4..ccbf7f8 100644 --- a/generator/golang/templates/struct.go +++ b/generator/golang/templates/struct.go @@ -168,10 +168,14 @@ var StructLikeRead = ` {{- UseStdLibrary "thrift" "fmt"}} {{- $TypeName := .GoName}} func (p *{{$TypeName}}) Read(iprot thrift.TProtocol) (err error) { - {{if Features.ApacheWarning}} + {{- if Features.ApacheWarning -}} {{- UseStdLibrary "apache_warning"}} apache_warning.WarningApache("{{$TypeName}}") {{end}} + {{- if Features.ApacheAdaptor -}} + {{- UseStdLibrary "apache_adaptor" -}} + return apache_adaptor.AdaptRead(p, iprot) + {{- else -}} {{if Features.KeepUnknownFields}}var name string{{end}} var fieldTypeId thrift.TType var fieldId int16 @@ -265,6 +269,7 @@ ReadStructEndError: RequiredFieldNotSetError: return thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, fmt.Errorf("required field %s is not set", fieldIDToName_{{$TypeName}}[fieldId])) {{- end}}{{/* if $RequiredFieldNotSetError */}} +{{- end}}{{/* if Features.ApacheAdaptor */}} } {{- end}}{{/* define "StructLikeRead" */}} ` @@ -291,6 +296,7 @@ var StructLikeReadField = ` {{- range .Fields}} {{$FieldName := .GoName}} {{- $isBaseVal := .Type | IsBaseType -}} +{{- if not Features.ApacheAdaptor -}} func (p *{{$TypeName}}) {{.Reader}}(iprot thrift.TProtocol) error { {{- if Features.WithFieldMask}} if {{if $isBaseVal}}_{{else}}fm{{end}}, ex := p._fieldmask.Field({{.ID}}); ex { @@ -308,6 +314,7 @@ func (p *{{$TypeName}}) {{.Reader}}(iprot thrift.TProtocol) error { {{- end}} return nil } +{{- end}}{{/* if Features.ApacheAdaptor */}} {{- end}}{{/* range .Fields */}} {{- end}}{{/* define "StructLikeReadField" */}} ` @@ -318,10 +325,14 @@ var StructLikeWrite = ` {{- UseStdLibrary "thrift" "fmt"}} {{- $TypeName := .GoName}} func (p *{{$TypeName}}) Write(oprot thrift.TProtocol) (err error) { - {{if Features.ApacheWarning}} + {{- if Features.ApacheWarning -}} {{- UseStdLibrary "apache_warning"}} apache_warning.WarningApache("{{$TypeName}}") {{end}} + {{- if Features.ApacheAdaptor -}} + {{- UseStdLibrary "apache_adaptor" -}} + return apache_adaptor.AdaptWrite(p, oprot) + {{- else -}} {{- if gt (len .Fields) 0 }} var fieldId int16 {{- end}} @@ -373,6 +384,7 @@ WriteStructEndError: UnknownFieldsWriteError: return thrift.PrependError(fmt.Sprintf("%T write unknown fields error: ", p), err) {{- end}}{{/* if Features.KeepUnknownFields */}} +{{- end}}{{/* if Features.ApacheAdaptor */}} } {{- end}}{{/* define "StructLikeWrite" */}} ` @@ -387,6 +399,7 @@ var StructLikeWriteField = ` {{- $IsSetName := .IsSetter}} {{- $TypeID := .Type | GetTypeIDConstant }} {{- $isBaseVal := .Type | IsBaseType }} +{{- if not Features.ApacheAdaptor -}} func (p *{{$TypeName}}) {{.Writer}}(oprot thrift.TProtocol) (err error) { {{- if .Requiredness.IsOptional}} if p.{{$IsSetName}}() { @@ -432,6 +445,7 @@ WriteFieldBeginError: WriteFieldEndError: return thrift.PrependError(fmt.Sprintf("%T write field {{.ID}} end error: ", p), err) } +{{- end}}{{/* if Features.ApacheAdaptor */}} {{end}}{{/* range .Fields */}} {{- end}}{{/* define "StructLikeWriteField" */}} ` diff --git a/generator/golang/util.go b/generator/golang/util.go index 76f9c0d..7677b7a 100644 --- a/generator/golang/util.go +++ b/generator/golang/util.go @@ -49,6 +49,7 @@ const ( ThriftJSONUtilLib = "github.com/cloudwego/thriftgo/utils/json_utils" KitexStreamingLib = "github.com/cloudwego/kitex/pkg/streaming" ApacheWarningLib = "github.com/cloudwego/thriftgo/utils" + ApacheAdaptor = "github.com/cloudwego/gopkg/protocol/thrift/apache/adaptor" ) var escape = regexp.MustCompile(`\\.`)