Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hz): rm_tag for default json #1140

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions cmd/hz/protobuf/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
1 change: 1 addition & 0 deletions cmd/hz/protobuf/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
Expand Down
105 changes: 105 additions & 0 deletions cmd/hz/thrift/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
6 changes: 6 additions & 0 deletions cmd/hz/thrift/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
Expand Down
Loading