Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #82 from eonpatapon/pkg-alias
Browse files Browse the repository at this point in the history
Support import aliases
  • Loading branch information
grouville authored Aug 1, 2022
2 parents e98c200 + 52ad866 commit f653e26
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 21 deletions.
22 changes: 18 additions & 4 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type File struct {

// Definitions of the file
defs *parser.Definitions

// Package aliases of the file
// Maps alias to import path
importAliases map[string]string
}

// New create a File and analise CUE ast in it.
Expand All @@ -29,15 +33,25 @@ func New(path string) (*File, error) {
}

defs := parser.Definitions{}
parser.ParseDefs(&defs, content)
importAliases := make(map[string]string)
parser.ParseDefs(&defs, importAliases, content)

return &File{
path: path,
content: content,
defs: &defs,
path: path,
content: content,
defs: &defs,
importAliases: importAliases,
}, nil
}

// AliasImportPath returns the import path of some package alias.
func (f *File) AliasImportPath(alias string) (string, bool) {
if importPath, ok := f.importAliases[alias]; ok {
return importPath, true
}
return "", false
}

func (f *File) String() string {
return fmt.Sprintf("%s,%s", f.path, f.defs)
}
10 changes: 9 additions & 1 deletion parser/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"fmt"
"sort"
"strconv"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (def Definitions) Find(line int, column int) (string, error) {
// - Ident: those are definitions from the package itself
// - SelectorExpr: those are definitions from external package
// they will be stored as <pkg>.<def> (E.g., foo.#Bar)
func ParseDefs(defs *Definitions, f *ast.File) {
func ParseDefs(defs *Definitions, importAliases map[string]string, f *ast.File) {
ast.Walk(f, func(node ast.Node) bool {
switch v := node.(type) {
// case: #Def
Expand All @@ -75,7 +76,14 @@ func ParseDefs(defs *Definitions, f *ast.File) {
defs.AppendRange(definitionName, pkg.Pos(), v.Sel.End())
return false
}

case *ast.ImportSpec:
if v.Name != nil {
importPath, _ := strconv.Unquote(v.Path.Value)
importAliases[v.Name.String()] = importPath
}
}

return true
}, nil)
}
6 changes: 4 additions & 2 deletions parser/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func TestDefinitionParsing(t *testing.T) {
}

defs := Definitions{}
ParseDefs(&defs, f)
importAliases := make(map[string]string)
ParseDefs(&defs, importAliases, f)
output := defs.String()
for _, o := range tc.output {
require.Contains(t, output, o)
Expand Down Expand Up @@ -177,7 +178,8 @@ func TestFindDefinition(t *testing.T) {
}

defs := Definitions{}
ParseDefs(&defs, f)
importAliases := make(map[string]string)
ParseDefs(&defs, importAliases, f)

name, err := defs.Find(tc.input.line, tc.input.col)
if err != nil {
Expand Down
36 changes: 33 additions & 3 deletions plan/plan.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plan

import (
"errors"
"fmt"
"path/filepath"
"sync"
Expand Down Expand Up @@ -143,7 +144,9 @@ func (p *Plan) GetDefinition(path string, line, char int) (*loader.Value, error)
} else {
i, found := p.imports[def.Pkg()]
if !found {
return nil, fmt.Errorf("imported package %s not registered in plan", def.Def())
if i, err = p.findInstanceAlias(path, def.Pkg()); err != nil {
return nil, fmt.Errorf("imported package %s not registered in plan", def.Def())
}
}

return i.GetDefinition(def.Def())
Expand Down Expand Up @@ -186,13 +189,38 @@ func (p *Plan) GetInstance(path string, line, char int) (*loader.Instance, error
} else {
i, found := p.imports[def.Pkg()]
if !found {
return nil, fmt.Errorf("imported package %s not registered in plan", def.Def())
if i, err = p.findInstanceAlias(path, def.Pkg()); err != nil {
return nil, fmt.Errorf("imported package %s not registered in plan", def.Def())
}
}

return i, nil
}
}

// findInstanceAlias returns the instance of a package alias
func (p *Plan) findInstanceAlias(path string, pkgAlias string) (*loader.Instance, error) {
p.log.Debugf("Looking for pkg alias: %s", pkgAlias)

p.muFiles.RLock()
defer p.muFiles.RUnlock()

f, ok := p.files[path]
if !ok {
return nil, fmt.Errorf("file not registered")
}

if importPath, ok := f.AliasImportPath(pkgAlias); ok {
for _, i := range p.imports {
if i.ImportPath == importPath {
return i, nil
}
}
return nil, fmt.Errorf("no instance found for %s", importPath)
}
return nil, errors.New("not an alias")
}

func (p *Plan) findDefInFile(path string, line, char int) (*internal.Definition, error) {
p.log.Debugf("Looking for file: %s", path)

Expand Down Expand Up @@ -243,7 +271,9 @@ func (p *Plan) GetDoc(path string, line, char int) (*loader.Value, error) {
} else {
i, found := p.imports[_def.Pkg()]
if !found {
return nil, fmt.Errorf("imported package %s not registered in plan", _def.Def())
if i, err = p.findInstanceAlias(path, _def.Pkg()); err != nil {
return nil, fmt.Errorf("imported package %s not registered in plan", _def.Def())
}
}

return i.GetValue()
Expand Down
22 changes: 12 additions & 10 deletions plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func TestNew(t *testing.T) {
RootFilePath: "main.cue",
Kind: File,
imports: map[string]*loader.Instance{
"test": nil,
"test": nil,
"test2": nil,
},
},
},
Expand Down Expand Up @@ -176,22 +177,22 @@ func TestPlan_GetDefinition(t *testing.T) {
defs: []Def{
{
path: "_#TestName",
line: 7,
line: 8,
char: 1,
},
{
path: "#Test",
line: 9,
line: 10,
char: 9,
},
{
path: "_#TestName",
line: 15,
line: 16,
char: 12,
},
{
path: "#Test",
line: 14,
line: 15,
char: 15,
},
},
Expand Down Expand Up @@ -365,19 +366,19 @@ func TestPlan_GetInstance(t *testing.T) {
file: "main.cue",
defs: []Def{
{
line: 7,
line: 8,
char: 1,
},
{
line: 9,
line: 10,
char: 9,
},
{
line: 15,
line: 16,
char: 12,
},
{
line: 14,
line: 15,
char: 15,
},
},
Expand Down Expand Up @@ -500,7 +501,8 @@ func TestPlan_Reload(t *testing.T) {
RootFilePath: "main.cue",
Kind: File,
imports: map[string]*loader.Instance{
"test": nil,
"test": nil,
"test2": nil,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test2

#Test: {
name: string

assert: string
}
3 changes: 2 additions & 1 deletion plan/testdata/with-cue-mod/main.cue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"test.com/test"
t "test.com/test2"
)

_#TestName: =~"test"
Expand All @@ -11,7 +12,7 @@ test1: test.#Test & {
assert: "it's the first test"
}

test2: test.#Test & {
test2: t.#Test & {
name: _#TestName & "test 2"
assert: "it's the second test"
}

0 comments on commit f653e26

Please sign in to comment.