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: validate generics #40

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 11 additions & 4 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,17 @@ func exprName(expr ast.Expr) string {
return i.Name
}

s, ok := expr.(*ast.SelectorExpr)
if !ok {
return ""
if s, ok := expr.(*ast.SelectorExpr); ok {
return s.Sel.Name
}

if i, ok := expr.(*ast.IndexExpr); ok {
return exprName(i.X)
}

if i, ok := expr.(*ast.IndexListExpr); ok {
return exprName(i.X)
}

return s.Sel.Name
return ""
}
2 changes: 1 addition & 1 deletion pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAll(t *testing.T) {
testdata := filepath.Join(filepath.Dir(filepath.Dir(wd)), "testdata")

a, err := analyzer.NewAnalyzer(
[]string{".*\\.Test", ".*\\.Test2", ".*\\.Embedded", ".*\\.External"},
[]string{".*\\.Test", ".*\\.Test2", ".*\\.Embedded", ".*\\.External", ".*\\.Generic"},
[]string{".*Excluded$"},
)
if err != nil {
Expand Down
34 changes: 33 additions & 1 deletion testdata/src/s/s.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ type Test2 struct {
External e.External
}

type Generic[T any] struct {
A T
B int
C float32
D bool
E string `exhaustruct:"optional"`
}

type Generic2[T any, V any] struct {
A T
B V
C float32
D bool
E string `exhaustruct:"optional"`
}

func shouldPass() Test {
return Test{
A: "a",
Expand Down Expand Up @@ -101,6 +117,22 @@ func shouldFailWithMissingFields() Test {
}
}

func shouldFailWithMissingFieldsGeneric() Generic[string] {
return Generic[string]{ // want "C is missing in Generic"
A: "a",
B: 1,
D: false,
}
}

func shouldFailWithMissingFieldsGeneric2() Generic2[string, int] {
return Generic2[string, int]{ // want "C is missing in Generic2"
A: "a",
B: 1,
D: false,
}
}

// Unchecked is a struct not listed in StructPatternList
type Unchecked struct {
A int
Expand Down Expand Up @@ -131,7 +163,7 @@ func shouldFailOnEmbedded() Test2 {
}
}

func shoildFailOnExternal() Test2 {
func shouldFailOnExternal() Test2 {
return Test2{
External: e.External{ // want "A is missing in External"
B: "",
Expand Down