jmattheis / goverter

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

Add reverse operation of Source Object Mapping #55

Closed shuqingzai closed 1 year ago

shuqingzai commented 1 year ago

Have you read the project readme?

Describe your question A clear and concise description of what the question is. Include errors and go source files.

mapping#source-object The reverse operation does not seem to be supported, what should I do?

package example

// goverter:converter
type Converter interface {
    // goverter:map Address .
    Convert(Person) FlatPerson
}

type FlatPerson struct {
    Name    string
    Age     int
    Street  string
    ZipCode string
}
type Person struct {
    Name    string
    Age     int
    Address Address
}
type Address struct {
    Street  string
    ZipCode string
}
jmattheis commented 1 year ago

Currently, there is no automatic way for this. So you've to define every field via map:

// goverter:converter
type Converter interface {
    // goverter:map Address.Street Street
    // goverter:map Address.ZipCode ZipCode
    Convert(Person) FlatPerson
}
jmattheis commented 1 year ago

With v0.15.0 you can use goverter:autoMap: https://goverter.jmattheis.de/#/conversion/mapping?id=auto-map

The example of the first comment can be fixed with:

// goverter:converter
type Converter interface {
    // goverter:autoMap Address
    Convert(Person) FlatPerson
}