You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
When outputting into the same package it's quite tedious to having to specify the full package name with goverter:output:package. This is an albeit small friction point, but still feels like goverter should be able to figure this out for us instead of us having to input this.
I want to only need to specify goverter:output:file. The issue with this comes when you want to both output into the same package and reference some other functions.
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.//go:build !goverterpackage generated
import sample "goverter-test/pkg/sample"typeConverterImplstruct{}
func (c*ConverterImpl) ConvertFoo(source sample.Foo) sample.Bar {
varsampleBar sample.BarsampleBar.Moo=sample.Uppercase(source.Moo)
returnsampleBar
}
This fails to compile because the default package name is generated, and having two different package names in the same dir is not allowed:
$ go test ./pkg/samplefound packages generated (generated.go) and sample (sample.go) in /home/kallefagerberg/code/gh/goverter-test/pkg/sample
If you override just the package name with // goverter:output:package :sample then it correctly says package sample in the generated file, but it still fails to compile because it is still importing the sample package from the sample package itself.
$ go test ./pkg/samplepackage goverter-test/pkg/sample imports goverter-test/pkg/sample: import cycle not allowed
The only way to get around this today is to write the fully-qualified package name (as the docs suggest).
Describe the solution you'd like
It would be much nicer if the output package name defaulted to the package name that's already used in that directory (if any). If none exist, then you can default back to generated again.
This new behavior should apply if you leave goverter:output:package unset.
$ go run .2025/01/29 17:39:52 Package ID: goverter-test/pkg/sample2025/01/29 17:39:52 Package name: sample
(I named the module goverter-test with go mod init goverter-test).
Describe alternatives you've considered
An alternative could be to instead programmatically try get the module name (the goverter-test part), and then resolve what the full package name would be. This would mean it would be able to assume the full package name with this too:
// goverter:converter// goverter:output:package mygeneratedconvertertypeConverterinterface {
ConvertFoo(fooFoo) Bar
}
although I don't think it has any benefit if goverter is aware of the full package name when it's not using other files...
The text was updated successfully, but these errors were encountered:
By looking at goverter's code I see that you're already using the golang.org/x/tools/package package, which makes sense. Question would be how to incorporate it into this use case...?
Generally supporting this has some edge-cases that make this flaky to use. E.g. your code example only works if there is at least one .go file in the directory, otherwise the package ID won't contain the full path and there must be no compile errors. In the case where the output package is the same as the input package, it's guaranteed that there is a go file, so this is fine.
Can be implemented, it's probably a check at the end of config/converter.go#parseConverter and if the output:file is relative to ./ then we can automatically set the output:package.
Is your feature request related to a problem? Please describe.
When outputting into the same package it's quite tedious to having to specify the full package name with
goverter:output:package
. This is an albeit small friction point, but still feels like goverter should be able to figure this out for us instead of us having to input this.I want to only need to specify
goverter:output:file
. The issue with this comes when you want to both output into the same package and reference some other functions.For example, given this:
When generating it outputs this:
This fails to compile because the default package name is
generated
, and having two different package names in the same dir is not allowed:If you override just the package name with
// goverter:output:package :sample
then it correctly sayspackage sample
in the generated file, but it still fails to compile because it is still importing thesample
package from thesample
package itself.The only way to get around this today is to write the fully-qualified package name (as the docs suggest).
Describe the solution you'd like
It would be much nicer if the output package name defaulted to the package name that's already used in that directory (if any). If none exist, then you can default back to
generated
again.This new behavior should apply if you leave
goverter:output:package
unset.By looking around Go's parsing packages, I found this which could help: https://pkg.go.dev/golang.org/x/tools/go/packages#example-package
For example if I have the following repo:
And in the
main.go
I have the following code:Running it results in this:
(I named the module
goverter-test
withgo mod init goverter-test
).Describe alternatives you've considered
An alternative could be to instead programmatically try get the module name (the
goverter-test
part), and then resolve what the full package name would be. This would mean it would be able to assume the full package name with this too:although I don't think it has any benefit if goverter is aware of the full package name when it's not using other files...
The text was updated successfully, but these errors were encountered: