github.com/goplus/gossa@v0.3.25/importer.go (about)

     1  package gossa
     2  
     3  import (
     4  	"go/types"
     5  )
     6  
     7  type Importer struct {
     8  	loader Loader
     9  	pkgs   map[string]*types.Package
    10  	impl   types.Importer
    11  }
    12  
    13  func NewImporter(loader Loader, importer types.Importer) *Importer {
    14  	return &Importer{
    15  		loader: loader,
    16  		pkgs:   make(map[string]*types.Package),
    17  		impl:   importer,
    18  	}
    19  }
    20  
    21  func (i *Importer) Import(path string) (*types.Package, error) {
    22  	if pkg, ok := i.pkgs[path]; ok {
    23  		return pkg, nil
    24  	}
    25  	if pkg, err := i.loader.Import(path); err == nil {
    26  		i.pkgs[path] = pkg
    27  		return pkg, nil
    28  	}
    29  	if i.impl == nil {
    30  		return nil, ErrNotFoundImporter
    31  	}
    32  	pkg, err := i.impl.Import(path)
    33  	if err == nil {
    34  		i.pkgs[path] = pkg
    35  	}
    36  	return pkg, err
    37  }