Skip to content

Commit

Permalink
feat(ax): 添加xlsx转换支持和配置生成
Browse files Browse the repository at this point in the history
新增从xlsx文件生成Go配置代码和JSON数据文件的功能。实现xlsx到Go的结构体定义及数据转换,支持客户端、服务端和双端导出模式。同时提供xlsx模板文件的导出功能。
  • Loading branch information
kercylan98 committed Aug 16, 2024
1 parent 6b82d33 commit 818b80c
Show file tree
Hide file tree
Showing 28 changed files with 897 additions and 130 deletions.
22 changes: 18 additions & 4 deletions ax/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"
)

var stack bool

var rootCmd = &cobra.Command{
Use: "minotaur-ax",
Short: "Minotaur ax is a CLI (Command Line Interface) tool designed to facilitate the rapid development of Minotaur projects.",
Long: "Minotaur ax is an essential command line tool aimed at enhancing and accelerating the development process of projects using the Minotaur framework.",
}

func Execute() {
defer func() {
if err := recover(); err != nil {
checkError(err)
}
}()
if err := rootCmd.Execute(); err != nil {
panic(err)
fmt.Println(err)
os.Exit(1)
}
}

func init() {

rootCmd.PersistentFlags().BoolVar(&stack, "stack", false, "show stack trace")
}

func checkError(err error) {
func checkError(err any) {
if err != nil {
panic(err)
if stack {
panic(err)
}
fmt.Println(err)
os.Exit(1)
}
}
72 changes: 70 additions & 2 deletions ax/cmd/table/configs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package table

import (
"fmt"
"github.com/kercylan98/minotaur/toolkit"
"github.com/kercylan98/minotaur/toolkit/charproc"
"github.com/kercylan98/minotaur/toolkit/collection"
"sort"
Expand All @@ -12,6 +14,7 @@ func GenerateConfigs(tables []Table, typeParser TypeParser, codeParser CodeParse
typeParser: typeParser,
codeParser: codeParser,
dataParser: dataParser,
data: make(map[string]map[string]any),
}).gen()
}

Expand All @@ -21,16 +24,20 @@ type Configs struct {
dataParser DataParser
tables []Table
config []*Config
data map[string]map[string]any
}

func (cs *Configs) GenerateCode() []byte {
return []byte(cs.codeParser.Parse(cs.config))
}

func (cs *Configs) GenerateData() map[string]map[string]any {
return cs.data
}

//goland:noinspection t
func (cs *Configs) gen() *Configs {
for _, table := range cs.tables {

config := &Config{
name: table.GetName(),
desc: table.GetDescribe(),
Expand All @@ -43,6 +50,7 @@ func (cs *Configs) gen() *Configs {
typ Type
}
var indexFields []indexField
var fields []Field

// 结构体解析
configStruct := &StructType{Name: table.GetName()}
Expand All @@ -52,6 +60,10 @@ func (cs *Configs) gen() *Configs {
if field == nil {
break
}
if field.IsIgnore() {
continue
}
fields = append(fields, field)

config.fieldDesc[field.GetName()] = field.GetDesc()

Expand Down Expand Up @@ -85,10 +97,66 @@ func (cs *Configs) gen() *Configs {
}
}
}

// 数据解析
var tableData = make(map[string]any)
var rowNo int
scan:
for {
var indexes []Field
var indexValue = make(map[string]map[string]any)
var row = make(map[string]any)
scanRow:
for _, field := range fields {
val, skip, end := field.Query(rowNo)
if end {
break scan
}
if skip {
rowNo++
continue scanRow
}
toolkit.MarshalToTargetWithJSON(val, &row)
if field.GetIndex() > 0 {
indexes = append(indexes, field)
indexValue[field.GetName()] = val
}
}

if len(indexes) == 0 {
tableData = row
}

// index 处理
sort.Slice(indexes, func(i, j int) bool {
return indexes[i].GetIndex() >= indexes[j].GetIndex()
})
for i, index := range indexes {
if i != len(indexes)-1 {
row = map[string]any{
fmt.Sprint(indexValue[index.GetName()][index.GetName()]): row,
}
} else {
topKey := fmt.Sprint(indexValue[index.GetName()][index.GetName()])
children, exist := tableData[topKey].(map[string]any)
if !exist {
children = make(map[string]any)
tableData[topKey] = children
}
for k, v := range row {
children[k] = v
}
}
}

rowNo++
}

cs.data[table.GetName()] = tableData

sort.Slice(indexFields, func(i, j int) bool {
return indexFields[i].idx < indexFields[j].idx // 升序
})

for _, f := range indexFields {
config.indexes = append(config.indexes, f.typ)
}
Expand Down
7 changes: 5 additions & 2 deletions ax/cmd/table/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type Field interface {
// GetParam 字段参数
GetParam() string

// GetData 字段数据
GetData() FieldDataScanner
// IsIgnore 是否忽略
IsIgnore() bool

// Query 数据查询
Query(pos int) (val map[string]any, skip, end bool)
}
6 changes: 0 additions & 6 deletions ax/cmd/table/field_data_scanner.go

This file was deleted.

7 changes: 5 additions & 2 deletions ax/cmd/table/fieldparser/lexer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fieldparser

import "unicode"
import (
"fmt"
"unicode"
)

type lexer struct {
input string
Expand Down Expand Up @@ -54,6 +57,6 @@ func (l *lexer) lex() token {
}
return token{Type: TokenIdent, Value: l.input[start:l.pos]}
}
panic(fmt.Errorf("%s unexpected character: %q", l.input, ch))
}
panic("invalid character")
}
3 changes: 3 additions & 0 deletions ax/cmd/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ type Table interface {

// GetFields 获取配置结构的字段
GetFields() FieldScanner

// IsIgnore 是否忽略该表
IsIgnore() bool
}
27 changes: 10 additions & 17 deletions ax/cmd/table/type2go/template.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by Minotaur-AX CLI v0.6.1. DO NOT EDIT.

