Skip to content

Commit

Permalink
refactor: 导表工具重构,增加部分特性,修复部分问题
Browse files Browse the repository at this point in the history
1、增加测试用例;
2、支持多文件合并导表;
3、支持 "#" 开头忽略;
4、修复越界问题;
5、优化模板样式,增加模板规则说明;
  • Loading branch information
kercylan98 committed Jul 1, 2023
1 parent 73cefc9 commit afdda79
Show file tree
Hide file tree
Showing 22 changed files with 267 additions and 312 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ func Init(loadDir string, loadHandle LoadHandle, refreshHandle RefreshHandle) {
cLoadDir = loadDir
cLoadHandle = loadHandle
cRefreshHandle = refreshHandle
Load()
Refresh()
}

// Load 加载配置
// - 加载后并不会刷新线上配置,需要执行 Refresh 函数对线上配置进行刷新
func Load() {
mutex.Lock()
if cTicker != nil {
Expand Down
14 changes: 0 additions & 14 deletions examples/simple-server-config/config/configs/config.define.go

This file was deleted.

47 changes: 0 additions & 47 deletions examples/simple-server-config/config/configs/config.go

This file was deleted.

4 changes: 0 additions & 4 deletions examples/simple-server-config/config/json/SystemConfig.json

This file was deleted.

10 changes: 0 additions & 10 deletions examples/simple-server-config/config/json/WelcomeConfig.json

This file was deleted.

Binary file not shown.
30 changes: 0 additions & 30 deletions examples/simple-server-config/main.go

This file was deleted.

74 changes: 69 additions & 5 deletions planner/configexport/configexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,52 @@ import (
"fmt"
"github.com/kercylan98/minotaur/planner/configexport/internal"
"github.com/kercylan98/minotaur/utils/file"
"github.com/kercylan98/minotaur/utils/log"
"github.com/kercylan98/minotaur/utils/str"
"github.com/tealeg/xlsx"
"go.uber.org/zap"
"os/exec"
"path/filepath"
"strings"
"text/template"
)

// New 创建一个导表配置
func New(xlsxPath string) *ConfigExport {
ce := &ConfigExport{xlsxPath: xlsxPath}
ce := &ConfigExport{xlsxPath: xlsxPath, exist: make(map[string]bool)}
xlsxFile, err := xlsx.OpenFile(xlsxPath)
if err != nil {
panic(err)
}
for i := 0; i < len(xlsxFile.Sheets); i++ {
if config, err := internal.NewConfig(xlsxFile.Sheets[i]); err != nil {
panic(err)
sheet := xlsxFile.Sheets[i]
if config, err := internal.NewConfig(sheet, ce.exist); err != nil {
switch err {
case internal.ErrReadConfigFailedSame:
log.Warn("ConfigExport",
zap.String("File", xlsxPath),
zap.String("Sheet", sheet.Name),
zap.String("Info", "A configuration with the same name exists, skipped"),
)
case internal.ErrReadConfigFailedIgnore:
log.Info("ConfigExport",
zap.String("File", xlsxPath),
zap.String("Sheet", sheet.Name),
zap.String("Info", "Excluded non-configuration table files"),
)
default:
log.ErrorHideStack("ConfigExport",
zap.String("File", xlsxPath),
zap.String("Sheet", sheet.Name),
zap.String("Info", "Excluded non-configuration table files"),
)
}
} else {
if config == nil {
continue
}
ce.configs = append(ce.configs, config)
ce.exist[config.Name] = true
}
}
return ce
Expand All @@ -31,6 +59,34 @@ func New(xlsxPath string) *ConfigExport {
type ConfigExport struct {
xlsxPath string
configs []*internal.Config
exist map[string]bool
}

// Merge 合并多个导表配置
func Merge(exports ...*ConfigExport) *ConfigExport {
if len(exports) == 0 {
return nil
}
if len(exports) == 1 {
return exports[0]
}
var export = exports[0]
for i := 1; i < len(exports); i++ {
ce := exports[i]
for _, config := range ce.configs {
if _, ok := export.exist[config.Name]; ok {
log.Warn("ConfigExport",
zap.String("File", ce.xlsxPath),
zap.String("Sheet", config.Name),
zap.String("Info", "A configuration with the same name exists, skipped"),
)
continue
}
export.configs = append(export.configs, config)
export.exist[config.Name] = true
}
}
return export
}

func (slf *ConfigExport) ExportClient(prefix, outputDir string) {
Expand Down Expand Up @@ -99,9 +155,13 @@ func (slf *ConfigExport) exportGoConfig(outputDir string) {
return nil
})

if err := file.WriterFile(filepath.Join(outputDir, "config.go"), []byte(result)); err != nil {
filePath := filepath.Join(outputDir, "config.go")
if err := file.WriterFile(filePath, []byte(result)); err != nil {
panic(err)
}

cmd := exec.Command("gofmt", "-w", filePath)
_ = cmd.Run()
}

func (slf *ConfigExport) exportGoDefine(outputDir string) {
Expand Down Expand Up @@ -136,7 +196,11 @@ func (slf *ConfigExport) exportGoDefine(outputDir string) {
return nil
})

if err := file.WriterFile(filepath.Join(outputDir, "config.define.go"), []byte(result)); err != nil {
filePath := filepath.Join(outputDir, "config.define.go")
if err := file.WriterFile(filePath, []byte(result)); err != nil {
panic(err)
}

cmd := exec.Command("gofmt", "-w", filePath)
_ = cmd.Run()
}
42 changes: 42 additions & 0 deletions planner/configexport/configexport_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package configexport_test

import (
"fmt"
"github.com/kercylan98/minotaur/config"
"github.com/kercylan98/minotaur/planner/configexport"
"github.com/kercylan98/minotaur/planner/configexport/example"
"os"
"path/filepath"
"strings"
)

func ExampleNew() {
var workdir = "./"
files, err := os.ReadDir(workdir)
if err != nil {
panic(err)
}
var ces []*configexport.ConfigExport
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(file.Name(), ".xlsx") || strings.HasPrefix(file.Name(), "~") {
continue
}

ces = append(ces, configexport.New(filepath.Join(workdir, file.Name())))
}

c := configexport.Merge(ces...)
outDir := filepath.Join(workdir, "example")
c.ExportGo("", outDir)
c.ExportServer("", outDir)
c.ExportClient("", outDir)

// 下方为配置加载代码
// 使用生成的 LoadConfig 函数加载配置
config.Init(outDir, example.LoadConfig, example.Refresh)

fmt.Println("success")

// Output:
// success
}
11 changes: 0 additions & 11 deletions planner/configexport/configexport_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"Id": 1,
"Award": {
"0": "asd",
"1": "12"
},
"Other": {
"1": {
"id": 2,
"name": "刘备"
},
"0": {
"id": 1,
"name": "张飞"
},
"1": {
"id": 2,
"name": "刘备"
}
},
"Id": 1,
"Count": "a",
"Award": {
"0": "asd",
"1": "12"
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"1": {
"b": {
"Id": 1,
"Count": "b",
"Award": {
"0": "asd",
"1": "12"
},
"Other": {
"0": {
"id": 1,
"name": "张飞"
"name": "张飞",
"id": 1
},
"1": {
"id": 2,
"name": "刘备"
"name": "刘备",
"id": 2
}
}
},
"Id": 1,
"Count": "b"
}
},
"2": {
Expand All @@ -27,12 +27,12 @@
},
"Other": {
"0": {
"id": 1,
"name": "张飞"
"name": "张飞",
"id": 1
},
"1": {
"name": "刘备",
"id": 2
"id": 2,
"name": "刘备"
}
},
"Id": 2,
Expand All @@ -42,8 +42,8 @@
"Id": 2,
"Count": "d",
"Award": {
"0": "asd",
"1": "12"
"1": "12",
"0": "asd"
},
"Other": {
"0": {
Expand Down
Loading

0 comments on commit afdda79

Please sign in to comment.