-
Notifications
You must be signed in to change notification settings - Fork 60
Closed
Description
Hi,
This might be a weird corner case or I might be missing something. I'm trying to generate code that uses types from stdlib that is also in a package with the same suffix as the code from the stdlib (example.com/dstissue/go/types
using go/types
in the example code below. What gets returned doesn't import the stdlib package and just assumes the types are in the output package.
Is there some feature that I'm missing that would facilitate this (import aliases?)? Can my example be tweaked to produce the expected results?
Here's my main.go
in the example.com/dstissue
module:
package main
import (
"go/token"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
"github.com/dave/dst/decorator/resolver/goast"
"github.com/dave/dst/decorator/resolver/gopackages"
)
func main() {
src := "package types"
fSet := token.NewFileSet()
file, err := decorator.NewDecoratorWithImports(
fSet, "example.com/dstissue/go/types", goast.New()).Parse(src)
if err != nil {
panic(err)
}
file.Decls = []dst.Decl{
&dst.GenDecl{
Tok: token.TYPE,
Specs: []dst.Spec{&dst.TypeSpec{
Name: dst.NewIdent("myFunc"),
Type: &dst.FuncType{
Results: &dst.FieldList{List: []*dst.Field{
{Type: &dst.Ident{Name: "Object", Path: "go/types"}},
}},
},
}},
},
}
restorer := decorator.NewRestorerWithImports("go/types", gopackages.New("go/types"))
if err = restorer.Print(file); err != nil {
panic(err)
}
}
And here's the output:
package types
type myFunc func() Object
I think this is what I want:
package types
import types2 "go/types"
type myFunc func() types2.Object
Thanks in advance!
Metadata
Metadata
Assignees
Labels
No labels