package {{ .PackageName }}
package {{.PackageName}}

import (
"sync"
Expand Down Expand Up @@ -50,25 +50,18 @@ func RefreshAllConfig() (err error) {
lock.Lock()
defer lock.Unlock()

var rollbacks []func()

// check nil
{{- range .Vars}}
if ready{{.Name}} == nil {
err = errors.New("ready{{.Name}} is nil")
if _{{.Name}} == nil {
err = errors.New("_{{.Name}} is nil")
return
}
backup := _{{.Name}}
rollbacks = append(rollbacks, func() {
_{{.Name}} = backup
})
_{{.Name}} = ready{{.Name}}
{{- end}}

if err != nil {
for _, rollback := range rollbacks {
rollback()
}
}
// replace
{{- range .Vars}}
_{{.Name}} = ready{{.Name}}
{{- end}}

return err
}
Expand Down Expand Up @@ -107,8 +100,8 @@ func SetConfigReadyWithSign(sign string, config any) error {
return fmt.Errorf("invalid config type, expect {{.Type}}, got %T", config)
}
ready{{.Name}} = v
return nil
{{- end}}
return nil
default:
return errors.New("invalid config sign")
}
Expand Down Expand Up @@ -175,7 +168,7 @@ func GetConfigWithSignCopy(sign string) (any, error) {
bytes, _ := json.Marshal(_{{.Name}})
_ = json.Unmarshal(bytes, &copy)
{{- else}}
copy := new({{.Type}})
copy := new({{.ValueType}})
bytes, _ := json.Marshal(_{{.Name}})
_ = json.Unmarshal(bytes, copy)
{{- end}}
Expand Down
11 changes: 6 additions & 5 deletions ax/cmd/table/type2go/type2go.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ func (p *parser) init(configs []*table.Config) {
}
}
p.Vars = append(p.Vars, &configVar{
Name: c.GetName(),
Desc: c.GetDesc(),
HasDesc: c.GetDesc() != "",
Type: typ,
IsMake: len(c.GetIndexes()) > 0,
Name: c.GetName(),
Desc: c.GetDesc(),
HasDesc: c.GetDesc() != "",
Type: typ,
ValueType: charproc.FirstUpper(c.GetName()),
IsMake: len(c.GetIndexes()) > 0,
})

// struct
Expand Down
4 changes: 2 additions & 2 deletions ax/cmd/table/type2go/type2go_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package type2go

import (
"fmt"
"github.com/kercylan98/minotaur/ax/cmd/table"
"github.com/kercylan98/minotaur/ax/cmd/table/fieldparser"
"github.com/kercylan98/minotaur/ax/cmd/table/lua2jsonparser"
"github.com/kercylan98/minotaur/ax/cmd/table/xlsxsheet"
"github.com/kercylan98/minotaur/toolkit/fileproc"
"github.com/tealeg/xlsx"
"testing"
)
Expand All @@ -15,5 +15,5 @@ func TestGen(t *testing.T) {
t1 := xlsxsheet.NewTable(xlsxFile.Sheets[1])
r := table.GenerateConfigs([]table.Table{t1}, fieldparser.New(), New("type2go"), lua2jsonparser.New())
code := r.GenerateCode()
fileproc.WriteToFile("./template_test_config.go", []byte(code))
fmt.Println(code)
}
11 changes: 6 additions & 5 deletions ax/cmd/table/type2go/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ type configStructField struct {
}

type configVar struct {
Name string
Desc string
HasDesc bool
Type string
IsMake bool
Name string
Desc string
HasDesc bool
Type string
IsMake bool
ValueType string
}
Loading

0 comments on commit 818b80c

Please sign in to comment.