Skip to content

Commit

Permalink
feat: apache bridge (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Dec 13, 2024
1 parent 2cc413b commit d74a745
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions generator/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
18 changes: 16 additions & 2 deletions generator/golang/templates/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" */}}
`
Expand All @@ -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 {
Expand All @@ -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" */}}
`
Expand All @@ -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}}
Expand Down Expand Up @@ -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" */}}
`
Expand All @@ -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}}() {
Expand Down Expand Up @@ -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" */}}
`
Expand Down
1 change: 1 addition & 0 deletions generator/golang/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`\\.`)
Expand Down

0 comments on commit d74a745

Please sign in to comment.