Skip to content

Issue generating code using similar stdlib package path #68

@myshkin5

Description

@myshkin5

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions