From d74d31485ef593a21e963f1289037a9e0f18bada Mon Sep 17 00:00:00 2001 From: fgy Date: Tue, 21 Nov 2023 21:23:39 +0800 Subject: [PATCH] optimize: rm tag --- cmd/hz/app/app.go | 2 +- cmd/hz/generator/model/model.go | 5 +++-- cmd/hz/protobuf/plugin.go | 12 +----------- cmd/hz/protobuf/tag_test.go | 2 +- cmd/hz/protobuf/tags.go | 23 ++++++++++++++++++----- cmd/hz/thrift/plugin.go | 6 +++++- cmd/hz/thrift/tags.go | 12 +++++++++--- 7 files changed, 38 insertions(+), 24 deletions(-) diff --git a/cmd/hz/app/app.go b/cmd/hz/app/app.go index a6c44c011..95c9cb4e2 100644 --- a/cmd/hz/app/app.go +++ b/cmd/hz/app/app.go @@ -184,7 +184,7 @@ func Init() *cli.App { unsetOmitemptyFlag := cli.BoolFlag{Name: "unset_omitempty", Usage: "Remove 'omitempty' tag for generated struct.", Destination: &globalArgs.UnsetOmitempty} protoCamelJSONTag := cli.BoolFlag{Name: "pb_camel_json_tag", Usage: "Convert Name style for json tag to camel(Only works protobuf).", Destination: &globalArgs.ProtobufCamelJSONTag} snakeNameFlag := cli.BoolFlag{Name: "snake_tag", Usage: "Use snake_case style naming for tags. (Only works for 'form', 'query', 'json')", Destination: &globalArgs.SnakeName} - rmTagFlag := cli.StringSliceFlag{Name: "rm_tag", Usage: "Remove the specified tag"} + rmTagFlag := cli.StringSliceFlag{Name: "rm_tag", Usage: "Remove the specified tag, it's priority is lower than the annotation."} customLayout := cli.StringFlag{Name: "customize_layout", Usage: "Specify the path for layout template.", Destination: &globalArgs.CustomizeLayout} customLayoutData := cli.StringFlag{Name: "customize_layout_data_path", Usage: "Specify the path for layout template render data.", Destination: &globalArgs.CustomizeLayoutData} customPackage := cli.StringFlag{Name: "customize_package", Usage: "Specify the path for package template.", Destination: &globalArgs.CustomizePackage} diff --git a/cmd/hz/generator/model/model.go b/cmd/hz/generator/model/model.go index 2bb786baf..f4028048c 100644 --- a/cmd/hz/generator/model/model.go +++ b/cmd/hz/generator/model/model.go @@ -377,8 +377,9 @@ type Choice struct { type Tags []Tag type Tag struct { - Key string - Value string + Key string + Value string + IsDefault bool // default tag } func (ts Tags) String() string { diff --git a/cmd/hz/protobuf/plugin.go b/cmd/hz/protobuf/plugin.go index e25803860..260334012 100644 --- a/cmd/hz/protobuf/plugin.go +++ b/cmd/hz/protobuf/plugin.go @@ -509,7 +509,7 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie }...) } - err := injectTagsToStructTags(field.Desc, &tags, true) + err := injectTagsToStructTags(field.Desc, &tags, true, rmTags) if err != nil { return err } @@ -518,16 +518,6 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie tags = append(tags, gotrackTags...) } - if len(rmTags) > 0 { - tmp := structTags{} - for _, tag := range tags { - if !rmTags.Exist(tag[0]) { - tmp = append(tmp, tag) - } - } - tags = tmp - } - name := field.GoName if field.Desc.IsWeak() { name = WeakFieldPrefix_goname + name diff --git a/cmd/hz/protobuf/tag_test.go b/cmd/hz/protobuf/tag_test.go index 7e6528b16..2e7a9ca3e 100644 --- a/cmd/hz/protobuf/tag_test.go +++ b/cmd/hz/protobuf/tag_test.go @@ -126,7 +126,7 @@ func TestTagGenerate(t *testing.T) { tags := structTags{ {"protobuf", fieldProtobufTagValue(field)}, } - err = injectTagsToStructTags(field.Desc, &tags, true) + err = injectTagsToStructTags(field.Desc, &tags, true, nil) if err != nil { t.Fatal(err) } diff --git a/cmd/hz/protobuf/tags.go b/cmd/hz/protobuf/tags.go index 1abf23111..26c6cc7cf 100644 --- a/cmd/hz/protobuf/tags.go +++ b/cmd/hz/protobuf/tags.go @@ -340,14 +340,18 @@ func defaultBindingStructTags(f protoreflect.FieldDescriptor) []model.Tag { val := getStructJsonValue(f, v.(string)) out[0] = tag("json", val) } else { - out[0] = reflectJsonTag(f) + t := reflectJsonTag(f) + t.IsDefault = true + out[0] = t } if v := checkFirstOption(api.E_Query, opts); v != nil { val := checkStructRequire(f, v.(string)) out[1] = tag(BindingTags[api.E_Query], val) } else { val := checkStructRequire(f, checkSnakeName(string(f.Name()))) - out[1] = tag(BindingTags[api.E_Query], val) + t := tag(BindingTags[api.E_Query], val) + t.IsDefault = true + out[1] = t } if v := checkFirstOption(api.E_Form, opts); v != nil { val := checkStructRequire(f, v.(string)) @@ -355,16 +359,20 @@ func defaultBindingStructTags(f protoreflect.FieldDescriptor) []model.Tag { } else { if v := checkFirstOption(api.E_FormCompatible, opts); v != nil { // compatible form_compatible val := checkStructRequire(f, v.(string)) - out[2] = tag(BindingTags[api.E_Form], val) + t := tag(BindingTags[api.E_Form], val) + t.IsDefault = true + out[2] = t } else { val := checkStructRequire(f, checkSnakeName(string(f.Name()))) - out[2] = tag(BindingTags[api.E_Form], val) + t := tag(BindingTags[api.E_Form], val) + t.IsDefault = true + out[2] = t } } return out } -func injectTagsToStructTags(f protoreflect.FieldDescriptor, out *structTags, needDefault bool) error { +func injectTagsToStructTags(f protoreflect.FieldDescriptor, out *structTags, needDefault bool, rmTags RemoveTags) error { as := f.Options() // binding tags tags := model.Tags(make([]model.Tag, 0, 6)) @@ -424,6 +432,11 @@ func injectTagsToStructTags(f protoreflect.FieldDescriptor, out *structTags, nee disableTag = true } } + for _, t := range tags { + if t.IsDefault && rmTags.Exist(t.Key) { + tags.Remove(t.Key) + } + } // protobuf tag as first sort.Sort(tags[1:]) for _, t := range tags { diff --git a/cmd/hz/thrift/plugin.go b/cmd/hz/thrift/plugin.go index 8b840e739..c315c9dcb 100644 --- a/cmd/hz/thrift/plugin.go +++ b/cmd/hz/thrift/plugin.go @@ -418,7 +418,11 @@ func getTagString(f *parser.Field, rmTags []string) (string, error) { } for _, rmTag := range rmTags { - field.Tags.Remove(rmTag) + for _, t := range field.Tags { + if t.IsDefault && strings.EqualFold(t.Key, rmTag) { + field.Tags.Remove(t.Key) + } + } } var tagString string diff --git a/cmd/hz/thrift/tags.go b/cmd/hz/thrift/tags.go index 96c414dca..115d79b28 100644 --- a/cmd/hz/thrift/tags.go +++ b/cmd/hz/thrift/tags.go @@ -205,21 +205,27 @@ func defaultBindingTags(f *parser.Field) []model.Tag { val := getJsonValue(f, v[0]) out[0] = tag("json", val) } else { - out[0] = jsonTag(f) + t := jsonTag(f) + t.IsDefault = true + out[0] = t } if v := getAnnotation(f.Annotations, AnnotationQuery); len(v) > 0 { val := checkRequire(f, v[0]) out[1] = tag(BindingTags[AnnotationQuery], val) } else { val := checkRequire(f, checkSnakeName(f.Name)) - out[1] = tag(BindingTags[AnnotationQuery], val) + t := tag(BindingTags[AnnotationQuery], val) + t.IsDefault = true + out[1] = t } if v := getAnnotation(f.Annotations, AnnotationForm); len(v) > 0 { val := checkRequire(f, v[0]) out[2] = tag(BindingTags[AnnotationForm], val) } else { val := checkRequire(f, checkSnakeName(f.Name)) - out[2] = tag(BindingTags[AnnotationForm], val) + t := tag(BindingTags[AnnotationForm], val) + t.IsDefault = true + out[2] = t } return out }