From 344c807093756d7a8bcd514e0949ef7ff6592b96 Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Wed, 25 Dec 2024 10:36:41 -0800 Subject: [PATCH 1/2] Allow comma separated values for input --- pkg/allowtags/allowtags.go | 12 ++++++++++-- pkg/allowtags/allowtags_test.go | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg/allowtags/allowtags.go b/pkg/allowtags/allowtags.go index cab647e..a97e182 100644 --- a/pkg/allowtags/allowtags.go +++ b/pkg/allowtags/allowtags.go @@ -348,7 +348,14 @@ func (tks *tagKeySet) String() string { } func (tks *tagKeySet) Set(value string) error { - *tks = append(*tks, value) + allValues := strings.Split(value, ",") + + for _, val := range allValues { + if len(val) > 0 { + *tks = append(*tks, val) + } + } + return nil } @@ -365,7 +372,8 @@ func New() *analysis.Analyzer { &at.allowedKeys, "allow-key", //nolint:lll - "tag key name to allow. Pass the flag multiple times to allow multiple keys", + "tag key name to allow. Pass the flag multiple times or separate values"+ + "with commas to specify multiple keys", ) return &analysis.Analyzer{ diff --git a/pkg/allowtags/allowtags_test.go b/pkg/allowtags/allowtags_test.go index 680a9d3..4e20ff7 100644 --- a/pkg/allowtags/allowtags_test.go +++ b/pkg/allowtags/allowtags_test.go @@ -201,6 +201,29 @@ func (s *AllowTagsSuite) TestLint() { inputTags: "`json :\"field,omitempty\"`", expectedMessage: "// want `invalid tag key character` ", }, + { + name: "CommaSeparatedKeys", + allowTags: []string{ + "binary,json", + }, + inputTags: "`json:\"field,omitempty\" binary:\"field\"`", + }, + { + name: "CommaSeparatedKeys_EmptyKey", + allowTags: []string{ + ",json", + }, + inputTags: "`json:\"field,omitempty\" binary:\"field\"`", + expectedMessage: "// want `unknown tag key 'binary'`", + }, + { + name: "CommaSeparatedKeysAndOtherKey", + allowTags: []string{ + "binary,json", + "xml", + }, + inputTags: "`json:\"field,omitempty\" binary:\"field\" xml:\"foo\"`", + }, } for _, test := range table { From fb6153b33b98cd039808b2dc775f28e4fc94f3d4 Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Wed, 25 Dec 2024 10:37:59 -0800 Subject: [PATCH 2/2] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a825a59..6766ed4 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ your whole project with AllowTags just run `allowtags --allow-key key1 ./...`. ### Flags AllowTags requires all tags that it allows be explicitly given. To specify multiple tag keys pass the `--allow-key` flag multiple times, each time with a -different tag key. +different tag key, or separate different tag keys with a comma. ## Limitations AllowTags uses a different parsing method than govet and the golang standard