jmattheis / goverter

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

Passing Converter interface to mapExtend ? #38

Closed jeremie-abt closed 1 year ago

jeremie-abt commented 1 year ago

Have you read the project readme?

Describe your question I'm wondering why there isn't the possibility to ask for the Converter interface when using mapExtend ?

Taking the documentation exemple, this would give something like this :

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

type Input struct {
    ID int
    FirstName string
    LastName string
}
type Output struct {
    ID int
    FullName string
    Age int
}
func ExtendFullName(c Converter, source Input) string {
    return source.FirstName + " " + source.LastName
}
func DefaultAge() int { return 42 }

This would perfectly solve my use case, but maybe there's another solution :

// goverter:converter
type Converter interface {
    // goverter:mapExtend Car ExtendCar
    Convert(source Input) Output

    ConvertBmwToCar(source *Bmw) *outputBmw
    ConvertFerrariToCar(source *Ferrari) *outputFerrari
}

func ExtendCar(input *Input) []Car {
    // I need Converter here so that I can loop on Bmw and Ferrari's array of the Input struct and
    // call c.ConvertBmwToCar(input.Bmw[0]) || c.ConvertFerrariToCar(input.Ferrari[0])
}

type Input struct {
    Bmw    []*Bmw
    Ferrari []*Ferrari
}

type Car interface{}

// implement Car
type outputBmw struct{}

// implement Car
type outputFerrari struct{}

type Output {
      Car []Car
}
jmattheis commented 1 year ago

It isn't supported yet, because it isn't implemented :P.

jmattheis commented 1 year ago

Supported in v0.11.0

jeremie-abt commented 1 year ago

Wow ! Thanks for the quickness <3 !