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

Allow a comma separated set of tag keys #7

Merged
merged 2 commits into from
Dec 25, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions pkg/allowtags/allowtags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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{
Expand Down
23 changes: 23 additions & 0 deletions pkg/allowtags/allowtags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading