diff --git a/planner/configexport/configexport.go b/planner/configexport/configexport.go index c192f10..e5c5cb2 100644 --- a/planner/configexport/configexport.go +++ b/planner/configexport/configexport.go @@ -1,18 +1,15 @@ -package main +package configxport 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" + "strings" ) func New(xlsxPath string) *ConfigExport { @@ -22,7 +19,9 @@ func New(xlsxPath string) *ConfigExport { panic(err) } for i := 0; i < len(xlsxFile.Sheets); i++ { - ce.configs = append(ce.configs, internal.NewConfig(xlsxFile.Sheets[i])) + if config := internal.NewConfig(xlsxFile.Sheets[i]); config != nil { + ce.configs = append(ce.configs, config) + } } return ce } @@ -33,31 +32,14 @@ type ConfigExport struct { } 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() + if err := file.WriterFile(filepath.Join(outputDir, fmt.Sprintf("%s.json", config.GetName())), config.GetJSON()); err != nil { + panic(err) + } + if err := file.WriterFile(filepath.Join(outputDir, fmt.Sprintf("%s.client.json", config.GetName())), config.GetJSONC()); err != nil { + panic(err) + } } } @@ -69,13 +51,21 @@ func (slf *ConfigExport) ExportGo(packageName string, outputDir 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 config.GetIndexCount() == 0 { + varsMake += fmt.Sprintf("_%sReady = new(%s)"+` + if err := handle("%s.json", &_%sReady); err != nil { + panic(err) + } +`, str.FirstUpper(config.GetName()), strings.TrimPrefix(v, "*"), str.FirstUpper(config.GetName()), str.FirstUpper(config.GetName())) + } else { + 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())) + varsReplace += fmt.Sprintf("%s = _%sReady\n", str.FirstUpper(config.GetName()), str.FirstUpper(config.GetName())) } _ = os.MkdirAll(outputDir, 0666) diff --git a/planner/configexport/internal/config.go b/planner/configexport/internal/config.go index 0096fdd..898df13 100644 --- a/planner/configexport/internal/config.go +++ b/planner/configexport/internal/config.go @@ -14,105 +14,186 @@ func NewConfig(sheet *xlsx.Sheet) *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")) - } + if len(config.Sheet.Rows) < 2 || len(config.Sheet.Rows[1].Cells) < 2 { + return nil } - 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 - } + if config.GetIndexCount() > 0 && len(config.Sheet.Rows) < 7 { + return nil + } + if config.GetIndexCount() > 0 { 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()) + 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 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 + // 分析数据 + { + 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")) } - 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] { + 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)) + dataLineC = make([]map[any]any, len(config.Sheet.Rows)) + ) + for i := 1; i < len(config.describeLine.Cells); i++ { + if 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 + 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()) + + var needC, needS bool + switch strings.ToLower(exportParam) { + case "c": + needC = true + case "s": + needS = true + case "sc", "cs": + needS = true + needC = true + } + + if needC { + line := dataLineC[row] + if line == nil { + line = map[any]any{} + dataLineC[row] = line + } + line[name] = value + } + + if needS { + 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 dataSourceC = make(map[any]any) + var dataC = dataSourceC + 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 { + if strings.HasPrefix(typeLine.Cells[i].String(), "#") { + break + } + continue + } + if 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) + } + + m, exist = dataC[value] + if !exist { + if currentIndex == index { + dataC[value] = dataLineC[row] + } else { + m = map[any]any{} + dataC[value] = m + } + } + if currentIndex < index { + dataC = m.(map[any]any) } } - if currentIndex < index { - data = m.(map[any]any) - } + } + data = dataSource + dataC = dataSourceC + currentIndex = 0 + } + config.data = dataSource + config.dataC = dataSourceC + } else { + config.data = map[any]any{} + config.dataC = map[any]any{} + for i := 4; i < len(config.Rows); i++ { + row := config.Sheet.Rows[i] + + desc := row.Cells[0].String() + if strings.HasPrefix(desc, "#") { + continue + } + name := row.Cells[1].String() + typ := row.Cells[2].String() + exportParam := row.Cells[3].String() + value := row.Cells[4].String() + + switch strings.ToLower(exportParam) { + case "c": + config.dataC[name] = getValueWithType(typ, value) + case "s": + config.data[name] = getValueWithType(typ, value) + case "sc", "cs": + config.dataC[name] = getValueWithType(typ, value) + config.data[name] = getValueWithType(typ, value) } } - data = dataSource - currentIndex = 0 } - config.data = dataSource - config.dataLine = dataLine[len(dataLine)-1] return config } @@ -124,7 +205,7 @@ type Config struct { typeLine *xlsx.Row exportParamLine *xlsx.Row data map[any]any - dataLine map[any]any + dataC map[any]any } // GetDisplayName 获取显示名称 @@ -134,7 +215,7 @@ func (slf *Config) GetDisplayName() string { // GetName 获取配置名称 func (slf *Config) GetName() string { - return slf.Sheet.Rows[0].Cells[1].String() + return str.FirstUpper(slf.Sheet.Rows[0].Cells[1].String()) } // GetIndexCount 获取索引数量 @@ -160,8 +241,20 @@ func (slf *Config) GetJSON() []byte { return bytes } +// GetJSONC 获取JSON类型数据 +func (slf *Config) GetJSONC() []byte { + bytes, err := jsonIter.MarshalIndent(slf.dataC, "", " ") + if err != nil { + panic(err) + } + return bytes +} + func (slf *Config) GetVariable() string { var index = slf.GetIndexCount() + if index <= 0 { + return fmt.Sprintf("*_%s", slf.GetName()) + } var mapStr = "map[%s]%s" for i := 1; i < len(slf.typeLine.Cells); i++ { if slf.skipField[i] { @@ -185,15 +278,41 @@ func (slf *Config) GetVariable() string { func (slf *Config) GetStruct() string { var result string + if slf.GetIndexCount() <= 0 { + for i := 4; i < len(slf.Rows); i++ { + row := slf.Sheet.Rows[i] + + desc := row.Cells[0].String() + if strings.HasPrefix(desc, "#") { + continue + } + name := row.Cells[1].String() + typ := row.Cells[2].String() + exportParam := row.Cells[3].String() + switch strings.ToLower(exportParam) { + case "s", "sc", "cs": + result += fmt.Sprintf("%s %s\n", str.FirstUpper(name), slf.GetType(typ)) + case "c": + continue + } + } + + return fmt.Sprintf("type _%s struct{\n%s}", slf.GetName(), result) + } 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)) + exportParam := slf.exportParamLine.Cells[i].String() + switch strings.ToLower(exportParam) { + case "s", "sc", "cs": + name = str.FirstUpper(name) + result += fmt.Sprintf("%s %s\n", name, slf.GetType(typ)) + case "c": + continue + } } return fmt.Sprintf("type _%s struct{\n%s}", str.FirstUpper(slf.GetName()), result) } diff --git a/planner/configexport/internal/template.xlsx b/planner/configexport/internal/template.xlsx index b21a7a5..b423244 100644 Binary files a/planner/configexport/internal/template.xlsx and b/planner/configexport/internal/template.xlsx differ