Skip to content

Commit

Permalink
add importer case
Browse files Browse the repository at this point in the history
  • Loading branch information
dengsgo committed Aug 27, 2023
1 parent 1c2127d commit 84be448
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
7 changes: 4 additions & 3 deletions cmd/decorator/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"errors"
"github.com/dengsgo/go-decorator/cmd/logs"
"go/ast"
"go/parser"
"go/printer"
Expand All @@ -13,6 +12,8 @@ import (
"path/filepath"
"runtime"
"strings"

"github.com/dengsgo/go-decorator/cmd/logs"
)

const msgDecorPkgNotImported = "decorator used but package not imported (need add `import _ \"" + decoratorPackagePath + "\"`)"
Expand Down Expand Up @@ -117,8 +118,8 @@ func compile(args []string) error {
logs.Error(msgDecorPkgNotImported, "\n\t",
friendlyIDEPosition(fset, doc.Pos()))
} else if name == "_" {
imp.pathObjMap[decoratorPackagePath].Name = nil
imp.pathMap[decoratorPackagePath] = "decor"
imp.pathObjMap[decoratorPackagePath].Name = nil // rewrite this package import way
imp.pathMap[decoratorPackagePath] = "decor" // mark finished
}

if x := decorX(decorName); x != "" {
Expand Down
35 changes: 28 additions & 7 deletions cmd/decorator/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"bytes"
"github.com/dengsgo/go-decorator/cmd/logs"
"go/ast"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

"github.com/dengsgo/go-decorator/cmd/logs"
)

const listFormat = `GO.LIST.EXPORT={{.Export}}
Expand Down Expand Up @@ -91,18 +92,38 @@ func newImporter(f *ast.File) *importer {
if ip == nil {
continue
}
name := ""
var name string
pkg, _ := strconv.Unquote(ip.Path.Value)
if ip.Name != nil && ip.Name.Name != "" && ip.Name.Name != "_" {
name = ip.Name.Name
extName := strings.TrimRight(
filepath.Base(pkg),
filepath.Ext(pkg),
)

if ip.Name == nil {
// import path/name // name form pkg
name = extName
} else {
name = filepath.Base(pkg)
switch ip.Name.Name {
case "":
// import path/name // name form pkg
name = extName
case "_":
// import _ path/name // name pkg, about to be replaced
name = extName
case ".":
// import . path/name // ""
name = extName
default:
// import yname path/name // yname from alias
name = ip.Name.Name
}
}

nameMap[name] = pkg
pathObjMap[pkg] = ip
pathMap[pkg] = func() string {
if ip.Name != nil && ip.Name.Name == "_" {
return "_"
if ip.Name != nil {
return ip.Name.Name
}
return name
}()
Expand Down
66 changes: 64 additions & 2 deletions cmd/decorator/transformer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package main

import "testing"
import (
"go/parser"
"go/token"
"testing"
)

const importWays = `
package main
import (
_ "github.com/dengsgo/go-decorator/decor"
"github.com/dengsgo/cmd/logs"
"gopkg.in/yaml.v3"
. "log"
o "os"
)
`

func TestGetGoModPath(t *testing.T) {
s := getGoModPath()
Expand All @@ -10,5 +25,52 @@ func TestGetGoModPath(t *testing.T) {
}

func TestImporter(t *testing.T) {
// TODO
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "main.go", []byte(importWays), parser.ParseComments)
if err != nil {
t.Fatal("parse importWays content error", err)
}
imp := newImporter(f)
cLen := 5
if len(imp.nameMap) != cLen || len(imp.pathMap) != cLen || len(imp.pathObjMap) != cLen {
t.Fatal("newImporter() error,Clen=5 but got", len(imp.nameMap), len(imp.pathMap), len(imp.pathObjMap))
}
cases := []struct {
name,
pkg string
}{
{"decor", "github.com/dengsgo/go-decorator/decor"},
{"logs", "github.com/dengsgo/cmd/logs"},
{"yaml", "gopkg.in/yaml.v3"},
{"log", "log"},
{"o", "os"},
}
for _, v := range cases {
pkg, ok := imp.importedName(v.name)
if !ok {
t.Fatal("importedName() error, name not found", v.name)
}
if pkg != v.pkg {
t.Fatalf("importedName() error, got %s, want %s\n", pkg, v.pkg)
}
}
cass := []struct {
pkg,
name string
}{
{"github.com/dengsgo/go-decorator/decor", "_"},
{"github.com/dengsgo/cmd/logs", "logs"},
{"gopkg.in/yaml.v3", "yaml"},
{"log", "."},
{"os", "o"},
}
for _, v := range cass {
name, ok := imp.importedPath(v.pkg)
if !ok {
t.Fatal("importedPath() error, pkg not found", v.name)
}
if name != v.name {
t.Fatalf("importedPath() error, got %s, want %s\n", name, v.name)
}
}
}

0 comments on commit 84be448

Please sign in to comment.