Skip to content

Commit

Permalink
Merge pull request #7 from ashmrtn/flag-parsing
Browse files Browse the repository at this point in the history
Allow a comma separated set of tag keys
  • Loading branch information
ashmrtn authored Dec 25, 2024
2 parents d609120 + fb6153b commit c491108
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
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

0 comments on commit c491108

Please sign in to comment.