diff --git a/planner/configexport/configexport.go b/planner/configexport/configexport.go index aa28c4d..685ad9e 100644 --- a/planner/configexport/configexport.go +++ b/planner/configexport/configexport.go @@ -36,6 +36,9 @@ type ConfigExport struct { func (slf *ConfigExport) ExportClient(prefix, outputDir string) { for _, config := range slf.configs { config := config + if len(prefix) > 0 { + config.Prefix = fmt.Sprintf("%s.", prefix) + } if err := file.WriterFile(filepath.Join(outputDir, fmt.Sprintf("%s.%s.json", prefix, config.Name)), config.JsonClient()); err != nil { panic(err) } @@ -45,13 +48,21 @@ func (slf *ConfigExport) ExportClient(prefix, outputDir string) { func (slf *ConfigExport) ExportServer(prefix, outputDir string) { for _, config := range slf.configs { config := config + if len(prefix) > 0 { + config.Prefix = fmt.Sprintf("%s.", prefix) + } if err := file.WriterFile(filepath.Join(outputDir, fmt.Sprintf("%s.%s.json", prefix, config.Name)), config.JsonServer()); err != nil { panic(err) } } } -func (slf *ConfigExport) ExportGo(outputDir string) { +func (slf *ConfigExport) ExportGo(prefix, outputDir string) { + if len(prefix) > 0 { + for _, config := range slf.configs { + config.Prefix = fmt.Sprintf("%s.", prefix) + } + } slf.exportGoConfig(outputDir) slf.exportGoDefine(outputDir) } diff --git a/planner/configexport/configexport_test.go b/planner/configexport/configexport_test.go index 90a5b9b..b8a379d 100644 --- a/planner/configexport/configexport_test.go +++ b/planner/configexport/configexport_test.go @@ -5,7 +5,7 @@ import "testing" func TestNew(t *testing.T) { c := New(`D:\sources\minotaur\planner\configexport\template.xlsx`) - c.ExportGo("./example") + c.ExportGo("server", "./example") c.ExportClient("client", "./example") c.ExportServer("server", "./example") } diff --git a/planner/configexport/example/client.UNConfig.json b/planner/configexport/example/client.UNConfig.json index 2174b4c..67c853c 100644 --- a/planner/configexport/example/client.UNConfig.json +++ b/planner/configexport/example/client.UNConfig.json @@ -11,8 +11,8 @@ "name": "张飞" }, "1": { - "name": "刘备", - "id": 2 + "id": 2, + "name": "刘备" } } } \ No newline at end of file diff --git a/planner/configexport/example/client.XXConfig.json b/planner/configexport/example/client.XXConfig.json index 8c7c6c3..720cdf1 100644 --- a/planner/configexport/example/client.XXConfig.json +++ b/planner/configexport/example/client.XXConfig.json @@ -1,44 +1,27 @@ { "1": { - "a": { - "Id": 1, - "Count": "a", - "Award": { - "0": "asd", - "1": "12" - }, + "b": { "Other": { "0": { - "name": "张飞", - "id": 1 + "id": 1, + "name": "张飞" }, "1": { "id": 2, "name": "刘备" } - } - }, - "b": { + }, "Id": 1, "Count": "b", "Award": { "0": "asd", "1": "12" - }, - "Other": { - "0": { - "id": 1, - "name": "张飞" - }, - "1": { - "id": 2, - "name": "刘备" - } } } }, "2": { "c": { + "Count": "c", "Award": { "0": "asd", "1": "12" @@ -53,8 +36,7 @@ "name": "刘备" } }, - "Id": 2, - "Count": "c" + "Id": 2 }, "d": { "Id": 2, diff --git a/planner/configexport/example/config.go b/planner/configexport/example/config.go index 2e581e2..9d80983 100644 --- a/planner/configexport/example/config.go +++ b/planner/configexport/example/config.go @@ -14,8 +14,8 @@ var ( ) func LoadConfig(handle func(filename string, config any) error) { - handle("XXConfig.json", &gameXXConfig) - handle("UNConfig.json", &gameUNConfig) + handle("server.XXConfig.json", &gameXXConfig) + handle("server.UNConfig.json", &gameUNConfig) } func Refresh() { diff --git a/planner/configexport/example/server.XXConfig.json b/planner/configexport/example/server.XXConfig.json index 2416d88..2019748 100644 --- a/planner/configexport/example/server.XXConfig.json +++ b/planner/configexport/example/server.XXConfig.json @@ -1,34 +1,10 @@ { "1": { - "a": { - "Id": 1, - "Count": "a", - "Info": { - "id": 1, - "name": "小明", - "info": { - "exp": { - "mux": 10, - "count": 100 - }, - "lv": 1 - } - }, - "Other": { - "0": { - "id": 1, - "name": "张飞" - }, - "1": { - "id": 2, - "name": "刘备" - } - } - }, "b": { "Id": 1, "Count": "b", "Info": { + "id": 1, "name": "小明", "info": { "lv": 1, @@ -36,13 +12,12 @@ "mux": 10, "count": 100 } - }, - "id": 1 + } }, "Other": { "0": { - "id": 1, - "name": "张飞" + "name": "张飞", + "id": 1 }, "1": { "id": 2, @@ -66,41 +41,41 @@ } }, "Other": { + "0": { + "name": "张飞", + "id": 1 + }, "1": { "id": 2, "name": "刘备" - }, - "0": { - "id": 1, - "name": "张飞" } }, "Id": 2 }, "d": { - "Id": 2, - "Count": "d", "Info": { - "id": 1, "name": "小明", "info": { "lv": 1, "exp": { - "count": 100, - "mux": 10 + "mux": 10, + "count": 100 } - } + }, + "id": 1 }, "Other": { - "0": { - "id": 1, - "name": "张飞" - }, "1": { "id": 2, "name": "刘备" + }, + "0": { + "id": 1, + "name": "张飞" } - } + }, + "Id": 2, + "Count": "d" } } } \ No newline at end of file diff --git a/planner/configexport/internal/config.go b/planner/configexport/internal/config.go index 65cda64..399a01b 100644 --- a/planner/configexport/internal/config.go +++ b/planner/configexport/internal/config.go @@ -27,6 +27,7 @@ func NewConfig(sheet *xlsx.Sheet) (*Config, error) { } type Config struct { + Prefix string DisplayName string Name string Describe string @@ -118,27 +119,31 @@ func (slf *Config) initField(sheet *xlsx.Sheet) error { var ( describe, fieldName, fieldType, exportParam string ) - + var skip bool if value := slf.matrix.Get(dx, dy); value == nil { - return ErrReadConfigFailedWithFieldPosition + skip = true } else { describe = value.String() } if value := slf.matrix.Get(nx, ny); value == nil { - return ErrReadConfigFailedWithFieldPosition + skip = true } else { fieldName = str.FirstUpper(strings.TrimSpace(value.String())) } if value := slf.matrix.Get(tx, ty); value == nil { - return ErrReadConfigFailedWithFieldPosition + skip = true } else { fieldType = strings.TrimSpace(value.String()) } if value := slf.matrix.Get(ex, ey); value == nil { - return ErrReadConfigFailedWithFieldPosition + skip = true } else { exportParam = strings.ToLower(strings.TrimSpace(value.String())) } + if len(strings.TrimSpace(fieldName))+len(strings.TrimSpace(fieldType))+len(strings.TrimSpace(exportParam)) < 3 { + skip = true + } + var field = NewField(slf.Name, fieldName, fieldType) field.Describe = describe field.ExportParam = exportParam @@ -159,38 +164,41 @@ func (slf *Config) initField(sheet *xlsx.Sheet) error { ey++ } - field.Ignore = slf.excludeFields[len(slf.Fields)] - if !field.Ignore { - if strings.HasPrefix(field.Describe, slf.ignore) { - field.Ignore = true - } else if strings.HasPrefix(field.Name, slf.ignore) { - field.Ignore = true - } else if strings.HasPrefix(field.Type, slf.ignore) { - field.Ignore = true - } else if strings.HasPrefix(field.ExportParam, slf.ignore) { - field.Ignore = true - } - } - if !field.Ignore { - switch exportParam { - case "s", "c", "sc", "cs": - default: - return ErrReadConfigFailedWithExportParamException - } - } + if !skip { - if fields[field.Name] && !field.Ignore { - return ErrReadConfigFailedWithNameDuplicate - } - if index > 0 && !field.Ignore { - if _, exist := basicTypeName[field.Type]; !exist { - return ErrReadConfigFailedWithIndexTypeException + field.Ignore = slf.excludeFields[len(slf.Fields)] + if !field.Ignore { + if strings.HasPrefix(field.Describe, slf.ignore) { + field.Ignore = true + } else if strings.HasPrefix(field.Name, slf.ignore) { + field.Ignore = true + } else if strings.HasPrefix(field.Type, slf.ignore) { + field.Ignore = true + } else if strings.HasPrefix(field.ExportParam, slf.ignore) { + field.Ignore = true + } + } + if !field.Ignore { + switch exportParam { + case "s", "c", "sc", "cs": + default: + continue + } } - index-- - } - fields[field.Name] = true - slf.Fields = append(slf.Fields, field) + if fields[field.Name] && !field.Ignore { + return ErrReadConfigFailedWithNameDuplicate + } + if index > 0 && !field.Ignore { + if _, exist := basicTypeName[field.Type]; !exist { + return ErrReadConfigFailedWithIndexTypeException + } + index-- + } + + fields[field.Name] = true + slf.Fields = append(slf.Fields, field) + } if horizontal { if dx >= slf.matrix.GetWidth() { @@ -223,17 +231,39 @@ func (slf *Config) initData() error { var lineServer = map[any]any{} var lineClient = map[any]any{} + var skip bool + var offset int for i := 0; i < len(slf.Fields); i++ { if slf.excludeFields[i] { + if c := slf.matrix.Get(x+i, y); c != nil && strings.HasPrefix(c.String(), "#") { + skip = true + break + } continue } field := slf.Fields[i] + if field.Ignore { + continue + } var value any if slf.horizontal { - value = getValueWithType(field.SourceType, slf.matrix.Get(x+i, y).String()) + c := slf.matrix.Get(x+i, y) + if c == nil || (currentIndex < slf.IndexCount && len(c.String()) == 0) { + skip = true + break + } + value = getValueWithType(field.SourceType, c.String()) } else { - value = getValueWithType(field.SourceType, slf.matrix.Get(x, y+i).String()) + c := slf.matrix.Get(x, y+i+offset) + for c == nil { + offset++ + c = slf.matrix.Get(x, y+i+offset) + if y+i+offset >= slf.matrix.GetHeight() { + break + } + } + value = getValueWithType(field.SourceType, c.String()) } switch field.ExportParam { case "s": @@ -280,12 +310,11 @@ func (slf *Config) initData() error { break } } else { - x++ - if x >= slf.matrix.GetWidth() { + if !skip { slf.dataServer = lineServer slf.dataClient = lineClient - break } + break } } if slf.horizontal { diff --git a/planner/configexport/internal/template.go b/planner/configexport/internal/template.go index 82bf982..cb0c64d 100644 --- a/planner/configexport/internal/template.go +++ b/planner/configexport/internal/template.go @@ -42,7 +42,7 @@ var ( func LoadConfig(handle func(filename string, config any) error) { {{range $index, $config := .Configs}} - handle("{{$config.Name}}.json", &game{{$config.Name}}) + handle("{{$config.Prefix}}{{$config.Name}}.json", &game{{$config.Name}}) {{end}} } diff --git a/planner/configexport/template.xlsx b/planner/configexport/template.xlsx index b423244..a3affdf 100644 Binary files a/planner/configexport/template.xlsx and b/planner/configexport/template.xlsx differ