jmattheis / goverter

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

bug: generating in the same folder results in an invalid import #88

Closed marmiha closed 11 months ago

marmiha commented 11 months ago

Describe the bug Generating the converter in the same folder as the interface extends results in an invalid package name "commandlinearguments" when used in pair with local converter functions extends functionality.

To Reproduce

Run go generate on this example:

package converter

type (
    Input struct {
        Value int32
    }

    Output struct {
        Value int
    }
)

func IntToInt32(value int32) int {
    return int(value)
}

//go:generate goverter --packageName=converter --packagePath=. --output=./converter.gen.go ./converter.go

// goverter:converter
// goverter:extend IntToInt32
type Converter interface {
    Convert(Input) Output
}

Which produces converter.gen.go with invalid imports:


// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.

package converter

import commandlinearguments "command-line-arguments"

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source commandlinearguments.Input) commandlinearguments.Output {
    var converterOutput commandlinearguments.Output
    converterOutput.Value = commandlinearguments.IntToInt32(source.Value)
    return converterOutput
}

Expected behavior

It should detect that the conversion methods are in the same package and thus does not import anyhting when accessing the extended methods.

I have also tried this with different combinations of cmd line arguments but nothing worked.

marmiha commented 11 months ago

Wrote too quick, duplicate of #35. For anyone else write the full path like this:

//go:generate goverter --packageName=converter  --output=./converter.gen.go --packagePath=github.com/converter github.com/converter