Skip to content

Commit

Permalink
日志组件和gin框架render组件 (#8)
Browse files Browse the repository at this point in the history
* fix(autoCode): 目录名称生成改为小驼峰

* feat(autoCode): 方法名调整,按照模块获取模板参数

* feat(autoCode): 支持接口维度自动生成代码

* feat(autoCode): 实例化方式调整

* feat(glog): 日志组件初始版本

* feat(glog): 日志工具ctx赋值去掉

* feat(utils): 数组去重方法

* feat(gcontext): render

* feat(gcontext): render中的错误断言调整

* feat(gcontext): render中常量定义

* feat(gcontext): 去掉render中的无用代码

* feat(glog): 结构调整,支持初始化指定日志组件类型

* feat(glog): zap日志组件支持扩展字段

* fear(glog): 字段命名调整

* feat(glog): 增加infow方法

* feat(glog): 日志组件单测
  • Loading branch information
morehao authored Jun 22, 2024
1 parent 6b52634 commit 39238c3
Show file tree
Hide file tree
Showing 42 changed files with 1,066 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ go.work
go.work.sum

.idea/
autoCode/tmpAutoCode/*
codeGen/tmpAutoCode/*
glog/log/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
go get github.com/morehao/go-tools
```
### 使用
使用示例参照[autoCode单测](./autoCode/auto_code_test.go)
使用示例参照[autoCode单测](codeGen/auto_code_test.go)

## excel

Expand Down
2 changes: 1 addition & 1 deletion autoCode/auto_code.go → codeGen/auto_code.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package autoCode
package codeGen

import (
"gorm.io/gorm"
Expand Down
2 changes: 1 addition & 1 deletion autoCode/auto_code_test.go → codeGen/auto_code_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package autoCode
package codeGen

import (
"fmt"
Expand Down
6 changes: 3 additions & 3 deletions autoCode/base.go → codeGen/base.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package autoCode
package codeGen

import (
"fmt"
"github.com/morehao/go-tools/utils"
"github.com/morehao/go-tools/gutils"
"gorm.io/gorm"
"strings"
)
Expand Down Expand Up @@ -76,7 +76,7 @@ func (impl *baseImpl) GetApiTemplateParam(cfg *ApiCfg) (*ApiTemplateParams, erro
LayerPrefix: tplItem.layerPrefix,
})
}
packagePascalName := utils.SnakeToPascal(cfg.PackageName)
packagePascalName := gutils.SnakeToPascal(cfg.PackageName)
res := &ApiTemplateParams{
PackageName: cfg.PackageName,
PackagePascalName: packagePascalName,
Expand Down
2 changes: 1 addition & 1 deletion autoCode/db.go → codeGen/db.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package autoCode
package codeGen

import (
"database/sql"
Expand Down
10 changes: 5 additions & 5 deletions autoCode/mysql.go → codeGen/mysql.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package autoCode
package codeGen

import (
"fmt"
"github.com/morehao/go-tools/utils"
"github.com/morehao/go-tools/gutils"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -59,8 +59,8 @@ func (impl *mysqlImpl) GetModuleTemplateParam(db *gorm.DB, cfg *ModuleCfg) (*Mod
ModelFields: modelFieldList,
})
}
packagePascalName := utils.SnakeToPascal(cfg.PackageName)
structName := utils.SnakeToPascal(cfg.TableName)
packagePascalName := gutils.SnakeToPascal(cfg.PackageName)
structName := gutils.SnakeToPascal(cfg.TableName)
res := &ModuleTemplateParams{
PackageName: cfg.PackageName,
PackagePascalName: packagePascalName,
Expand All @@ -84,7 +84,7 @@ func (impl *mysqlImpl) getModelField(db *gorm.DB, dbName string, cfg *ModuleCfg)
var modelFieldList []ModelField
for _, v := range entities {
item := ModelField{
FiledName: utils.SnakeToPascal(v.ColumnName),
FiledName: gutils.SnakeToPascal(v.ColumnName),
FieldType: columnTypeMap[v.DataType],
ColumnName: v.ColumnName,
ColumnType: v.DataType,
Expand Down
14 changes: 7 additions & 7 deletions autoCode/template.go → codeGen/template.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package autoCode
package codeGen

import (
"fmt"
"github.com/morehao/go-tools/utils"
"github.com/morehao/go-tools/gutils"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -61,7 +61,7 @@ func (t *tplCfg) BuildTargetDir(rootDir, packageName string) string {
if t.layerPrefix == "" {
return fmt.Sprintf("%s/%s", rootDir, t.layerName)
}
layerDirName := fmt.Sprintf("%s%s", t.layerPrefix, utils.SnakeToPascal(packageName))
layerDirName := fmt.Sprintf("%s%s", t.layerPrefix, gutils.SnakeToPascal(packageName))
return fmt.Sprintf("%s/%s/%s", rootDir, t.layerName, layerDirName)
}

Expand Down Expand Up @@ -122,7 +122,7 @@ func getTplFiles(path string) ([]tplFile, error) {
for _, name := range names {

// 判断是否是模板文件
if utils.GetFileSuffix(name) == tplFileSuffix {
if gutils.GetFileSuffix(name) == tplFileSuffix {
layerName := strings.TrimSuffix(name, fmt.Sprintf("%s%s", goFileSuffix, tplFileSuffix))
if specialName, ok := layerSpecialNameMap[layerName]; ok {
layerName = specialName
Expand Down Expand Up @@ -163,16 +163,16 @@ func buildTplCfg(tplFiles []tplFile, defaultFilename string) ([]tplCfg, error) {
}

func createFile(targetDir, targetFileName string, tpl *template.Template, tplParam interface{}) error {
if err := utils.CreateDir(targetDir); err != nil {
if err := gutils.CreateDir(targetDir); err != nil {
return err
}
codeFilepath := fmt.Sprintf("%s/%s", targetDir, targetFileName)
// 判断文件是否存在
if exist := utils.FileExists(codeFilepath); exist {
if exist := gutils.FileExists(codeFilepath); exist {
// 如果存在,先写入一个临时文件,再对既有文件进行追加
tempDir := fmt.Sprintf("%s/tmp", targetDir)
tmpFilepath := fmt.Sprintf("%s/%s", tempDir, targetFileName)
if err := utils.CreateDir(tempDir); err != nil {
if err := gutils.CreateDir(tempDir); err != nil {
return err
}
tempF, openTempErr := os.OpenFile(tmpFilepath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
Expand Down
2 changes: 1 addition & 1 deletion autoCode/template_test.go → codeGen/template_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package autoCode
package codeGen

import (
"fmt"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/morehao/go-tools/utils"
"github.com/morehao/go-tools/gutils"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -19,5 +19,5 @@ func TestLoadConfig(t *testing.T) {
}
var config Config
LoadConfig("", "config_example.yaml", &config)
fmt.Println(utils.ToJson(config))
fmt.Println(gutils.ToJson(config))
}
67 changes: 67 additions & 0 deletions gcore/ginRender/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ginRender

import (
"github.com/gin-gonic/gin"
"github.com/morehao/go-tools/gcore"
"github.com/morehao/go-tools/gerror"
"github.com/pkg/errors"
"net/http"
)

func RenderSuccess(ctx *gin.Context, data interface{}) {
r := gcore.NewResponseRender()
r.SetCode(0)
r.SetMsg("success")
r.SetData(data)
ctx.JSON(http.StatusOK, r)
return
}

func RenderSuccessWithFormat(ctx *gin.Context, data interface{}) {
r := gcore.NewResponseRender()
r.SetCode(0)
r.SetMsg("success")
r.SetDataWithFormat(data)
ctx.JSON(http.StatusOK, r)
return
}

func RenderFail(ctx *gin.Context, err error) {
r := gcore.NewResponseRender()

var code int
var msg string

var gErr gerror.Error
if errors.As(err, &gErr) {
code = gErr.Code
msg = gErr.Msg
} else {
code = -1
msg = errors.Cause(err).Error()
}

r.SetCode(code)
r.SetMsg(msg)
r.SetData(gin.H{})
ctx.JSON(http.StatusOK, r)
return
}

func RenderAbort(ctx *gin.Context, err error) {
r := gcore.NewResponseRender()

var gErr gerror.Error
if errors.As(err, &gErr) {
r.SetCode(gErr.Code)
r.SetMsg(gErr.Msg)
r.SetData(gin.H{})
} else {
r.SetCode(-1)
r.SetMsg(errors.Cause(err).Error())
r.SetData(gin.H{})
}
ctx.AbortWithStatusJSON(http.StatusOK, r)

return
}
34 changes: 34 additions & 0 deletions gcore/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gcore

// ResponseRender 返回数据格式化
type ResponseRender interface {
SetCode(int)
SetMsg(string)
SetData(interface{})
SetDataWithFormat(interface{})
}

func NewResponseRender() ResponseRender {
return &responseRender{}
}

type responseRender struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}

func (r *responseRender) SetCode(code int) {
r.Code = code
}
func (r *responseRender) SetMsg(msg string) {
r.Msg = msg
}
func (r *responseRender) SetData(data interface{}) {
r.Data = data
}

func (r *responseRender) SetDataWithFormat(data interface{}) {
ResponseFormat(data)
r.Data = data
}
Loading

0 comments on commit 39238c3

Please sign in to comment.