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 1 commit
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: 18 additions & 1 deletion cmd/protoc-gen-go/internal_gengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,15 @@ 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"
}) {
return append([][2]string{
{"json", fieldJSONTagValue(field)},
}, tags...)
}
return tags
}

func buildTags(wrappedTags string) [][2]string {
Expand Down Expand Up @@ -1122,3 +1130,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])
}
}