map 使用简化以及错误处理优化
This commit is contained in:
parent
eeb8f08e19
commit
d82ad77bfe
|
@ -3,6 +3,12 @@ package system
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
|
||||
model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
||||
|
@ -10,12 +16,7 @@ import (
|
|||
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
|
||||
"github.com/pkg/errors"
|
||||
"go/token"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var AutoCodePackage = new(autoCodePackage)
|
||||
|
@ -159,7 +160,7 @@ func (s *autoCodePackage) All(ctx context.Context) (entities []model.SysAutoCode
|
|||
//dir目录需要包含所有的dirNameMap
|
||||
for k := 0; k < len(dir); k++ {
|
||||
if dir[k].IsDir() {
|
||||
if _, ok := dirNameMap[dir[k].Name()]; ok {
|
||||
if ok := dirNameMap[dir[k].Name()]; ok {
|
||||
delete(dirNameMap, dir[k].Name())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,20 +4,21 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
||||
utilsAst "github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
|
||||
"github.com/pkg/errors"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var AutoCodeTemplate = new(autoCodeTemplate)
|
||||
|
@ -191,13 +192,12 @@ func (s *autoCodeTemplate) Preview(ctx context.Context, info request.AutoCode) (
|
|||
return nil, errors.Wrap(err, "查询包失败!")
|
||||
}
|
||||
// 增加判断: 重复创建struct 或者重复的简称
|
||||
if AutocodeHistory.Repeat(info.BusinessDB, info.StructName, info.Abbreviation, info.Package) && !info.IsAdd {
|
||||
if AutocodeHistory.Repeat(info.BusinessDB, info.StructName, info.Abbreviation, info.Package) && !info.IsAdd {
|
||||
return nil, errors.New("已经创建过此数据结构或重复简称,请勿重复创建!")
|
||||
}
|
||||
|
||||
codes := make(map[string]strings.Builder)
|
||||
preview := make(map[string]string)
|
||||
codes, _, _, err = s.generate(ctx, info, entity)
|
||||
codes, _, _, err := s.generate(ctx, info, entity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -291,8 +291,7 @@ func (s *autoCodeTemplate) AddFunc(info request.AutoFunc) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = s.addTemplateToAst("router", info)
|
||||
return nil
|
||||
return s.addTemplateToAst("router", info)
|
||||
}
|
||||
|
||||
func (s *autoCodeTemplate) GetApiAndServer(info request.AutoFunc) (map[string]string, error) {
|
||||
|
@ -353,10 +352,14 @@ func (s *autoCodeTemplate) addTemplateToAst(t string, info request.AutoFunc) err
|
|||
}
|
||||
|
||||
src, err := os.ReadFile(tPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fileSet := token.NewFileSet()
|
||||
astFile, err := parser.ParseFile(fileSet, "", src, 0)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
funcDecl := utilsAst.FindFunction(astFile, funcName)
|
||||
stmtNode := utilsAst.CreateStmt(stmtStr)
|
||||
|
|
Loading…
Reference in New Issue