Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

missing import black #44

Closed
ChildWangWorld opened this issue May 2, 2020 · 3 comments
Closed

missing import black #44

ChildWangWorld opened this issue May 2, 2020 · 3 comments

Comments

@ChildWangWorld
Copy link

package main

import (
	"github.com/dave/dst/decorator"
	"github.com/dave/dst/decorator/resolver/goast"
	"github.com/dave/dst/decorator/resolver/guess"
	"go/token"
)

func main() {
	code := `package main

import (
	"github.com/opentracing/opentracing-go"
)

var a opentracing.Tracer
`

	dec := decorator.NewDecoratorWithImports(token.NewFileSet(), "main", goast.New())

	f, err := dec.Parse(code)
	if err != nil {
		panic(err)
	}
	res := decorator.NewRestorerWithImports("main", guess.New())
	if err := res.Print(f); err != nil {
		panic(err)
	}
}

get:

package main

var a opentracing.Tracer

expect:

package main

import (
	"github.com/opentracing/opentracing-go"
)

var a opentracing.Tracer
@dave
Copy link
Owner

dave commented May 2, 2020

Unfortunately with the simple resolvers in the guess and goast packages, there's no way to determine that the name of the github.com/opentracing/opentracing-go package is opentracing.

The guess resolver takes a guess by using the part of the path after the last slash. This fails for many package paths.

You have two options:

  1. You can supply a hardcoded mapping that tells the resolver what the package names are (example shown below).

  2. Alternatively you can use the more fully featured gotypes and gopackages resolvers.

There's a Go2 proposal that would fix all this complexity here. Feel free to give it a thunbs-up!

package main

import (
	"github.com/dave/dst/decorator"
	"github.com/dave/dst/decorator/resolver/goast"
	"github.com/dave/dst/decorator/resolver/guess"
	"go/token"
)

func main() {
	code := `package main

import (
	"github.com/opentracing/opentracing-go"
)

var a opentracing.Tracer
`
	packageNames := map[string]string{"github.com/opentracing/opentracing-go" : "opentracing"}
	packageNameResolver := guess.WithMap(packageNames)
	decoratorResolver := goast.WithResolver(packageNameResolver)

	dec := decorator.NewDecoratorWithImports(token.NewFileSet(), "main", decoratorResolver)

	f, err := dec.Parse(code)
	if err != nil {
		panic(err)
	}
	
	res := decorator.NewRestorerWithImports("main", packageNameResolver)
	if err := res.Print(f); err != nil {
		panic(err)
	}
}

Output:

package main

import (
	"github.com/opentracing/opentracing-go"
)

var a opentracing.Tracer

@dave
Copy link
Owner

dave commented May 2, 2020

One more possible solution... I assumed you need dst to manage the import block automatically, but if this is not the case, then you can just remove the resolvers entirely and it won't modify the import block at all:

package main

import (
	"github.com/dave/dst/decorator"
	"go/token"
)

func main() {
	code := `package main

import (
	"github.com/opentracing/opentracing-go"
)

var a opentracing.Tracer
`
	dec := decorator.NewDecorator(token.NewFileSet())

	f, err := dec.Parse(code)
	if err != nil {
		panic(err)
	}
	
	res := decorator.NewRestorer()
	if err := res.Print(f); err != nil {
		panic(err)
	}
}

Output:

package main

import (
	"github.com/opentracing/opentracing-go"
)

var a opentracing.Tracer

@ChildWangWorld
Copy link
Author

thx 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants