Skip to content

Commit

Permalink
optimize: rm tag
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Nov 22, 2023
1 parent 171630c commit d74d314
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/hz/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
5 changes: 3 additions & 2 deletions cmd/hz/generator/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 1 addition & 11 deletions cmd/hz/protobuf/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/hz/protobuf/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 18 additions & 5 deletions cmd/hz/protobuf/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,31 +340,39 @@ 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))
out[2] = tag(BindingTags[api.E_Form], val)
} 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))
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion cmd/hz/thrift/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions cmd/hz/thrift/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit d74d314

Please sign in to comment.