Skip to content

Commit

Permalink
opt: move mask check into FieldReader
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Dec 1, 2023
1 parent 9f7a28b commit 474dbef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
20 changes: 10 additions & 10 deletions generator/golang/templates/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,9 @@ func (p *{{$TypeName}}) Read(iprot thrift.TProtocol) (err error) {
{{- $isBaseVal := .Type | IsBaseType}}
case {{.ID}}:
if fieldTypeId == thrift.{{.Type | GetTypeIDConstant }} {
{{- if Features.WithFieldMask}}
if {{if $isBaseVal}}_{{else}}nfm{{end}}, ex := p._fieldmask.Field(fieldId); ex {
{{- end}}
if err = p.{{.Reader}}(iprot{{if and Features.WithFieldMask (not $isBaseVal)}}, nfm{{end}}); err != nil {
if err = p.{{.Reader}}(iprot); err != nil {
goto ReadFieldError
}
{{- if Features.WithFieldMask}}
} else if err = iprot.Skip(fieldTypeId); err != nil {
goto SkipFieldError
}
{{- end}}
{{- if .Requiredness.IsRequired}}
isset{{.GoName}} = true
{{- end}}
Expand Down Expand Up @@ -279,9 +271,17 @@ var StructLikeReadField = `
{{- range .Fields}}
{{$FieldName := .GoName}}
{{- $isBaseVal := .Type | IsBaseType -}}
func (p *{{$TypeName}}) {{.Reader}}(iprot thrift.TProtocol{{if and Features.WithFieldMask (not $isBaseVal)}}, fm *fieldmask.FieldMask{{end}}) error {
func (p *{{$TypeName}}) {{.Reader}}(iprot thrift.TProtocol) error {
{{- if Features.WithFieldMask}}
if {{if $isBaseVal}}_{{else}}fm{{end}}, ex := p._fieldmask.Field({{.ID}}); ex {
{{- end}}
{{$ctx := (MkRWCtx .).WithFieldMask "fm"}}
{{- template "FieldRead" $ctx}}
{{- if Features.WithFieldMask}}
} else if err := iprot.Skip(thrift.{{.Type | GetTypeIDConstant}}); err != nil {
return err
}
{{- end}}
return nil
}
{{- end}}{{/* range .Fields */}}
Expand Down
2 changes: 0 additions & 2 deletions test/golang/fieldmask/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/apache/thrift/lib/go/thrift"
"github.com/cloudwego/thriftgo/fieldmask"
"github.com/davecgh/go-spew/spew"

// abase "github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-go/base"
nbase "github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-new/base"
Expand Down Expand Up @@ -133,7 +132,6 @@ func TestMaskRequired(t *testing.T) {
if err != nil {
t.Fatal(err)
}
spew.Dump(fm)
j, err := fm.MarshalJSON()
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 474dbef

Please sign in to comment.