From cb1eaccfaf78cd6a914bb912150c2ce91e24b0f4 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Wed, 24 Mar 2021 14:12:07 +0800 Subject: [PATCH] =?UTF-8?q?#409=20bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 server/model/sys_auto_code.go server/service/sys_auto_code.go server/utils/file_operations.go --- server/model/sys_auto_code.go | 16 ++++++++-------- server/service/sys_auto_code.go | 5 +++++ server/utils/file_operations.go | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/server/model/sys_auto_code.go b/server/model/sys_auto_code.go index 78b77778..b8a3b524 100644 --- a/server/model/sys_auto_code.go +++ b/server/model/sys_auto_code.go @@ -4,14 +4,14 @@ import "errors" // 初始版本自动化代码工具 type AutoCodeStruct struct { - StructName string `json:"structName"` - TableName string `json:"tableName"` - PackageName string `json:"packageName"` - Abbreviation string `json:"abbreviation"` - Description string `json:"description"` - AutoCreateApiToSql bool `json:"autoCreateApiToSql"` - AutoMoveFile bool `json:"autoMoveFile"` - Fields []Field `json:"fields"` + StructName string `json:"structName"` + TableName string `json:"tableName"` + PackageName string `json:"packageName"` + Abbreviation string `json:"abbreviation"` + Description string `json:"description"` + AutoCreateApiToSql bool `json:"autoCreateApiToSql"` + AutoMoveFile bool `json:"autoMoveFile"` + Fields []*Field `json:"fields"` } type Field struct { diff --git a/server/service/sys_auto_code.go b/server/service/sys_auto_code.go index f8def744..835b02bd 100644 --- a/server/service/sys_auto_code.go +++ b/server/service/sys_auto_code.go @@ -320,6 +320,11 @@ func AutoCreateApi(a *model.AutoCodeStruct) (err error) { } func getNeedList(autoCode *model.AutoCodeStruct) (dataList []tplData, fileList []string, needMkdir []string, err error) { + // 去除所有空格 + utils.TrimSpace(autoCode) + for _, field := range autoCode.Fields { + utils.TrimSpace(field) + } // 获取 basePath 文件夹下所有tpl文件 tplFileList, err := GetAllTplFile(basePath, nil) if err != nil { diff --git a/server/utils/file_operations.go b/server/utils/file_operations.go index 04b04137..6963d9ec 100644 --- a/server/utils/file_operations.go +++ b/server/utils/file_operations.go @@ -3,6 +3,8 @@ package utils import ( "os" "path/filepath" + "reflect" + "strings" ) //@author: [songzhibin97](https://github.com/songzhibin97) @@ -39,3 +41,25 @@ Redirect: } return os.Rename(src, dst) } + +//@author: [songzhibin97](https://github.com/songzhibin97) +//@function: TrimSpace +//@description: 去除结构体空格 +//@param: target interface (target: 目标结构体,传入必须是指针类型) +//@return: err error + +func TrimSpace(target interface{}) { + t := reflect.TypeOf(target) + if t.Kind() != reflect.Ptr { + return + } + t = t.Elem() + v := reflect.ValueOf(target).Elem() + for i := 0; i < t.NumField(); i++ { + switch v.Field(i).Kind() { + case reflect.String: + v.Field(i).SetString(strings.TrimSpace(v.Field(i).String())) + } + } + return +}