From c299e854de4de4c9228159c530b959072ad68c2c Mon Sep 17 00:00:00 2001 From: fgy Date: Mon, 17 Jun 2024 15:09:25 +0800 Subject: [PATCH] fix: rm json tag --- cmd/hz/protobuf/tag_test.go | 127 ++++++++++++++++++++++++++++++++++++ cmd/hz/protobuf/tags.go | 1 + cmd/hz/thrift/tag_test.go | 105 +++++++++++++++++++++++++++++ cmd/hz/thrift/tags.go | 6 ++ 4 files changed, 239 insertions(+) diff --git a/cmd/hz/protobuf/tag_test.go b/cmd/hz/protobuf/tag_test.go index e8e9561ec..6127fec1c 100644 --- a/cmd/hz/protobuf/tag_test.go +++ b/cmd/hz/protobuf/tag_test.go @@ -151,3 +151,130 @@ func TestTagGenerate(t *testing.T) { } } } + +func TestRmTag(t *testing.T) { + type TagStruct struct { + Annotation string + GeneratedTag string + ActualTag string + } + + tagList := []TagStruct{ + { + Annotation: "query", + GeneratedTag: "protobuf:\"bytes,1,opt,name=QueryTag\" query:\"query\"", + }, + { + Annotation: "raw_body", + GeneratedTag: "protobuf:\"bytes,2,opt,name=RawBodyTag\" raw_body:\"raw_body\"", + }, + { + Annotation: "path", + GeneratedTag: "protobuf:\"bytes,3,opt,name=PathTag\" path:\"path\"", + }, + { + Annotation: "form", + GeneratedTag: "protobuf:\"bytes,4,opt,name=FormTag\" form:\"form\"", + }, + { + Annotation: "cookie", + GeneratedTag: "protobuf:\"bytes,5,opt,name=CookieTag\" cookie:\"cookie\"", + }, + { + Annotation: "header", + GeneratedTag: "protobuf:\"bytes,6,opt,name=HeaderTag\" header:\"header\"", + }, + { + Annotation: "body", + GeneratedTag: "bytes,7,opt,name=BodyTag\" form:\"body\"", + }, + { + Annotation: "go.tag", + GeneratedTag: "bytes,8,opt,name=GoTag\" form:\"form\" goTag:\"tag\" header:\"header\" json:\"json\" query:\"query\"", + }, + { + Annotation: "vd", + GeneratedTag: "bytes,9,opt,name=VdTag\" form:\"VdTag\" query:\"VdTag\" vd:\"$!='?'\"", + }, + { + Annotation: "non", + GeneratedTag: "bytes,10,opt,name=DefaultTag\" form:\"DefaultTag\" query:\"DefaultTag\"", + }, + { + Annotation: "query required", + GeneratedTag: "bytes,11,req,name=ReqQuery\" query:\"query,required\"", + }, + { + Annotation: "query optional", + GeneratedTag: "bytes,12,opt,name=OptQuery\" query:\"query\"", + }, + { + Annotation: "body required", + GeneratedTag: "protobuf:\"bytes,13,req,name=ReqBody\" form:\"body,required\" json:\"body,required\"", + }, + { + Annotation: "body optional", + GeneratedTag: "protobuf:\"bytes,14,opt,name=OptBody\" form:\"body\" json:\"body,omitempty\"", + }, + { + Annotation: "go.tag required", + GeneratedTag: "protobuf:\"bytes,15,req,name=ReqGoTag\" form:\"ReqGoTag,required\" json:\"json\" query:\"ReqGoTag,required\"", + }, + { + Annotation: "go.tag optional", + GeneratedTag: "bytes,16,opt,name=OptGoTag\" form:\"OptGoTag\" json:\"json\" query:\"OptGoTag\"", + }, + { + Annotation: "go tag cover query", + GeneratedTag: "bytes,17,req,name=QueryGoTag\" query:\"queryTag\"", + }, + } + + in, err := ioutil.ReadFile("./test_data/protobuf_tag_test.out") + if err != nil { + t.Fatal(err) + } + + req := &pluginpb.CodeGeneratorRequest{} + err = proto.Unmarshal(in, req) + if err != nil { + t.Fatalf("unmarshal stdin request error: %v", err) + } + + opts := protogen.Options{} + gen, err := opts.New(req) + rmTags := []string{"json"} + + for _, f := range gen.Files { + if f.Proto.GetName() == "test_tag.proto" { + fileInfo := newFileInfo(f) + for _, message := range fileInfo.allMessages { + for idx, field := range message.Fields { + tags := structTags{ + {"protobuf", fieldProtobufTagValue(field)}, + } + err = injectTagsToStructTags(field.Desc, &tags, true, rmTags) + if err != nil { + t.Fatal(err) + } + var actualTag string + for i, tag := range tags { + if i == 0 { + actualTag = tag[0] + ":" + "\"" + tag[1] + "\"" + } else { + actualTag = actualTag + " " + tag[0] + ":" + "\"" + tag[1] + "\"" + } + } + tagList[idx].ActualTag = actualTag + + } + } + } + } + + for i := range tagList { + if !strings.Contains(tagList[i].ActualTag, tagList[i].GeneratedTag) { + t.Fatalf("expected tag: '%s', but autual tag: '%s'", tagList[i].GeneratedTag, tagList[i].ActualTag) + } + } +} diff --git a/cmd/hz/protobuf/tags.go b/cmd/hz/protobuf/tags.go index 4880f99a0..afa752fd2 100644 --- a/cmd/hz/protobuf/tags.go +++ b/cmd/hz/protobuf/tags.go @@ -332,6 +332,7 @@ func defaultBindingStructTags(f protoreflect.FieldDescriptor) []model.Tag { for _, tag := range bindingTags { if vv := checkFirstOption(tag, opts); vv != nil { out[0] = reflectJsonTag(f) + out[0].IsDefault = true return out[:1] } } diff --git a/cmd/hz/thrift/tag_test.go b/cmd/hz/thrift/tag_test.go index 108dea881..8e8192c81 100644 --- a/cmd/hz/thrift/tag_test.go +++ b/cmd/hz/thrift/tag_test.go @@ -127,3 +127,108 @@ func TestInsertTag(t *testing.T) { } } } + +func TestRmTag(t *testing.T) { + data, err := ioutil.ReadFile("./test_data/thrift_tag_test.out") + if err != nil { + t.Fatal(err) + } + req, err := plugin.UnmarshalRequest(data) + if err != nil { + t.Fatal(err) + } + + plu := new(Plugin) + plu.req = req + plu.args = new(config.Argument) + plu.rmTags = []string{"json"} + + type TagStruct struct { + Annotation string + GeneratedTag string + ActualTag string + } + + tagList := []TagStruct{ + { + Annotation: "query", + GeneratedTag: "query:\"query\"", + }, + { + Annotation: "raw_body", + GeneratedTag: "raw_body:\"raw_body\"", + }, + { + Annotation: "path", + GeneratedTag: "path:\"path\"", + }, + { + Annotation: "form", + GeneratedTag: "form:\"form\"", + }, + { + Annotation: "cookie", + GeneratedTag: "cookie:\"cookie\"", + }, + { + Annotation: "header", + GeneratedTag: "header:\"header\"", + }, + { + Annotation: "body", + GeneratedTag: "form:\"body\" json:\"body\"", + }, + { + Annotation: "go.tag", + GeneratedTag: "", + }, + { + Annotation: "vd", + GeneratedTag: "form:\"VdTag\" query:\"VdTag\" vd:\"$!='?'\"", + }, + { + Annotation: "non", + GeneratedTag: "form:\"DefaultTag\" query:\"DefaultTag\"", + }, + { + Annotation: "query required", + GeneratedTag: "query:\"query,required\"", + }, + { + Annotation: "query optional", + GeneratedTag: "query:\"query\"", + }, + { + Annotation: "body required", + GeneratedTag: "form:\"body,required\" json:\"body,required\"", + }, + { + Annotation: "body optional", + GeneratedTag: "form:\"body\" json:\"body,omitempty\"", + }, + { + Annotation: "go.tag required", + GeneratedTag: "form:\"ReqGoTag,required\" query:\"ReqGoTag,required\"", + }, + { + Annotation: "go.tag optional", + GeneratedTag: "form:\"OptGoTag\" query:\"OptGoTag\"", + }, + { + // go.tag is generated by thriftgo, so in the plugin, the json should be removed, but it does not matter result + Annotation: "go tag cover query", + GeneratedTag: "form:\"QueryGoTag,required\"", + }, + } + + tags, err := plu.InsertTag() + if err != nil { + t.Fatal(err) + } + for i, tag := range tags { + tagList[i].ActualTag = tag.Content + if !strings.Contains(tagList[i].ActualTag, tagList[i].GeneratedTag) { + t.Fatalf("expected tag: '%s', but autual tag: '%s'", tagList[i].GeneratedTag, tagList[i].ActualTag) + } + } +} diff --git a/cmd/hz/thrift/tags.go b/cmd/hz/thrift/tags.go index 115d79b28..286d7457b 100644 --- a/cmd/hz/thrift/tags.go +++ b/cmd/hz/thrift/tags.go @@ -197,6 +197,12 @@ func defaultBindingTags(f *parser.Field) []model.Tag { for _, tag := range bindingTags { if v := getAnnotation(f.Annotations, tag); len(v) > 0 { out[0] = jsonTag(f) + // bugfix: if there is user's tag, json tag is always default to set + out[0].IsDefault = true + //// user's json tag + //if v := getAnnotation(f.Annotations, AnnotationBody); len(v) ==0 { + // out[0].IsDefault = false + //} return out[:1] } }