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

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

Closed
phm07 opened this issue Nov 21, 2023 · 1 comment
Closed

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

phm07 opened this issue Nov 21, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@phm07
Copy link

phm07 commented Nov 21, 2023

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
Copy link
Owner

jmattheis commented Nov 21, 2023

Thanks for the report! Fixed with v1.1.1

@jmattheis jmattheis added the bug Something isn't working label Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants