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

fix: tags when using trailing + regular comments #46

Merged
merged 2 commits into from Aug 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions file.go
Expand Up @@ -88,10 +88,16 @@ func parseFile(inputPath string, xxxSkip []string) (areas []textArea, err error)

if field.Doc != nil {
comments = append(comments, field.Doc.List...)
} else if field.Comment != nil {
}

// The "doc" field (above comment) is more commonly "free-form"
// due to the ability to have a much larger comment without it
// being unwieldly. As such, the "comment" field (trailing comment),
// should take precedence if there happen to be multiple tags
// specified, both in the field doc, and the field line. Whichever
// comes last, will take precedence.
if field.Comment != nil {
comments = append(comments, field.Comment.List...)
} else {
continue
}

for _, comment := range comments {
Expand Down
5 changes: 3 additions & 2 deletions main_test.go
Expand Up @@ -42,8 +42,8 @@ func TestParseWriteFile(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(areas) != 8 {
t.Fatalf("expected 8 areas to replace, got: %d", len(areas))
if len(areas) != 9 {
t.Fatalf("expected 9 areas to replace, got: %d", len(areas))
}
area := areas[0]
t.Logf("area: %v", area)
Expand Down Expand Up @@ -112,6 +112,7 @@ func TestNewTagItems(t *testing.T) {
func TestContinueParsingWhenSkippingFields(t *testing.T) {
expectedTags := []string{
`valid:"ip" yaml:"ip" json:"overrided"`,
`valid:"-"`,
`valid:"http|https"`,
`valid:"nonzero"`,
`validate:"omitempty"`,
Expand Down
9 changes: 7 additions & 2 deletions pb/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions pb/test.proto
Expand Up @@ -11,8 +11,13 @@ message IP {
}

message URL {
// @inject_tag: valid:"http|https"
string scheme = 1;
// below is an example where the line-specific comment should take precedence
// over the "doc" comment, which is known to be more free-form. On the
// resulting struct field, you should see `valid:"http|https"` added, not
// `valid:"-"`.
//
// @inject_tag: valid:"-"
string scheme = 1; // @inject_tag: valid:"http|https"
string url = 2;
// @inject_tag: valid:"nonzero"
int32 port = 3;
Expand Down