jmattheis / goverter

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

Support types.Struct for embedded structs #41

Closed jdmeyer3 closed 1 year ago

jdmeyer3 commented 1 year ago

Is your feature request related to a problem? Please describe. There are several structs I cannot edit and have Nested structs. For example

package converter

// goverter:converter
type Converter interface {
    ConvertHouse(source DBHouseNames) APIHouseNames
}

type DBHouseNames struct {
    House struct {
        Name string
    }
}

type APIHouseNames struct {
    House struct {
        Name string
    }
}

The resulting code panics because goverter does not support *types.Struct.

Describe the solution you'd like Goverter to handle nested structs

jmattheis commented 1 year ago

Could you try out the anon-structs branch? This branch should include a fix for this, but I don't know if this covers all possible definitions of unnamed structs.

You can run it directly with:

$ go run github.com/jmattheis/goverter/cmd/goverter@anon-structs ./path/dir/with/goverter/inteface
jdmeyer3 commented 1 year ago

It works for most of my use cases, the only failure was when the anon struct was a pointer

Reproducible code

// goverter:converter
type Converter interface {
    ConvertHouse(source DBHouseNames) APIHouseNames
}

type DBHouseNames struct {
    House *struct {
        Name string
        Owner *string
    }
}

type APIHouseNames struct {
    House *struct {
        Name string
        Owner *string
    }
}
jdmeyer3 commented 1 year ago

And thank you very much for your prompt responses. It is greatly appreciated.

jmattheis commented 1 year ago

Yeah, no problem (:, could you retry with the anon-structs2 branch?

$ go run github.com/jmattheis/goverter/cmd/goverter@anon-structs2 ./path/dir/with/goverter/inteface
jdmeyer3 commented 1 year ago

Initial code generation works, I will need a little time to verify it returns the expected result due to the size of the structs I'm dealing with. Again, thanks for hoping on this.

jmattheis commented 1 year ago

Fixed in v0.12.0