配置导出工具实现
This commit is contained in:
parent
a685758ca9
commit
423318168f
7
go.mod
7
go.mod
|
@ -26,6 +26,7 @@ require (
|
|||
github.com/goccy/go-json v0.10.0 // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/gopherjs/gopherjs v1.17.2 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/jonboulle/clockwork v0.3.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/jtolds/gls v4.20.0+incompatible // indirect
|
||||
|
@ -50,8 +51,14 @@ require (
|
|||
github.com/richardlehane/mscfb v1.0.4 // indirect
|
||||
github.com/richardlehane/msoleps v1.0.3 // indirect
|
||||
github.com/smartystreets/assertions v1.13.1 // indirect
|
||||
github.com/spf13/cobra v1.7.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/tealeg/xlsx v1.0.5 // indirect
|
||||
github.com/templexxx/cpu v0.0.9 // indirect
|
||||
github.com/templexxx/xorsimd v0.4.1 // indirect
|
||||
github.com/tidwall/gjson v1.14.4 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
github.com/tjfoc/gmsm v1.4.1 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.9 // indirect
|
||||
|
|
17
go.sum
17
go.sum
|
@ -13,6 +13,7 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhD
|
|||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
|
@ -62,6 +63,8 @@ github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25d
|
|||
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
|
||||
github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
|
@ -134,12 +137,17 @@ github.com/richardlehane/msoleps v1.0.3 h1:aznSZzrwYRl3rLKRT3gUk9am7T/mLNSnJINvN
|
|||
github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/smartystreets/assertions v1.13.1 h1:Ef7KhSmjZcK6AVf9YbJdvPYG9avaF0ZxudX+ThRdWfU=
|
||||
github.com/smartystreets/assertions v1.13.1/go.mod h1:cXr/IwVfSo/RbCSPhoAPv73p3hlSdrBH/b3SdnW/LMY=
|
||||
github.com/smartystreets/goconvey v1.8.0 h1:Oi49ha/2MURE0WexF052Z0m+BNSGirfjg5RL+JXWq3w=
|
||||
github.com/smartystreets/goconvey v1.8.0/go.mod h1:EdX8jtrTIj26jmjCOVNMVSIYAtgexqXKHOXW2Dx9JLg=
|
||||
github.com/sony/sonyflake v1.1.0 h1:wnrEcL3aOkWmPlhScLEGAXKkLAIslnBteNUq4Bw6MM4=
|
||||
github.com/sony/sonyflake v1.1.0/go.mod h1:LORtCywH/cq10ZbyfhKrHYgAUGH7mOBa76enV9txy/Y=
|
||||
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
|
||||
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
|
@ -151,12 +159,20 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
|||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
|
||||
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
|
||||
github.com/templexxx/cpu v0.0.1/go.mod h1:w7Tb+7qgcAlIyX4NhLuDKt78AHA5SzPmq0Wj6HiEnnk=
|
||||
github.com/templexxx/cpu v0.0.7/go.mod h1:w7Tb+7qgcAlIyX4NhLuDKt78AHA5SzPmq0Wj6HiEnnk=
|
||||
github.com/templexxx/cpu v0.0.9 h1:cGGLK8twbc1J1S/fHnZW7BylXYaFP+0fR2s+nzsFDiU=
|
||||
github.com/templexxx/cpu v0.0.9/go.mod h1:w7Tb+7qgcAlIyX4NhLuDKt78AHA5SzPmq0Wj6HiEnnk=
|
||||
github.com/templexxx/xorsimd v0.4.1 h1:iUZcywbOYDRAZUasAs2eSCUW8eobuZDy0I9FJiORkVg=
|
||||
github.com/templexxx/xorsimd v0.4.1/go.mod h1:W+ffZz8jJMH2SXwuKu9WhygqBMbFnp14G2fqEr8qaNo=
|
||||
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
|
||||
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
|
||||
github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=
|
||||
github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
|
||||
|
@ -322,6 +338,7 @@ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175
|
|||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
package main
|
||||
|
||||
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"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func New(xlsxPath string) *ConfigExport {
|
||||
ce := &ConfigExport{xlsxPath: xlsxPath}
|
||||
xlsxFile, err := xlsx.OpenFile(xlsxPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for i := 0; i < len(xlsxFile.Sheets); i++ {
|
||||
ce.configs = append(ce.configs, internal.NewConfig(xlsxFile.Sheets[i]))
|
||||
}
|
||||
return ce
|
||||
}
|
||||
|
||||
type ConfigExport struct {
|
||||
xlsxPath string
|
||||
configs []*internal.Config
|
||||
}
|
||||
|
||||
func (slf *ConfigExport) ExportJSON(outputDir string) {
|
||||
var errors []func()
|
||||
var wait sync.WaitGroup
|
||||
for _, config := range slf.configs {
|
||||
config := config
|
||||
go func() {
|
||||
wait.Add(1)
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
errors = append(errors, func() {
|
||||
log.Error("导出失败", zap.String("名称", slf.xlsxPath), zap.String("Sheet", config.GetName()), zap.Any("err", err))
|
||||
fmt.Println(debug.Stack())
|
||||
})
|
||||
}
|
||||
}()
|
||||
if err := file.WriterFile(filepath.Join(outputDir, fmt.Sprintf("%s.json", config.GetName())), config.GetJSON()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
wait.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
wait.Wait()
|
||||
|
||||
for _, f := range errors {
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *ConfigExport) ExportGo(packageName string, outputDir string) {
|
||||
var vars string
|
||||
var varsMake string
|
||||
var types string
|
||||
var varsReplace string
|
||||
for _, config := range slf.configs {
|
||||
v := config.GetVariable()
|
||||
vars += fmt.Sprintf("var %s %s\nvar _%sReady %s\n", str.FirstUpper(config.GetName()), v, str.FirstUpper(config.GetName()), v)
|
||||
varsMake += fmt.Sprintf("_%sReady = make(%s)"+`
|
||||
if err := handle("%s.json", &_%sReady); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
`, str.FirstUpper(config.GetName()), v, str.FirstUpper(config.GetName()), str.FirstUpper(config.GetName()))
|
||||
types += fmt.Sprintf("%s\n", config.GetStruct())
|
||||
varsReplace += fmt.Sprintf("%s = _%sReady", str.FirstUpper(config.GetName()), str.FirstUpper(config.GetName()))
|
||||
}
|
||||
|
||||
_ = os.MkdirAll(outputDir, 0666)
|
||||
if err := file.WriterFile(filepath.Join(outputDir, "config.struct.go"), []byte(fmt.Sprintf(internal.TemplateStructGo, packageName, types))); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := file.WriterFile(filepath.Join(outputDir, "config.go"), []byte(fmt.Sprintf(internal.TemplateGo, packageName, vars, varsMake, varsReplace))); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cmd := exec.Command("gofmt", "-w", filepath.Join(outputDir, "config.struct.go"))
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("gofmt", "-w", filepath.Join(outputDir, "config.go"))
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,257 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
jsonIter "github.com/json-iterator/go"
|
||||
"github.com/kercylan98/minotaur/utils/str"
|
||||
"github.com/tealeg/xlsx"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewConfig(sheet *xlsx.Sheet) *Config {
|
||||
config := &Config{
|
||||
Sheet: sheet,
|
||||
}
|
||||
|
||||
var (
|
||||
skipField = make(map[int]bool)
|
||||
describeLine = config.Sheet.Rows[3]
|
||||
nameLine = config.Sheet.Rows[4]
|
||||
typeLine = config.Sheet.Rows[5]
|
||||
exportParamLine = config.Sheet.Rows[6]
|
||||
)
|
||||
// 分析数据
|
||||
{
|
||||
for i := 1; i < len(describeLine.Cells); i++ {
|
||||
describe := strings.TrimSpace(nameLine.Cells[i].String())
|
||||
if strings.HasPrefix(describe, "#") {
|
||||
skipField[i] = true
|
||||
continue
|
||||
}
|
||||
typ := strings.TrimSpace(typeLine.Cells[i].String())
|
||||
if strings.HasPrefix(typ, "#") || len(typ) == 0 {
|
||||
skipField[i] = true
|
||||
continue
|
||||
}
|
||||
exportParam := strings.TrimSpace(exportParamLine.Cells[i].String())
|
||||
if strings.HasPrefix(exportParam, "#") || len(exportParam) == 0 {
|
||||
skipField[i] = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
if len(nameLine.Cells)-1-len(skipField) < config.GetIndexCount() {
|
||||
panic(errors.New("index count must greater or equal to field count"))
|
||||
}
|
||||
}
|
||||
|
||||
config.skipField = skipField
|
||||
config.describeLine = describeLine
|
||||
config.nameLine = nameLine
|
||||
config.typeLine = typeLine
|
||||
config.exportParamLine = exportParamLine
|
||||
|
||||
// 整理数据
|
||||
var (
|
||||
dataLine = make([]map[any]any, len(config.Sheet.Rows))
|
||||
)
|
||||
for i := 1; i < len(config.describeLine.Cells); i++ {
|
||||
if skipField[i] {
|
||||
continue
|
||||
}
|
||||
|
||||
var (
|
||||
//describe = strings.TrimSpace(describeLine.Cells[i].String())
|
||||
name = strings.TrimSpace(nameLine.Cells[i].String())
|
||||
//typ = strings.TrimSpace(typeLine.Cells[i].String())
|
||||
//exportParam = strings.TrimSpace(exportParamLine.Cells[i].String())
|
||||
)
|
||||
|
||||
for row := 7; row < len(config.Sheet.Rows); row++ {
|
||||
//value := slf.Sheet.Rows[row].Cells[i].String()
|
||||
var value = getValueWithType(typeLine.Cells[i].String(), config.Sheet.Rows[row].Cells[i].String())
|
||||
|
||||
line := dataLine[row]
|
||||
if line == nil {
|
||||
line = map[any]any{}
|
||||
dataLine[row] = line
|
||||
}
|
||||
line[name] = value
|
||||
}
|
||||
}
|
||||
|
||||
// 索引
|
||||
var dataSource = make(map[any]any)
|
||||
var data = dataSource
|
||||
var index = config.GetIndexCount()
|
||||
var currentIndex = 0
|
||||
for row := 7; row < len(config.Sheet.Rows); row++ {
|
||||
for i, cell := range config.Sheet.Rows[row].Cells {
|
||||
if i == 0 || skipField[i] {
|
||||
continue
|
||||
}
|
||||
var value = getValueWithType(typeLine.Cells[i].String(), cell.String())
|
||||
|
||||
if currentIndex < index {
|
||||
currentIndex++
|
||||
m, exist := data[value]
|
||||
if !exist {
|
||||
if currentIndex == index {
|
||||
data[value] = dataLine[row]
|
||||
} else {
|
||||
m = map[any]any{}
|
||||
data[value] = m
|
||||
}
|
||||
}
|
||||
if currentIndex < index {
|
||||
data = m.(map[any]any)
|
||||
}
|
||||
}
|
||||
}
|
||||
data = dataSource
|
||||
currentIndex = 0
|
||||
}
|
||||
config.data = dataSource
|
||||
config.dataLine = dataLine[len(dataLine)-1]
|
||||
return config
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
*xlsx.Sheet
|
||||
skipField map[int]bool
|
||||
describeLine *xlsx.Row
|
||||
nameLine *xlsx.Row
|
||||
typeLine *xlsx.Row
|
||||
exportParamLine *xlsx.Row
|
||||
data map[any]any
|
||||
dataLine map[any]any
|
||||
}
|
||||
|
||||
// GetDisplayName 获取显示名称
|
||||
func (slf *Config) GetDisplayName() string {
|
||||
return slf.Name
|
||||
}
|
||||
|
||||
// GetName 获取配置名称
|
||||
func (slf *Config) GetName() string {
|
||||
return slf.Sheet.Rows[0].Cells[1].String()
|
||||
}
|
||||
|
||||
// GetIndexCount 获取索引数量
|
||||
func (slf *Config) GetIndexCount() int {
|
||||
index, err := slf.Sheet.Rows[1].Cells[1].Int()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
// GetData 获取数据
|
||||
func (slf *Config) GetData() map[any]any {
|
||||
return slf.data
|
||||
}
|
||||
|
||||
// GetJSON 获取JSON类型数据
|
||||
func (slf *Config) GetJSON() []byte {
|
||||
bytes, err := jsonIter.MarshalIndent(slf.GetData(), "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
func (slf *Config) GetVariable() string {
|
||||
var index = slf.GetIndexCount()
|
||||
var mapStr = "map[%s]%s"
|
||||
for i := 1; i < len(slf.typeLine.Cells); i++ {
|
||||
if slf.skipField[i] {
|
||||
continue
|
||||
}
|
||||
typ := slf.typeLine.Cells[i].String()
|
||||
if index > 0 {
|
||||
index--
|
||||
if index == 0 {
|
||||
mapStr = fmt.Sprintf(mapStr, typ, "%s")
|
||||
} else {
|
||||
mapStr = fmt.Sprintf(mapStr, typ, "map[%s]%s")
|
||||
}
|
||||
} else {
|
||||
mapStr = fmt.Sprintf(mapStr, "*_"+str.FirstUpper(slf.GetName()))
|
||||
break
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s", mapStr)
|
||||
}
|
||||
|
||||
func (slf *Config) GetStruct() string {
|
||||
var result string
|
||||
for i := 1; i < len(slf.typeLine.Cells); i++ {
|
||||
if slf.skipField[i] {
|
||||
continue
|
||||
}
|
||||
typ := slf.typeLine.Cells[i].String()
|
||||
name := slf.nameLine.Cells[i].String()
|
||||
|
||||
name = str.FirstUpper(name)
|
||||
result += fmt.Sprintf("%s %s\n", name, slf.GetType(typ))
|
||||
}
|
||||
return fmt.Sprintf("type _%s struct{\n%s}", str.FirstUpper(slf.GetName()), result)
|
||||
}
|
||||
|
||||
func (slf *Config) GetType(fieldType string) string {
|
||||
if name, exist := basicTypeName[fieldType]; exist {
|
||||
return name
|
||||
} else if strings.HasPrefix(fieldType, "[]") {
|
||||
s := strings.TrimPrefix(fieldType, "[]")
|
||||
if name, exist := basicTypeName[s]; exist {
|
||||
return fmt.Sprintf("map[int]%s", name)
|
||||
} else {
|
||||
return slf.GetType(s)
|
||||
}
|
||||
}
|
||||
|
||||
var s = strings.TrimSuffix(strings.TrimPrefix(fieldType, "{"), "}")
|
||||
var fields []string
|
||||
var field string
|
||||
var leftBrackets []int
|
||||
for i, c := range s {
|
||||
switch c {
|
||||
case ',':
|
||||
if len(leftBrackets) == 0 {
|
||||
fields = append(fields, field)
|
||||
field = ""
|
||||
} else {
|
||||
field += string(c)
|
||||
}
|
||||
case '{':
|
||||
leftBrackets = append(leftBrackets, i)
|
||||
field += string(c)
|
||||
case '}':
|
||||
leftBrackets = leftBrackets[:len(leftBrackets)-1]
|
||||
field += string(c)
|
||||
if len(leftBrackets) == 0 {
|
||||
fields = append(fields, field)
|
||||
field = ""
|
||||
}
|
||||
default:
|
||||
field += string(c)
|
||||
}
|
||||
}
|
||||
if len(field) > 0 {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
|
||||
var result = "*struct {\n%s}"
|
||||
var fieldStr string
|
||||
for _, fieldInfo := range fields {
|
||||
fieldName, fieldType := str.KV(strings.TrimSpace(fieldInfo), ":")
|
||||
n, exist := basicTypeName[fieldType]
|
||||
if exist {
|
||||
fieldStr += fmt.Sprintf("%s %s\n", str.FirstUpper(fieldName), n)
|
||||
} else {
|
||||
fieldStr += fmt.Sprintf("%s %s\n", str.FirstUpper(fieldName), slf.GetType(fieldType))
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf(result, fieldStr)
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package internal
|
||||
|
||||
var TemplateGo = `// Code generated DO NOT EDIT.
|
||||
|
||||
package %s
|
||||
|
||||
import (
|
||||
jsonIter "github.com/json-iterator/go"
|
||||
"os"
|
||||
)
|
||||
|
||||
var json = jsonIter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
%s
|
||||
|
||||
func LoadConfig(handle func(filename string, config any) error) {
|
||||
%s
|
||||
}
|
||||
|
||||
func Replace() {
|
||||
%s
|
||||
}
|
||||
|
||||
|
||||
func DefaultLoad(filepath string) {
|
||||
LoadConfig(func(filename string, config any) error {
|
||||
bytes, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(bytes, &config)
|
||||
})
|
||||
}
|
||||
|
||||
`
|
||||
|
||||
var TemplateStructGo = `// Code generated DO NOT EDIT.
|
||||
|
||||
package %s
|
||||
|
||||
%s
|
||||
`
|
|
@ -0,0 +1,46 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/kercylan98/minotaur/utils/file"
|
||||
"github.com/kercylan98/minotaur/utils/log"
|
||||
"github.com/tealeg/xlsx"
|
||||
"go.uber.org/zap"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func ExportJSON(xlsxPath string, output string) {
|
||||
xlsxFile, err := xlsx.OpenFile(xlsxPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var errors []func()
|
||||
var wait sync.WaitGroup
|
||||
for _, sheet := range xlsxFile.Sheets {
|
||||
sheet := sheet
|
||||
go func() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
errors = append(errors, func() {
|
||||
log.Error("导出失败", zap.String("名称", xlsxPath), zap.String("Sheet", sheet.Name), zap.Any("err", err))
|
||||
fmt.Println(debug.Stack())
|
||||
})
|
||||
}
|
||||
}()
|
||||
wait.Add(1)
|
||||
config := NewConfig(sheet)
|
||||
if err := file.WriterFile(filepath.Join(output, fmt.Sprintf("%s.json", config.GetName())), config.GetJSON()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
wait.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
wait.Wait()
|
||||
|
||||
for _, f := range errors {
|
||||
f()
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
package internal
|
|
@ -0,0 +1,22 @@
|
|||
package internal
|
||||
|
||||
type XXConfig struct {
|
||||
Id int
|
||||
Count string
|
||||
Award map[int]string
|
||||
Info *struct {
|
||||
Id int
|
||||
Name string
|
||||
Info *struct {
|
||||
Lv int
|
||||
Exp *struct {
|
||||
Mux int
|
||||
Count int
|
||||
}
|
||||
}
|
||||
}
|
||||
Other *struct {
|
||||
Id int
|
||||
Name string
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,222 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"github.com/kercylan98/minotaur/utils/str"
|
||||
"github.com/tidwall/gjson"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var basicType = map[string]func(fieldValue string) any{
|
||||
"string": withStringType,
|
||||
"int": withIntType,
|
||||
"int8": withInt8Type,
|
||||
"int16": withInt16Type,
|
||||
"int32": withInt32Type,
|
||||
"int64": withInt64Type,
|
||||
"uint": withUintType,
|
||||
"uint8": withUint8Type,
|
||||
"uint16": withUint16Type,
|
||||
"uint32": withUint32Type,
|
||||
"uint64": withUint64Type,
|
||||
"float32": withFloat32Type,
|
||||
"float64": withFloat64Type,
|
||||
"float": withFloat64Type,
|
||||
"double": withFloat64Type,
|
||||
"number": withFloat64Type,
|
||||
"byte": withByteType,
|
||||
"rune": withRuneType,
|
||||
"bool": withBoolType,
|
||||
"boolean": withBoolType,
|
||||
}
|
||||
|
||||
var basicTypeName = map[string]string{
|
||||
"string": "string",
|
||||
"int": "int",
|
||||
"int8": "int8",
|
||||
"int16": "int16",
|
||||
"int32": "int32",
|
||||
"int64": "int64",
|
||||
"uint": "uint",
|
||||
"uint8": "uint8",
|
||||
"uint16": "uint16",
|
||||
"uint32": "uint32",
|
||||
"uint64": "uint64",
|
||||
"float32": "float32",
|
||||
"float64": "float64",
|
||||
"float": "float64",
|
||||
"double": "float64",
|
||||
"number": "float64",
|
||||
"byte": "byte",
|
||||
"rune": "rune",
|
||||
"bool": "bool",
|
||||
"boolean": "bool",
|
||||
}
|
||||
|
||||
func getValueWithType(fieldType string, fieldValue string) any {
|
||||
fieldType = strings.ToLower(strings.TrimSpace(fieldType))
|
||||
handle, exist := basicType[fieldType]
|
||||
if exist {
|
||||
return handle(fieldValue)
|
||||
} else {
|
||||
return withStructType(fieldType, fieldValue)
|
||||
}
|
||||
}
|
||||
|
||||
func withStructType(fieldType string, fieldValue string) any {
|
||||
// {id:int,name:string,info:{lv:int,exp:int}}
|
||||
if strings.HasPrefix(fieldType, "[]") {
|
||||
return withSliceType(fieldType, fieldValue)
|
||||
} else if !strings.HasPrefix(fieldType, "{") || !strings.HasSuffix(fieldType, "}") {
|
||||
return nil
|
||||
}
|
||||
var s = strings.TrimSuffix(strings.TrimPrefix(fieldType, "{"), "}")
|
||||
var data = map[any]any{}
|
||||
var fields []string
|
||||
var field string
|
||||
var leftBrackets []int
|
||||
for i, c := range s {
|
||||
switch c {
|
||||
case ',':
|
||||
if len(leftBrackets) == 0 {
|
||||
fields = append(fields, field)
|
||||
field = ""
|
||||
} else {
|
||||
field += string(c)
|
||||
}
|
||||
case '{':
|
||||
leftBrackets = append(leftBrackets, i)
|
||||
field += string(c)
|
||||
case '}':
|
||||
leftBrackets = leftBrackets[:len(leftBrackets)-1]
|
||||
field += string(c)
|
||||
if len(leftBrackets) == 0 {
|
||||
fields = append(fields, field)
|
||||
field = ""
|
||||
}
|
||||
default:
|
||||
field += string(c)
|
||||
}
|
||||
}
|
||||
if len(field) > 0 {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
|
||||
for _, fieldInfo := range fields {
|
||||
fieldName, fieldType := str.KV(strings.TrimSpace(fieldInfo), ":")
|
||||
handle, exist := basicType[fieldType]
|
||||
if exist {
|
||||
data[fieldName] = handle(gjson.Get(fieldValue, fieldName).String())
|
||||
} else {
|
||||
data[fieldName] = withStructType(fieldType, gjson.Get(fieldValue, fieldName).String())
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func withSliceType(fieldType string, fieldValue string) any {
|
||||
if !strings.HasPrefix(fieldType, "[]") {
|
||||
return nil
|
||||
}
|
||||
t := strings.TrimPrefix(fieldType, "[]")
|
||||
var data = map[any]any{}
|
||||
gjson.ForEachLine(fieldValue, func(line gjson.Result) bool {
|
||||
line.ForEach(func(key, value gjson.Result) bool {
|
||||
handle, exist := basicType[t]
|
||||
if exist {
|
||||
data[len(data)] = handle(value.String())
|
||||
} else {
|
||||
data[len(data)] = withStructType(t, value.String())
|
||||
}
|
||||
return true
|
||||
})
|
||||
return true
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
func withStringType(fieldValue string) any {
|
||||
return fieldValue
|
||||
}
|
||||
|
||||
func withIntType(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return value
|
||||
}
|
||||
|
||||
func withInt8Type(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return int8(value)
|
||||
}
|
||||
|
||||
func withInt16Type(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return int16(value)
|
||||
}
|
||||
|
||||
func withInt32Type(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return int32(value)
|
||||
}
|
||||
|
||||
func withInt64Type(fieldValue string) any {
|
||||
value, _ := strconv.ParseInt(fieldValue, 10, 64)
|
||||
return value
|
||||
}
|
||||
|
||||
func withUintType(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return uint(value)
|
||||
}
|
||||
|
||||
func withUint8Type(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return uint8(value)
|
||||
}
|
||||
|
||||
func withUint16Type(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return uint16(value)
|
||||
}
|
||||
|
||||
func withUint32Type(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return uint32(value)
|
||||
}
|
||||
|
||||
func withUint64Type(fieldValue string) any {
|
||||
value, _ := strconv.ParseInt(fieldValue, 10, 64)
|
||||
return uint64(value)
|
||||
}
|
||||
|
||||
func withFloat32Type(fieldValue string) any {
|
||||
value, _ := strconv.ParseFloat(fieldValue, 32)
|
||||
return value
|
||||
}
|
||||
|
||||
func withFloat64Type(fieldValue string) any {
|
||||
value, _ := strconv.ParseFloat(fieldValue, 64)
|
||||
return value
|
||||
}
|
||||
|
||||
func withByteType(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return byte(value)
|
||||
}
|
||||
|
||||
func withRuneType(fieldValue string) any {
|
||||
value, _ := strconv.Atoi(fieldValue)
|
||||
return rune(value)
|
||||
}
|
||||
|
||||
func withBoolType(fieldValue string) any {
|
||||
switch fieldValue {
|
||||
case "0", "false", "!":
|
||||
return false
|
||||
case "1", "true", "&":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
|
@ -1,2 +1,20 @@
|
|||
package str
|
||||
|
||||
// FirstUpper 首字母大写
|
||||
func FirstUpper(str string) string {
|
||||
var upperStr string
|
||||
vv := []rune(str) // 后文有介绍
|
||||
for i := 0; i < len(vv); i++ {
|
||||
if i == 0 {
|
||||
if vv[i] >= 97 && vv[i] <= 122 { // 后文有介绍
|
||||
vv[i] -= 32 // string的码表相差32位
|
||||
upperStr += string(vv[i])
|
||||
} else {
|
||||
return str
|
||||
}
|
||||
} else {
|
||||
upperStr += string(vv[i])
|
||||
}
|
||||
}
|
||||
return upperStr
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue