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

Handle the case if json tag was not overwritten #23

Open
wants to merge 3 commits into
base: master
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
19 changes: 17 additions & 2 deletions cmd/protoc-gen-go/internal_gengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,13 @@ func getAdditionalTags(m *messageInfo, field *protogen.Field) [][2]string {
{"json", fieldJSONTagValue(field)},
}
}
return buildTags(o.goStructTags)
tags := buildTags(o.goStructTags)
if !contains(tags, func(kv [2]string) bool {
return kv[0] == "json"
}) {
tags = append(tags, [2]string{"json", fieldJSONTagValue(field)})
}
return tags
}

func buildTags(wrappedTags string) [][2]string {
Expand All @@ -608,7 +614,7 @@ func buildTags(wrappedTags string) [][2]string {
if len(submatch) != 3 {
continue
}
if submatch[1] == "protobuf_key" || submatch[1] == "protobuf_val" || submatch[1] == "protobuf" {
if submatch[1] == "protobuf_key" || submatch[1] == "protobuf_val" || submatch[1] == "protobuf" || submatch[1] == "protobuf_oneof" {
continue
}
tags = append(tags, [2]string{submatch[1], submatch[2]})
Expand Down Expand Up @@ -1122,3 +1128,12 @@ func (c trailingComment) String() string {
}
return s
}

func contains[E any](arr []E, f func(e E) bool) bool {
for _, e := range arr {
if found := f(e); found {
return true
}
}
return false
}
50 changes: 50 additions & 0 deletions cmd/protoc-gen-go/internal_gengo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/infiniteloopcloud/protoc-gen-go-types/compiler/protogen"
"github.com/infiniteloopcloud/protoc-gen-go-types/internal/filedesc"
"github.com/infiniteloopcloud/protoc-gen-go-types/parser"
)

Expand Down Expand Up @@ -92,3 +93,52 @@ func TestGetAdditionalTags(t *testing.T) {
t.Errorf("First tag should be `true`, instead of %s", tags[2][1])
}
}

func TestGetAdditionalTagsNoJsonOverride(t *testing.T) {
TypeOverride = true
overrideFields = map[string]map[string]overrideParams{
"TestStruct": {
"TestField": overrideParams{
goStructTags: `omitempty;boil=donno;validate=true`,
},
},
}
tags := getAdditionalTags(&messageInfo{
Message: &protogen.Message{
GoIdent: protogen.GoIdent{
GoName: "TestStruct",
},
},
}, &protogen.Field{
GoName: "TestField",
Desc: &filedesc.Field{
Base: filedesc.Base{
L0: filedesc.BaseL0{
FullName: "my_test_field",
},
},
},
})

if len(tags) != 3 {
t.Fatal("Tags length must be 3")
}
if tags[0][0] != "json" {
t.Errorf("First tag should be `json`, instead of %s", tags[0][0])
}
if tags[0][1] != "my_test_field,omitempty" {
t.Errorf("First tag should be `my_test_field,omitempty`, instead of %s", tags[0][1])
}
if tags[1][0] != "boil" {
t.Errorf("First tag should be `boil`, instead of %s", tags[1][0])
}
if tags[1][1] != "donno" {
t.Errorf("First tag should be `donno`, instead of %s", tags[1][1])
}
if tags[2][0] != "validate" {
t.Errorf("First tag should be `validate`, instead of %s", tags[2][0])
}
if tags[2][1] != "true" {
t.Errorf("First tag should be `true`, instead of %s", tags[2][1])
}
}