jmattheis / goverter

Generate type-safe Go converters by simply defining an interface
https://goverter.jmattheis.de/
MIT License
502 stars 47 forks source link

Goverter fails if one of the properties' types is error #102

Closed phm07 closed 10 months ago

phm07 commented 10 months ago

Take this code snippet for example:

package example

type SomeType struct{}

// goverter:converter
type Converter interface {
    AToB(A) B
}

type A struct {
    SomeProperty SomeType
}

type B struct {
    SomeProperty error
}

Usually, you would expect a message like this:

Error while creating converter method:
    func (example.Converter).AToB(example.A) example.B

| example.A
|
|      | example.SomeType
|      |
source.SomeProperty
target.SomeProperty
|      |
|      | error
|
| example.B

TypeMismatch: Cannot convert example.SomeType to error

However, goverter panics if one of or both types are error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1043e3dd4]

goroutine 1 [running]:
go/types.(*Package).Name(...)
        /usr/local/go/src/go/types/package.go:36
github.com/jmattheis/goverter/xtype.(*Type).asID(0x14000093560?, 0x0?, 0xc0?)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/xtype/type.go:261 +0x44
github.com/jmattheis/goverter/xtype.(*Type).UnescapedID(...)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/xtype/type.go:256
github.com/jmattheis/goverter/generator.(*generator).Build(0x14000090fa0, 0x14000093560, 0x14000093560?, 0x140001b15f0, 0x140001b12b0, 0x140001b1040?)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/generator/generator.go:281 +0x338
github.com/jmattheis/goverter/builder.(*Struct).Build(0x30, {0x1044d5928, 0x14000090fa0?}, 0x14000093560, 0x140000a6720, 0x140001b1040, 0x140001b0ea0)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/builder/struct.go:80 +0x490
github.com/jmattheis/goverter/generator.(*generator).buildNoLookup(0x1044909c0?, 0x140001bc060?, 0x104403f09?, 0x140001b1040, 0x140001b0ea0)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/generator/generator.go:136 +0xd4
github.com/jmattheis/goverter/generator.(*generator).buildMethod(0x14000090fa0, 0x14000090f80, 0x1?)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/generator/generator.go:114 +0x82c
github.com/jmattheis/goverter/generator.(*generator).buildMethods(0x14000090fa0, 0xd?)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/generator/generator.go:46 +0x1b0
github.com/jmattheis/goverter/generator.generateConverter(0x140000aa6e0, {{0x140000aa6e0?, 0x0?}}, 0x140001960b0)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/generator/generate.go:60 +0x5c0
github.com/jmattheis/goverter/generator.Generate({0x140000a8308, 0x1, 0x1?}, {{0x0?, 0x0?}})
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/generator/generate.go:40 +0xc8
github.com/jmattheis/goverter.generateConvertersRaw(0x14000095450)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/runner.go:51 +0x178
github.com/jmattheis/goverter.GenerateConverters(0x140000a0090?)
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/runner.go:25 +0x1c
main.main()
        /Users/paul/go/pkg/mod/github.com/jmattheis/goverter@v1.1.0/cmd/goverter/main.go:18 +0x80
jmattheis commented 10 months ago

Thanks for the report! Fixed with v1.1.1