start example/test for components access

This commit is contained in:
Michael McLoughlin
2018-12-17 20:52:26 -08:00
parent 8282c9b17e
commit 34a3ddefcc
10 changed files with 160 additions and 12 deletions

View File

@@ -6,9 +6,11 @@ import (
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/reg"
"golang.org/x/tools/go/packages"
)
type Context struct {
pkg *packages.Package
file *avo.File
function *avo.Function
errs []error
@@ -22,6 +24,25 @@ func NewContext() *Context {
}
}
func (c *Context) Package(path string) {
cfg := &packages.Config{
Mode: packages.LoadTypes,
}
pkgs, err := packages.Load(cfg, path)
if err != nil {
c.AddError(err)
return
}
pkg := pkgs[0]
if len(pkg.Errors) > 0 {
for _, err := range pkg.Errors {
c.AddError(err)
}
return
}
c.pkg = pkg
}
func (c *Context) Function(name string) {
c.function = avo.NewFunction(name)
c.file.Functions = append(c.file.Functions, c.function)
@@ -32,7 +53,7 @@ func (c *Context) Signature(s *gotypes.Signature) {
}
func (c *Context) SignatureExpr(expr string) {
s, err := gotypes.ParseSignature(expr)
s, err := gotypes.ParseSignatureInPackage(c.pkg.Types, expr)
if err != nil {
c.AddError(err)
return

View File

@@ -14,6 +14,8 @@ import (
// ctx provides a global build context.
var ctx = NewContext()
func Package(path string) { ctx.Package(path) }
func TEXT(name, signature string) {
ctx.Function(name)
ctx.SignatureExpr(signature)