github.com/ipld/go-ipld-prime@v0.21.0/schema/gen/go/externUtil.go (about)

     1  package gengo
     2  
     3  import (
     4  	"go/ast"
     5  	"go/parser"
     6  	"go/token"
     7  	path "path/filepath"
     8  	"strings"
     9  )
    10  
    11  // getExternTypes provides a mapping of all types defined in the destination package.
    12  // It is used by generate to not duplicate defined types to allow overriding of types.
    13  func getExternTypes(pth string) (map[string]struct{}, error) {
    14  	set := token.NewFileSet()
    15  	packs, err := parser.ParseDir(set, pth, nil, 0)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	types := make(map[string]struct{})
    21  	for _, pack := range packs {
    22  		for fname, f := range pack.Files {
    23  			if strings.HasPrefix(path.Base(fname), "ipldsch_") {
    24  				continue
    25  			}
    26  			for _, d := range f.Decls {
    27  				if t, isType := d.(*ast.GenDecl); isType {
    28  					if t.Tok == token.TYPE {
    29  						for _, s := range t.Specs {
    30  							ts := s.(*ast.TypeSpec)
    31  							types[ts.Name.Name] = struct{}{}
    32  						}
    33  					}
    34  				}
    35  			}
    36  		}
    37  	}
    38  
    39  	return types, nil
    40  }