jmattheis / goverter

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

Generated file doesn't contain generic type #66

Closed admannon closed 1 year ago

admannon commented 1 year ago

Describe the bug Generated file dosn't contain generic type.

To Reproduce

//go:generate goverter -ignoreUnexportedFields -packageName mapper -output mapper2.gen.go ./

// goverter:converter // goverter:extend github.com/gotidy/ptr:To.+ type Mapper interface { // goverter:autoMap Content MapTypedToAnother(Typed[InnerType]) ResultType[AnotherInnerType] }

type Typed[S any] struct { ID string Content *S }

type InnerType struct { Name string }

type AnotherInnerType struct { Name string }

type ResultType[S any] struct { ID string Name string Content *S }

- Currently (v0.17.0) this is actual behavior:
```golang
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.

package mapper

import (
    ptr "github.com/gotidy/ptr"
)

type MapperImpl struct{}

func (c *MapperImpl) MapTypedToAnother(source Typed) ResultType {
    var mapperResultType ResultType
    mapperResultType.ID = source.ID
    var pString *string
    if source.Content != nil {
        pString = &source.Content.Name
    }
    mapperResultType.Name = ptr.ToString(pString)
    mapperResultType.Content = c.pMapperInnerTypeToPMapperAnotherInnerType(source.Content)
    return mapperResultType
}
func (c *MapperImpl) pMapperInnerTypeToPMapperAnotherInnerType(source *InnerType) *AnotherInnerType {
    var pMapperAnotherInnerType *AnotherInnerType
    if source != nil {
        var mapperAnotherInnerType AnotherInnerType
        mapperAnotherInnerType.Name = (*source).Name
        pMapperAnotherInnerType = &mapperAnotherInnerType
    }
    return pMapperAnotherInnerType
}

Expected behavior

package mapper

import ( ptr "github.com/gotidy/ptr" )

type MapperImpl struct{}

func (c MapperImpl) MapTypedToAnother(source Typed[InnerType]) ResultType[AnotherInnerType] { var mapperResultType ResultType[AnotherInnerType] mapperResultType.ID = source.ID var pString string if source.Content != nil { pString = &source.Content.Name } mapperResultType.Name = ptr.ToString(pString) mapperResultType.Content = c.pMapperInnerTypeToPMapperAnotherInnerType(source.Content) return mapperResultType } func (c MapperImpl) pMapperInnerTypeToPMapperAnotherInnerType(source InnerType) AnotherInnerType { var pMapperAnotherInnerType AnotherInnerType if source != nil { var mapperAnotherInnerType AnotherInnerType mapperAnotherInnerType.Name = (*source).Name pMapperAnotherInnerType = &mapperAnotherInnerType } return pMapperAnotherInnerType }

jmattheis commented 1 year ago

Hey, thanks for your detailed bug report, I've released a fix with v0.17.1. Could you verify that it works for you?

admannon commented 1 year ago

I have tried it and it works fine. Thank you for the response.