jmattheis / goverter

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

return error from mapExtend func #43

Closed YasserRabee closed 1 year ago

YasserRabee commented 1 year ago

Is your feature request related to a problem? Please describe.

We use mapExtend to map two fields from the source type to a single field in the target. Applying this mapping can return an error and it should be propagated back to the main convert method.

goverter prohibits returning multiple values from mapExtend functions with error xx has no or too many returns

Describe the solution you'd like A clear and concise description of what you want to happen.

Support returning errors the same as goverter:extend


// goverter:converter
type Converter interface {
    // goverter:mapExtend FullName ExtendFullName
    Convert(source Input) (Output, error)
}

type Input struct {
    ID int
    FirstName string
    LastName string
}
type Output struct {
    ID int
    FullName string
}
func ExtendFullName(source Input) (string, error) {
    // .... some crazy parsing logic that can return an error
    if err != nil {
        return "", nil
    }
    return source.FirstName + " " + source.LastName, nil
}

Describe alternatives you've considered

We didn't find a possible alternative to use goverter in that scenario. We tried multiple solutions that all do the parsing outside goverter. While it was possible, we missed the best part of having everything generated automatically.

jmattheis commented 1 year ago

Yeah, this is currently unsupported, but it would be a good addition. Currently, this can't be easily implemented, because the method that handles the mapExtend has no access to the error returning functionality that already exists.

YasserRabee commented 1 year ago

I'm happy to contribute just need some guidance. I tried to implement but things got out of control quickly.

jmattheis commented 1 year ago

I don't have a clear solution in mind, because it's not that easy to implement. I'll have to try out some solutions to find out what's best.

jmattheis commented 1 year ago

Fixed in v0.13.0, see https://goverter.jmattheis.de/#/conversion/custom?id=mapping-method