Bug in Code Generation for Non-Main Packages
Hello,
First of all, thank you for creating such a useful tool. After reading your blog, I was inspired to try it out. I have discovered an issue in the generated code that I would like to report.
Generation in package main (works as expected)
The following code works properly when the package is main:
package main
import "time"
//go:generate go run github.com/masaushi/accessory@latest -type=Sample -output=gen_$GOFILE
type Sample struct {
time time.Time `accessor:"getter,setter"`
}
In this case, the generated code is correct.
Generation in non-main package (issue occurs)
When the package is something other than main, like in the following example:
package foo
import "time"
//go:generate go run github.com/masaushi/accessory@latest -type=Sample -output=gen_$GOFILE
type Sample struct {
time time.Time `accessor:"getter,setter"`
}
The generated code looks like this:
// Code generated by accessory; DO NOT EDIT.
package foo
func (s *Sample) Time() Time {
if s == nil {
return Time{}
}
return s.time
}
func (s *Sample) SetTime(val Time) {
if s == nil {
return
}
s.time = val
}
In this generated code, the type Time is not properly imported, causing a build failure. This issue only occurs when the package is not main. As many larger projects split their code into multiple packages, it would be very helpful if this tool could handle such cases appropriately.
I would be grateful if you could look into this matter.
Thank you very much for your time and consideration.