Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sub context to support combining nested automap and rename #79

Closed
wants to merge 1 commit into from

Conversation

goghcrow
Copy link

@goghcrow goghcrow commented Aug 7, 2023

When I Combine automap and fieldmap, it doesn't work

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

type FlatPerson struct {
	Name    string
	Age     int
	Street  string
	ZipCode string
}

type Person struct {
	Name string
	Age  int
	Address
}
type Address struct {
	StreetName string
	ZipCode    string
}

the error message was as follows

| github.com/jmattheis/goverter/example/nested.FlatPerson
|
|      | goverter:map . Address
|      |
|      |
|      |
source.       .???
target.Address.StreetName
|      |       |
|      |       | string
|      |
|      | github.com/jmattheis/goverter/example/nested.Address
|
| github.com/jmattheis/goverter/example/nested.Person

Cannot match the target field with the source entry: "StreetName" does not exist.

The solution might be to add the sub context when createing subMethod

It works fine.

package generated

import nested "github.com/jmattheis/goverter/example/nested"

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source nested.FlatPerson) nested.Person {
	var nestedPerson nested.Person
	nestedPerson.Name = source.Name
	nestedPerson.Age = source.Age
	nestedPerson.Address = c.nestedFlatPersonToNestedAddress(source)
	return nestedPerson
}
func (c *ConverterImpl) nestedFlatPersonToNestedAddress(source nested.FlatPerson) nested.Address {
	var nestedAddress nested.Address
	nestedAddress.StreetName = source.Street
	nestedAddress.ZipCode = source.ZipCode
	return nestedAddress
}

It works fine, too, and even handles nested cases. But I don't know if there are other problems

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

type FlatPerson struct {
	Name    string
	Age     int
	Street  string
	ZipCode string
}

type Person struct {
	Name string
	Age  int
	Address
}
type Address struct {
	StreetInfo
	ZipCode string
}
type StreetInfo struct {
	Name string
}

generated

package generated

import nested "github.com/jmattheis/goverter/example/nested"

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source nested.FlatPerson) nested.Person {
	var nestedPerson nested.Person
	nestedPerson.Name = source.Name
	nestedPerson.Age = source.Age
	nestedPerson.Address = c.nestedFlatPersonToNestedAddress(source)
	return nestedPerson
}
func (c *ConverterImpl) nestedFlatPersonToNestedAddress(source nested.FlatPerson) nested.Address {
	var nestedAddress nested.Address
	nestedAddress.StreetInfo = c.nestedFlatPersonToNestedStreetInfo(source)
	nestedAddress.ZipCode = source.ZipCode
	return nestedAddress
}
func (c *ConverterImpl) nestedFlatPersonToNestedStreetInfo(source nested.FlatPerson) nested.StreetInfo {
	var nestedStreetInfo nested.StreetInfo
	nestedStreetInfo.Name = source.Street
	return nestedStreetInfo
}

@goghcrow goghcrow changed the title add sub context add sub context to support combining nested automap and rename Aug 7, 2023
@jmattheis
Copy link
Owner

As described in #80 (comment). The changes have some edge-cases that aren't properly handled

@jmattheis jmattheis closed this Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants