Skip to content

Commit

Permalink
feat: removeDuplicateTags() validates tags and panic with meaningful …
Browse files Browse the repository at this point in the history
…error message (#2597)

Co-authored-by: kerry <kerry@oscer.ai>
  • Loading branch information
gitxiongpan and kerry authored Mar 28, 2023
1 parent 677d854 commit 6da735c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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

0 comments on commit 6da735c

Please sign in to comment.