Skip to content

Commit

Permalink
fix(generator): support for nil returns when using interface typed pa…
Browse files Browse the repository at this point in the history
…rams

As referenced on vektra#513
Types using generic typed interface params, should allow nil values on return
  • Loading branch information
jfragosoperez committed Oct 27, 2024
1 parent 5a0ad1c commit a31f038
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.

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

4 changes: 4 additions & 0 deletions pkg/fixtures/requester_iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ import "io"
type RequesterIface interface {
Get() io.Reader
}

type RequesterIfaceWithInterfaceTypedParam[T io.Reader] interface {
Get() T
}
2 changes: 1 addition & 1 deletion pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func isNillable(typ types.Type) bool {
switch t := typ.(type) {
case *types.Pointer, *types.Array, *types.Map, *types.Interface, *types.Signature, *types.Chan, *types.Slice:
return true
case *types.Named, *types.Alias:
case *types.Named, *types.Alias, *types.TypeParam:
return isNillable(t.Underlying())
}
return false
Expand Down
16 changes: 16 additions & 0 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,22 @@ func (s *GeneratorSuite) TestGeneratorChecksInterfacesForNilable() {
s.checkGeneration("requester_iface.go", "RequesterIface", false, "", "")
}

func (s *GeneratorSuite) TestGeneratorChecksInterfacesWithGenericInterfaceTypedParamForNilable() {
expectedBytes, err := os.ReadFile(getMocksPath("RequesterIfaceWithInterfaceTypedParam.go"))
s.Require().NoError(err)

expected := string(expectedBytes)
expected = expected[strings.Index(expected, "// RequesterIfaceWithInterfaceTypedParam is"):]

cfg := GeneratorConfig{
StructName: "RequesterIfaceWithInterfaceTypedParam",
WithExpecter: true,
UnrollVariadic: false,
}

s.checkGenerationWithConfig("requester_iface.go", "RequesterIfaceWithInterfaceTypedParam", cfg, expected)
}

func (s *GeneratorSuite) TestGeneratorTreatsAnyAsNilable() {
cfg := GeneratorConfig{
WithExpecter: true,
Expand Down

0 comments on commit a31f038

Please sign in to comment.