Skip to content

Commit

Permalink
feat(doc): readme文档更新
Browse files Browse the repository at this point in the history
  • Loading branch information
morehao committed Jul 21, 2024
1 parent 4ca7229 commit 2c9dd4a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 96 deletions.
118 changes: 26 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
# go-tools
`go-tools`是一个golang工具库,包含了一些个人在项目开发过程中总结的一些常用的工具函数和组件。
# go-tools简介
`go-tools`是一个golang工具组件库,包含了一些个人在项目开发过程中总结的一些常用的工具函数和组件。

组件列表:
- [codeGen](#codegen) 代码生成工具
- `conc` 简单的并发控制组件
- `conf` 配置文件读取组件
- `dbClient` 数据库组件
- [excel](#excel) 简单读写excel组件
- `gast` 语法树工具
- `gcontext` 上下文工具组件
- `gerror` 错误处理组件
- `glog` 日志组件
- `gutils` 一些常用的工具函数

# 安装
```bash
go get github.com/morehao/go-tools
```

## autoCode
# 组件使用说明

## codeGen

### 简介
`autoCode` 是一个简单的代码生成工具,通过读取数据库表结构,支持生成基础的CRUD代码,router、controller、service、dto、model、errorCode等代码。
`codeGen` 是一个简单的代码生成工具,通过读取数据库表结构,支持生成基础的CRUD代码,router、controller、service、dto、model、errorCode等代码。
### 特性
- 支持MySQL数据库
- 支持模板自定义和模板参数自定义
- 支持生成基础的CRUD代码

### 安装
```bash
go get github.com/morehao/go-tools
```
- 支持基于模板生成代码
### 使用
使用示例参照[autoCode单测](codeGen/auto_code_test.go)
使用示例参照[codeGen单测](codeGen/gen_test.go)

## excel

Expand All @@ -35,84 +49,4 @@ go get github.com/morehao/go-tools
go get github.com/morehao/go-tools
```
### 使用
### 读取Excel
读取Excel的简单示例:
```go
package main

import (
"fmt"
"github.com/morehao/go-tools"
"github.com/xuri/excelize/v2"
)

type DataItem struct {
SerialNumber int64 `ex:"head:序号" validate:"min=10,max=100"`
UserName string `ex:"head:姓名"`
Age int64 `ex:"head:年龄"`
}

func main() {
f, openErr := excelize.OpenFile("test.xlsx")
if openErr != nil {
fmt.Println("open file error: ", openErr)
return
}
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()

reader := excel.NewReader(f, &excel.ReaderOption{
SheetNumber: 0,
HeadRow: 0,
DataStartRow: 1,
})
var dataList []DataItem
validateErrMap, readerErr := reader.Read(&dataList)
if readerErr != nil {
fmt.Println("read error: ", readerErr)
return
}
if len(validateErrMap) > 0 {
fmt.Println("validate error: ", validateErrMap)
return
}
for _, item := range dataList {
fmt.Println(item)
}
}
```
### 写入Excel
生成Excel的简单示例:
```go
package main

import (
"fmt"
"github.com/morehao/go-tools"
)

type DataItem struct {
SerialNumber int64 `ex:"head:序号" validate:"min=10,max=100"`
UserName string `ex:"head:姓名"`
Age int64 `ex:"head:年龄"`
}

func main() {
var dataList []DataItem
dataList = append(dataList, DataItem{
SerialNumber: 1,
UserName: "张三",
Age: 18,
})
excelWriter := excel.NewWrite(&excel.WriteOption{
SheetName: "Sheet1",
HeadRow: 0,
})
if err := excelWriter.SaveAs(dataList, "write.xlsx"); err != nil {
fmt.Println("write error: ", err)
}
}
```
使用示例参照[excel使用说明](excel/README.md)
4 changes: 2 additions & 2 deletions gast/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func AddMethodToInterfaceInFile(file, interfaceName, receiverTypeName, methodNam
}

// AddContentToFunc 在指定函数的函数体内添加内容
func AddContentToFunc(content, functionName, functionFilepath string) error {
func AddContentToFunc(functionFilepath, functionName, content string) error {
// 解析整个文件
fileSet := token.NewFileSet()
node, parseErr := parser.ParseFile(fileSet, functionFilepath, nil, parser.AllErrors)
Expand Down Expand Up @@ -135,7 +135,7 @@ func AddContentToFunc(content, functionName, functionFilepath string) error {
}

// AddFunction 将指定的函数内容添加到指定文件中,如果文件不存在包声明则添加包声明
func AddFunction(content, functionFilepath, pkgName string) error {
func AddFunction(functionFilepath, content, pkgName string) error {
// 解析目标文件
fileSet := token.NewFileSet()
node, parseErr := parser.ParseFile(fileSet, functionFilepath, nil, parser.ParseComments)
Expand Down
4 changes: 2 additions & 2 deletions gast/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAddContentToFunc(t *testing.T) {
filePath := "./test.go"
content := `routerGroup.POST("test")`

err := AddContentToFunc(content, "platformRouter", filePath)
err := AddContentToFunc(filePath, "platformRouter", content)
assert.Nil(t, err)
}

Expand All @@ -26,7 +26,7 @@ func NewFunction() {
fmt.Println("Hello, World!")
}
`
err := AddFunction(content, "test.go", "gast")
err := AddFunction("test.go", content, "gast")
assert.Nil(t, err)
}

Expand Down

0 comments on commit 2c9dd4a

Please sign in to comment.