From ae8847565ee4e3b7068f6df31da9d792d3868806 Mon Sep 17 00:00:00 2001 From: yuanzhao <2206582181@qq.com> Date: Thu, 1 Jun 2023 14:56:53 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E8=AF=86=E5=88=AB=E5=88=B0=E4=B8=8D?= =?UTF-8?q?=E6=98=8E=E7=A1=AE=E7=9A=84import,=20=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E7=9B=AE=E5=BD=95=E5=92=8Cpackage=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E4=B8=8D=E4=B8=80=E8=87=B4=E6=97=B6=EF=BC=8C=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E6=89=8B=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console/commands/bean.go | 13 +++++++++---- parser/help_str.go | 11 +++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/console/commands/bean.go b/console/commands/bean.go index 51d9949..d7b73dd 100644 --- a/console/commands/bean.go +++ b/console/commands/bean.go @@ -86,8 +86,8 @@ func (BeanCommand) Execute(input command.Input) { for _, goType := range fileParser.Types { for _, attr := range goType.Attrs { if attr.HasTag("inject") { - for _, impStr := range fileParser.Imports { - bc.imports[impStr] = impStr + for alias, impStr := range fileParser.Imports { + bc.imports[impStr] = alias } break @@ -105,7 +105,8 @@ func (BeanCommand) Execute(input command.Input) { } type beanCache struct { - name string + name string + // path => alias imports map[string]string structList []parser.GoType } @@ -210,7 +211,11 @@ func getInitializeNewFunName(k parser.GoTypeAttr, m map[string]string) string { if !k.InPackage { a := m[k.TypeImport] - alias = a + "." + if a == "" { + panic("识别到不明确的import, 最后一个目录和package名称不一致时,需要手动, 例如\nredis \"github.com/go-redis/redis/v8\"") + } else { + alias = a + "." + } arr := strings.Split(k.TypeName, ".") name = arr[len(arr)-1] } else if name[0:1] == "*" { diff --git a/parser/help_str.go b/parser/help_str.go index 9d149a9..665f49f 100644 --- a/parser/help_str.go +++ b/parser/help_str.go @@ -457,10 +457,13 @@ func GenImportAlias(path, packageName string, m map[string]string) map[string]st keys = append(keys, s) } sort.Strings(keys) - for _, k := range keys { - imp := m[k] - temp := strings.Split(imp, "/") - key := temp[len(temp)-1] + for _, imp := range keys { + key := m[imp] + + temp := strings.Split(key, "/") + if len(temp) != 1 { + key = temp[len(temp)-1] + } if _, ok := aliasMapImport[key]; ok { for i := 1; i < 1000; i++ {