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

feat: removeDuplicateTags() validates tags and panic with meaningful … #2597

Merged
merged 1 commit into from
Mar 28, 2023
Merged
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
5 changes: 5 additions & 0 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ func removeDuplicateTags(t string) string {
// iterate backwards through tags so appended goTag directives are prioritized
for i := len(tt) - 1; i >= 0; i-- {
ti := tt[i]
// check if ti contains ":", and not contains any empty space. if not, tag is in wrong format
if !strings.Contains(ti, ":") || strings.Contains(ti, " ") {
panic(fmt.Errorf("wrong format of tags: %s. goTag directive should be in format: @goTag(key: \"something\", value:\"value1,value2,etc\"), no empty space is allowed", t))
}

kv := strings.Split(ti, ":")
if len(kv) == 0 || processed[kv[0]] {
continue
Expand Down
23 changes: 18 additions & 5 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,10 @@ func TestRemoveDuplicate(t *testing.T) {
t string
}
tests := []struct {
name string
args args
want string
name string
args args
want string
wantPanic bool
}{
{
name: "Duplicate Test with 1",
Expand Down Expand Up @@ -395,11 +396,23 @@ func TestRemoveDuplicate(t *testing.T) {
},
want: "something:\"name2\" json:\"name3\"",
},
{
name: "Test value with empty space",
args: args{
t: "json:\"name, name2\"",
},
want: "json:\"name, name2\"",
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeDuplicateTags(tt.args.t); got != tt.want {
t.Errorf("removeDuplicate() = %v, want %v", got, tt.want)
if tt.wantPanic {
assert.Panics(t, func() { removeDuplicateTags(tt.args.t) }, "The code did not panic")
} else {
if got := removeDuplicateTags(tt.args.t); got != tt.want {
t.Errorf("removeDuplicate() = %v, want %v", got, tt.want)
}
}
})
}
Expand Down