Closed phm07 closed 9 months ago
Good catch, generally goverter handles typealias fairly well, because they get automatically normalized to the actual type. any
seems like a special type alias, because it isn't exactly interface{}
from a type point of view.
When directly specifying methods, goverter should check if the types are assignable. So this bug fix should include the "feature" of using interfaces in custom mapping methods. Example:
import (
"fmt"
"time"
)
// goverter:converter
type Converter interface {
// goverter:map Value | ToString
Convert(source Input) Output
}
type Input struct { Value time.Time }
type Output struct { Value string }
func ToString(s fmt.Stringer) string {
return s.String()
}
Take this code snippet:
Goverter will fail to generate with the following error message:
This is problematic since some APIs still use the
interface{}
type while the Go standard library now only uses theany
type alias. This means that in some cases you have to write conversion methods that takeinterface{}
s as arguments and wrap the corresponding standard library methods.Fixing this would probably mean fully supporting type aliases. I don't know how complex the internal implementation for that would be, so maybe handling the special case for
any
would suffice as it's the most used type alias in Go.