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 Interface{} (any) type conversion #42

Closed jdmeyer3 closed 1 year ago

jdmeyer3 commented 1 year ago

Is your feature request related to a problem? Please describe. I have several structs that are generated with a type of interface{}. Converting an interface to an interface is unsupported

Describe the solution you'd like Using the example below, allow goverter to convert a struct that has an interface to another struct that has the same interface type

package advanced

// goverter:converter
type ConverterInterface interface {
    ConvertHouse(source DBHouseNamesI) APIHouseNamesI
}

type DBHouseNamesI struct {
    Name any
}

type APIHouseNamesI struct {
    Name any
}

Describe alternatives you've considered Try to get away from a type any, but that is a limitation of other code generations.

jmattheis commented 1 year ago

Goverter can't know how to convert any to any, you need to define the function yourself:

// goverter:converter
// goverter:extend ConvertAny
type ConverterInterface interface {
    ConvertHouse(source DBHouseNamesI) APIHouseNamesI
}

type DBHouseNamesI struct {
    Name any
}

type APIHouseNamesI struct {
    Name any
}

func ConvertAny(source any) any {
    return source
}
jdmeyer3 commented 1 year ago

That works for me, thank you