importer

package module
v0.0.0-...-0441b4c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2016 License: MIT Imports: 7 Imported by: 0

README

importer

importer for go/types package

It makes go/types.Config.Check usable with imported packages that have not been installed yet (fixes https://github.com/golang/go/issues/14496)

Documentation

see http://godoc.org/github.com/metakeule/importer

Example

package main

import (
    "fmt"
    "github.com/metakeule/importer"
    "go/ast"
    "go/build"
    "go/token"
    "go/types"
    "os"
    "path/filepath"
)

func mkInfo() types.Info {
    return types.Info{
        Types: make(map[ast.Expr]types.TypeAndValue),
        Defs:  make(map[*ast.Ident]types.Object),
        Uses:  make(map[*ast.Ident]types.Object),
    }
}

func mkConfig() types.Config {
    return types.Config{
        IgnoreFuncBodies: true,
    }
}

func panicOnErr(err error) {
    if err != nil {
        panic(err.Error())
    }
}

func main() {
    var (
        pkgpath = "github.com/metakeule/importer"

        // assumes a single GOPATH directory
        srcDir = filepath.Join(os.Getenv("GOPATH"), "src", pkgpath)

        fset     = token.NewFileSet()
        astFiles []*ast.File
    )

    buildPkg, err := build.Import(pkgpath, srcDir, 0)
    panicOnErr(err)

    astFiles, err = importer.ParseAstFiles(fset, buildPkg.Dir, append(buildPkg.GoFiles, buildPkg.CgoFiles...))

    panicOnErr(err)

    info, config := mkInfo(), mkConfig()
    // here the importer is used with the same config and info settings
    config.Importer = importer.CheckImporter(mkInfo, mkConfig)
    _, err = (&config).Check(buildPkg.ImportPath, fset, astFiles, &info)

    panicOnErr(err)

    for ident, obj := range info.Defs {
        if ident.IsExported() {
            fmt.Printf("%s:\n    %s\n", ident.Name, obj.Type())
        }
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckImporter

func CheckImporter(infogen func() types.Info, configgen func() types.Config) types.ImporterFrom

CheckImporter creates an go/types.ImporterFrom that can be used with go/types.Config.Check to resolv imports that might not be installed (fixes https://github.com/golang/go/issues/14496) It uses the given infogen and configgen callbacks to generate the desired types.Info and types.Config that are used with config.Check which is called to return the types.Package.

func ParseAstFiles

func ParseAstFiles(fset *token.FileSet, dir string, files []string) (astFiles []*ast.File, err error)

ParseAstFiles is a shortcut to parse files from a directory into a set of ast.Files.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL