代码优化及错误返回改进 (#1969)

* Makefile: 使用 '?=' 替代 if-else 的判断

* 移除多余的类型转换

* 移除多余的nil判断

* map 使用简化以及错误处理优化
This commit is contained in:
Feng.YJ 2024-12-22 00:13:22 +08:00 committed by GitHub
parent 532d282509
commit 601aa6e245
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 37 additions and 38 deletions

View File

@ -13,16 +13,9 @@ CONFIG_FILE = config.yaml
IMAGE_NAME = gva IMAGE_NAME = gva
#镜像地址 #镜像地址
REPOSITORY = registry.cn-hangzhou.aliyuncs.com/${IMAGE_NAME} REPOSITORY = registry.cn-hangzhou.aliyuncs.com/${IMAGE_NAME}
#镜像版本
ifeq ($(TAGS_OPT),) TAGS_OPT ?= latest
TAGS_OPT = latest PLUGIN ?= email
else
endif
ifeq ($(PLUGIN),)
PLUGIN = email
else
endif
#容器环境前后端共同打包 #容器环境前后端共同打包
build: build-web build-server build: build-web build-server

View File

@ -3,6 +3,12 @@ package system
import ( import (
"context" "context"
"fmt" "fmt"
"go/token"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/global"
common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request" common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
model "github.com/flipped-aurora/gin-vue-admin/server/model/system" 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"
"github.com/flipped-aurora/gin-vue-admin/server/utils/ast" "github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
"github.com/pkg/errors" "github.com/pkg/errors"
"go/token"
"gorm.io/gorm" "gorm.io/gorm"
"os"
"path/filepath"
"strings"
"text/template"
) )
var AutoCodePackage = new(autoCodePackage) var AutoCodePackage = new(autoCodePackage)
@ -159,7 +160,7 @@ func (s *autoCodePackage) All(ctx context.Context) (entities []model.SysAutoCode
//dir目录需要包含所有的dirNameMap //dir目录需要包含所有的dirNameMap
for k := 0; k < len(dir); k++ { for k := 0; k < len(dir); k++ {
if dir[k].IsDir() { if dir[k].IsDir() {
if _, ok := dirNameMap[dir[k].Name()]; ok { if ok := dirNameMap[dir[k].Name()]; ok {
delete(dirNameMap, dir[k].Name()) delete(dirNameMap, dir[k].Name())
} }
} }

View File

@ -4,20 +4,21 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/global"
model "github.com/flipped-aurora/gin-vue-admin/server/model/system" model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request" "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
utilsAst "github.com/flipped-aurora/gin-vue-admin/server/utils/ast" utilsAst "github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
"github.com/pkg/errors" "github.com/pkg/errors"
"go/ast"
"go/format"
"go/parser"
"go/token"
"gorm.io/gorm" "gorm.io/gorm"
"os"
"path/filepath"
"strings"
"text/template"
) )
var AutoCodeTemplate = new(autoCodeTemplate) var AutoCodeTemplate = new(autoCodeTemplate)
@ -191,13 +192,12 @@ func (s *autoCodeTemplate) Preview(ctx context.Context, info request.AutoCode) (
return nil, errors.Wrap(err, "查询包失败!") return nil, errors.Wrap(err, "查询包失败!")
} }
// 增加判断: 重复创建struct 或者重复的简称 // 增加判断: 重复创建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("已经创建过此数据结构或重复简称,请勿重复创建!") return nil, errors.New("已经创建过此数据结构或重复简称,请勿重复创建!")
} }
codes := make(map[string]strings.Builder)
preview := make(map[string]string) preview := make(map[string]string)
codes, _, _, err = s.generate(ctx, info, entity) codes, _, _, err := s.generate(ctx, info, entity)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -291,8 +291,7 @@ func (s *autoCodeTemplate) AddFunc(info request.AutoFunc) error {
if err != nil { if err != nil {
return err return err
} }
err = s.addTemplateToAst("router", info) return s.addTemplateToAst("router", info)
return nil
} }
func (s *autoCodeTemplate) GetApiAndServer(info request.AutoFunc) (map[string]string, error) { 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) src, err := os.ReadFile(tPath)
if err != nil {
return err
}
fileSet := token.NewFileSet() fileSet := token.NewFileSet()
astFile, err := parser.ParseFile(fileSet, "", src, 0) astFile, err := parser.ParseFile(fileSet, "", src, 0)
if err != nil { if err != nil {
fmt.Println(err) return err
} }
funcDecl := utilsAst.FindFunction(astFile, funcName) funcDecl := utilsAst.FindFunction(astFile, funcName)
stmtNode := utilsAst.CreateStmt(stmtStr) stmtNode := utilsAst.CreateStmt(stmtStr)

View File

@ -3,12 +3,13 @@ package system
import ( import (
"errors" "errors"
"fmt" "fmt"
"strings"
"github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/request" "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
"github.com/flipped-aurora/gin-vue-admin/server/model/system" "github.com/flipped-aurora/gin-vue-admin/server/model/system"
systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response" systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
"gorm.io/gorm" "gorm.io/gorm"
"strings"
) )
//@author: [piexlmax](https://github.com/piexlmax) //@author: [piexlmax](https://github.com/piexlmax)
@ -135,7 +136,7 @@ func (apiService *ApiService) IgnoreApi(ignoreApi system.SysIgnoreApi) (err erro
func (apiService *ApiService) EnterSyncApi(syncApis systemRes.SysSyncApis) (err error) { func (apiService *ApiService) EnterSyncApi(syncApis systemRes.SysSyncApis) (err error) {
return global.GVA_DB.Transaction(func(tx *gorm.DB) error { return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
var txErr error var txErr error
if syncApis.NewApis != nil && len(syncApis.NewApis) > 0 { if len(syncApis.NewApis) > 0 {
txErr = tx.Create(&syncApis.NewApis).Error txErr = tx.Create(&syncApis.NewApis).Error
if txErr != nil { if txErr != nil {
return txErr return txErr

View File

@ -4,6 +4,12 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"mime/multipart"
"net/url"
"strconv"
"strings"
"time"
"github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/request" "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
"github.com/flipped-aurora/gin-vue-admin/server/model/system" "github.com/flipped-aurora/gin-vue-admin/server/model/system"
@ -11,11 +17,6 @@ import (
"github.com/flipped-aurora/gin-vue-admin/server/utils" "github.com/flipped-aurora/gin-vue-admin/server/utils"
"github.com/xuri/excelize/v2" "github.com/xuri/excelize/v2"
"gorm.io/gorm" "gorm.io/gorm"
"mime/multipart"
"net/url"
"strconv"
"strings"
"time"
) )
type SysExportTemplateService struct { type SysExportTemplateService struct {
@ -155,7 +156,7 @@ func (sysExportTemplateService *SysExportTemplateService) ExportExcel(templateID
var tableTitle []string var tableTitle []string
var selectKeyFmt []string var selectKeyFmt []string
for _, key := range columns { for _, key := range columns {
selectKeyFmt = append(selectKeyFmt, fmt.Sprintf("%s", key)) selectKeyFmt = append(selectKeyFmt, key)
tableTitle = append(tableTitle, templateInfoMap[key]) tableTitle = append(tableTitle, templateInfoMap[key])
} }