导表工具优化

This commit is contained in:
kercylan98 2023-05-18 19:17:45 +08:00
parent 72fd4f127e
commit 2372d262bd
9 changed files with 111 additions and 114 deletions

View File

@ -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)
}

View File

@ -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")
}

View File

@ -11,8 +11,8 @@
"name": "张飞"
},
"1": {
"name": "刘备",
"id": 2
"id": 2,
"name": "刘备"
}
}
}

View File

@ -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,

View File

@ -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() {

View File

@ -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"
}
}
}

View File

@ -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 {

View File

@ -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}}
}

Binary file not shown.