-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
243 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package i | ||
|
||
func excludedConsumer(e TestExcluded) string { | ||
return e.A | ||
} | ||
|
||
func shouldNotFailOnIgnoreDirective() (Test, error) { | ||
// directive on previous line | ||
//exhaustruct:ignore | ||
_ = Test2{} | ||
|
||
// directive at the end of the line | ||
_ = Test{} //exhaustruct:ignore | ||
|
||
// some style weirdness | ||
_ = | ||
//exhaustruct:ignore | ||
Test3{ | ||
B: 0, | ||
} | ||
|
||
// directive after the literal | ||
_ = Test{ | ||
B: 0, | ||
} //exhaustruct:ignore | ||
|
||
//exhaustruct:ignore | ||
return Test{}, nil | ||
} | ||
|
||
func shouldFailOnExcludedButEnforced() { | ||
// directive on previous line associated with different ast leaf | ||
//exhaustruct:enforce | ||
_ = excludedConsumer(TestExcluded{B: 0}) // want "i.TestExcluded is missing field A" | ||
|
||
// initially excluded, but enforced | ||
//exhaustruct:enforce | ||
_ = TestExcluded{} // want "i.TestExcluded is missing fields A, B" | ||
} | ||
|
||
func shouldFailOnMisappliedDirectives() { | ||
// wrong directive name | ||
//exhaustive:enforce | ||
_ = TestExcluded{B: 0} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package comment | ||
|
||
import ( | ||
"go/ast" | ||
"go/token" | ||
"sync" | ||
) | ||
|
||
type Cache struct { | ||
comments map[*ast.File]ast.CommentMap | ||
mu sync.RWMutex | ||
} | ||
|
||
// Get returns a comment map for a given file. In case if a comment map is not | ||
// found, it creates a new one. | ||
func (c *Cache) Get(fset *token.FileSet, f *ast.File) ast.CommentMap { | ||
c.mu.RLock() | ||
if cm, ok := c.comments[f]; ok { | ||
c.mu.RUnlock() | ||
return cm | ||
} | ||
c.mu.RUnlock() | ||
|
||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
if c.comments == nil { | ||
c.comments = make(map[*ast.File]ast.CommentMap) | ||
} | ||
|
||
cm := ast.NewCommentMap(fset, f, f.Comments) | ||
c.comments[f] = cm | ||
|
||
return cm | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package comment | ||
|
||
import ( | ||
"go/ast" | ||
"strings" | ||
) | ||
|
||
type Directive string | ||
|
||
const ( | ||
prefix = `//exhaustruct:` | ||
DirectiveIgnore Directive = prefix + `ignore` | ||
DirectiveEnforce Directive = prefix + `enforce` | ||
) | ||
|
||
// HasDirective parses a directive from a given list of comments. | ||
// If no directive is found, the second return value is `false`. | ||
func HasDirective(comments []*ast.CommentGroup, expected Directive) bool { | ||
for _, cg := range comments { | ||
for _, commentLine := range cg.List { | ||
if strings.HasPrefix(commentLine.Text, string(expected)) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package comment_test | ||
|
||
import ( | ||
"go/ast" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/GaijinEntertainment/go-exhaustruct/v3/internal/comment" | ||
) | ||
|
||
func TestParseDirective(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
comments []*ast.CommentGroup | ||
directive comment.Directive | ||
found bool | ||
}{ | ||
{ | ||
name: "no directive", | ||
comments: []*ast.CommentGroup{ | ||
{ | ||
List: []*ast.Comment{ | ||
{ | ||
Text: "// some comment", | ||
}, | ||
}, | ||
}, | ||
}, | ||
directive: comment.DirectiveIgnore, | ||
found: false, | ||
}, | ||
{ | ||
name: "directive found", | ||
comments: []*ast.CommentGroup{ | ||
{ | ||
List: []*ast.Comment{ | ||
{ | ||
Text: "//exhaustruct:ignore", | ||
}, | ||
{ | ||
Text: "// some comment", | ||
}, | ||
{ | ||
Text: "//exhaustruct:enforce", | ||
}, | ||
}, | ||
}, | ||
}, | ||
directive: comment.DirectiveIgnore, | ||
found: true, | ||
}, | ||
{ | ||
name: "directive found (partial line match)", | ||
comments: []*ast.CommentGroup{ | ||
{ | ||
List: []*ast.Comment{ | ||
{ | ||
Text: "//exhaustruct:ignore", | ||
}, | ||
{ | ||
Text: "// some comment", | ||
}, | ||
{ | ||
Text: "//exhaustruct:enforce beacuse of some reason", | ||
}, | ||
}, | ||
}, | ||
}, | ||
directive: comment.DirectiveEnforce, | ||
found: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equal(t, tt.found, comment.HasDirective(tt.comments, tt.directive)) | ||
}) | ||
} | ||
} |