Skip to content

Forced pointer removal when using 'map . Target' with pointer source #220

Description

@doreshnikov

Describe the bug

When instructed to use map . Target with pointer Source, goverter generates a helper converter, stripping away the pointer. This causes go vet to spam warnings when used with Protobuf generated structures, each of which contain a lock inside, resulting in it being copied.

It becomes noticeable with definitions like these:

// goverter:converter
// goverter:output:file ./gen.go
type Converter interface {
	// goverter:map A A2
	Source2TargetBase(s *Source) *TargetBase
	// goverter:map . Base
	Source2Target(s *Source) *Target
}

To Reproduce

Full minimal example to reproduce:

type Source struct {
	A int
	B int
}
type TargetBase struct {
	A int
}
type Target struct {
	Base TargetBase
	B    int
}

// goverter:converter
// goverter:output:file ./gen.go
type Converter interface {
	Source2TargetBase(s *Source) *TargetBase
	// goverter:map . Base
	Source2Target(s *Source) *Target
}

Generates the following code, which, as can be seen, even ignores the already defined Source2Target in favor of a generated helper with stripped pointer on the source:

type ConverterImpl struct{}

func (c *ConverterImpl) Source2Target(source *Source) *Target {
	var pInternalTarget *Target
	if source != nil {
		var internalTarget Target
		internalTarget.Base = c.internalSourceToInternalTargetBase((*source))
		internalTarget.B = (*source).B
		pInternalTarget = &internalTarget
	}
	return pInternalTarget
}
func (c *ConverterImpl) Source2TargetBase(source *Source) *TargetBase {
	var pInternalTargetBase *TargetBase
	if source != nil {
		internalTargetBase := c.internalSourceToInternalTargetBase((*source))
		pInternalTargetBase = &internalTargetBase
	}
	return pInternalTargetBase
}
func (c *ConverterImpl) internalSourceToInternalTargetBase(source Source) TargetBase {
	var internalTargetBase TargetBase
	internalTargetBase.A = source.A
	return internalTargetBase
}

Expected behavior

Preserve the exact same source type when auto-generating helpers while using map .. OR at least prefer an already defined method with pointer receiver instead of generating a new one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions