From 4bf2616b8a4e4b731370ada5d33db1e47f395755 Mon Sep 17 00:00:00 2001 From: kerry Date: Tue, 28 Mar 2023 23:49:34 +1100 Subject: [PATCH] feat: removeDuplicateTags() validates tags and panic with meaningful error message --- plugin/modelgen/models.go | 5 +++++ plugin/modelgen/models_test.go | 23 ++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index 855ed1001fa..529cbd9fcb2 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -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 diff --git a/plugin/modelgen/models_test.go b/plugin/modelgen/models_test.go index f4f3f35834e..d5427531acd 100644 --- a/plugin/modelgen/models_test.go +++ b/plugin/modelgen/models_test.go @@ -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", @@ -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) + } } }) }