other: 配置导表优化

This commit is contained in:
kercylan98 2023-07-17 16:06:36 +08:00
parent 91b2b52fc8
commit 130869af4e
7 changed files with 113 additions and 49 deletions

View File

@ -8,16 +8,25 @@ import (
"strings" "strings"
) )
func NewXlsx(sheet *xlsx.Sheet) *Xlsx { type XlsxExportType int
const (
XlsxExportTypeServer XlsxExportType = iota
XlsxExportTypeClient
)
func NewXlsx(sheet *xlsx.Sheet, exportType XlsxExportType) *Xlsx {
config := &Xlsx{ config := &Xlsx{
sheet: sheet, sheet: sheet,
exportType: exportType,
} }
return config return config
} }
// Xlsx 内置的 Xlsx 配置 // Xlsx 内置的 Xlsx 配置
type Xlsx struct { type Xlsx struct {
sheet *xlsx.Sheet sheet *xlsx.Sheet
exportType XlsxExportType
} }
func (slf *Xlsx) GetConfigName() string { func (slf *Xlsx) GetConfigName() string {
@ -41,12 +50,13 @@ func (slf *Xlsx) GetIndexCount() int {
} }
func (slf *Xlsx) GetFields() []pce.DataField { func (slf *Xlsx) GetFields() []pce.DataField {
var handle = func(desc, name, fieldType, exportType *xlsx.Cell) (pce.DataField, bool) { var handle = func(index int, desc, name, fieldType, exportType *xlsx.Cell) (pce.DataField, bool) {
var field pce.DataField var field pce.DataField
if desc == nil || name == nil || fieldType == nil || exportType == nil { if desc == nil || name == nil || fieldType == nil || exportType == nil {
return field, false return field, false
} }
field = pce.DataField{ field = pce.DataField{
Index: index,
Name: strings.ReplaceAll(strings.ReplaceAll(str.FirstUpper(name.String()), "\r", ""), "\n", ""), Name: strings.ReplaceAll(strings.ReplaceAll(str.FirstUpper(name.String()), "\r", ""), "\n", ""),
Type: fieldType.String(), Type: fieldType.String(),
ExportType: exportType.String(), ExportType: exportType.String(),
@ -65,13 +75,13 @@ func (slf *Xlsx) GetFields() []pce.DataField {
var fields []pce.DataField var fields []pce.DataField
if slf.GetIndexCount() > 0 { if slf.GetIndexCount() > 0 {
for x := 1; x < slf.getWidth(); x++ { for x := 1; x < slf.getWidth(); x++ {
if field, match := handle(slf.get(x, 3), slf.get(x, 4), slf.get(x, 5), slf.get(x, 6)); match { if field, match := handle(x, slf.get(x, 3), slf.get(x, 4), slf.get(x, 5), slf.get(x, 6)); match {
fields = append(fields, field) fields = append(fields, field)
} }
} }
} else { } else {
for y := 4; y < slf.getHeight(); y++ { for y := 4; y < slf.getHeight(); y++ {
if field, match := handle(slf.get(0, y), slf.get(1, y), slf.get(2, y), slf.get(3, y)); match { if field, match := handle(y, slf.get(0, y), slf.get(1, y), slf.get(2, y), slf.get(3, y)); match {
fields = append(fields, field) fields = append(fields, field)
} }
} }
@ -86,32 +96,31 @@ func (slf *Xlsx) GetData() [][]pce.DataInfo {
for y := 7; y < slf.getHeight(); y++ { for y := 7; y < slf.getHeight(); y++ {
var line []pce.DataInfo var line []pce.DataInfo
var stop bool var stop bool
for x := 0; x < slf.getWidth(); x++ {
if prefixCell := slf.get(x, y); prefixCell != nil { if prefixCell := slf.get(0, y); prefixCell != nil {
prefix := prefixCell.String() prefix := prefixCell.String()
if strings.HasPrefix(prefix, "#") { if strings.HasPrefix(prefix, "#") {
break
}
}
if x == 0 {
continue continue
} }
var isIndex = x-1 < slf.GetIndexCount() }
for i, field := range slf.GetFields() {
var isIndex = i-1 < slf.GetIndexCount()
var value string var value string
if valueCell := slf.get(x, y); valueCell != nil { if valueCell := slf.get(field.Index, y); valueCell != nil {
value = valueCell.String() value = valueCell.String()
} else if isIndex { } else if isIndex {
stop = true stop = true
break break
} }
valueCell := slf.get(x, y) valueCell := slf.get(field.Index, y)
if valueCell == nil { if valueCell == nil {
break break
} }
if len(fields) > x-1 { if len(fields) > i-1 {
line = append(line, pce.DataInfo{ line = append(line, pce.DataInfo{
DataField: fields[x-1], DataField: field,
Value: value, Value: value,
}) })
} }
@ -164,7 +173,15 @@ func (slf *Xlsx) get(x, y int) *xlsx.Cell {
func (slf *Xlsx) checkFieldInvalid(field pce.DataField) bool { func (slf *Xlsx) checkFieldInvalid(field pce.DataField) bool {
switch strings.ToLower(field.ExportType) { switch strings.ToLower(field.ExportType) {
case "s", "c", "sc", "cs": case "s":
if slf.exportType != XlsxExportTypeServer {
return true
}
case "c":
if slf.exportType != XlsxExportTypeClient {
return true
}
case "sc", "cs":
default: default:
return true return true
} }

View File

@ -10,7 +10,7 @@ type Exporter struct{}
// ExportStruct 导出结构 // ExportStruct 导出结构
func (slf *Exporter) ExportStruct(tmpl Tmpl, tmplStruct ...*TmplStruct) ([]byte, error) { func (slf *Exporter) ExportStruct(tmpl Tmpl, tmplStruct ...*TmplStruct) ([]byte, error) {
raw, err := tmpl.Render(tmplStruct) raw, err := tmpl.Render(tmplStruct...)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,7 +1,6 @@
package pce package pce
import ( import (
"github.com/kercylan98/minotaur/utils/super"
"reflect" "reflect"
"strings" "strings"
) )
@ -20,6 +19,5 @@ type Field interface {
func GetFieldGolangType(field Field) string { func GetFieldGolangType(field Field) string {
typeOf := reflect.TypeOf(field).Elem() typeOf := reflect.TypeOf(field).Elem()
kind := strings.ToLower(typeOf.Kind().String()) kind := strings.ToLower(typeOf.Kind().String())
name := strings.ToLower(typeOf.Name()) return kind
return super.If(kind == name, kind, name)
} }

View File

@ -26,13 +26,13 @@ type Loader struct {
// LoadStruct 加载结构 // LoadStruct 加载结构
func (slf *Loader) LoadStruct(config Config) *TmplStruct { func (slf *Loader) LoadStruct(config Config) *TmplStruct {
var tmpl = &TmplStruct{ var tmpl = &TmplStruct{
Name: str.FirstUpper(config.GetConfigName()), Name: str.FirstUpper(config.GetConfigName()),
Desc: config.GetDescription(), Desc: config.GetDescription(),
IndexCount: config.GetIndexCount(),
} }
indexCount := config.GetIndexCount()
for i, field := range config.GetFields() { for i, field := range config.GetFields() {
f := tmpl.addField(tmpl.Name, str.FirstUpper(field.Name), field.Desc, field.Type, slf.fields) f := tmpl.addField(tmpl.Name, str.FirstUpper(field.Name), field.Desc, field.Type, slf.fields)
if i < indexCount { if i < tmpl.IndexCount {
f.isIndex = true f.isIndex = true
} }
} }
@ -167,6 +167,7 @@ type DataInfo struct {
// DataField 配置数据字段 // DataField 配置数据字段
type DataField struct { type DataField struct {
Index int // 字段索引
Name string // 字段名称 Name string // 字段名称
Desc string // 字段描述 Desc string // 字段描述
Type string // 字段类型 Type string // 字段类型

View File

@ -3,5 +3,5 @@ package pce
// Tmpl 配置结构模板接口 // Tmpl 配置结构模板接口
type Tmpl interface { type Tmpl interface {
// Render 渲染模板 // Render 渲染模板
Render(templates []*TmplStruct) (string, error) Render(templates ...*TmplStruct) (string, error)
} }

View File

@ -6,9 +6,10 @@ import (
// TmplStruct 模板结构 // TmplStruct 模板结构
type TmplStruct struct { type TmplStruct struct {
Name string // 结构名称 Name string // 结构名称
Desc string // 结构描述 Desc string // 结构描述
Fields []*TmplField // 字段列表 Fields []*TmplField // 字段列表
IndexCount int // 索引数量
} }
// addField 添加字段 // addField 添加字段
@ -20,6 +21,8 @@ func (slf *TmplStruct) addField(parent, name, desc, fieldType string, fields map
} }
if !hash.Exist(fields, fieldType) { if !hash.Exist(fields, fieldType) {
field.setStruct(parent, name, desc, fieldType, fields) field.setStruct(parent, name, desc, fieldType, fields)
} else {
field.Type = GetFieldGolangType(fields[fieldType])
} }
slf.Fields = append(slf.Fields, field) slf.Fields = append(slf.Fields, field)
return field return field

View File

@ -40,11 +40,7 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
var ( var (
json = jsonIter.ConfigCompatibleWithStandardLibrary json = jsonIter.ConfigCompatibleWithStandardLibrary
configs map[Sign]any = map[Sign]any{ configs map[Sign]any
{{- range .Templates}}
{{.Name}}Sign: {{.Name}},
{{- end}}
}
signs = []Sign{ signs = []Sign{
{{- range .Templates}} {{- range .Templates}}
{{.Name}}Sign, {{.Name}}Sign,
@ -55,13 +51,33 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
var ( var (
{{- range .Templates}} {{- range .Templates}}
{{.Name}} {{$.GetVariable .}} {{- if $.HasIndex .}}
{{.Name}} {{$.GetVariable .}}
{{- end}}
{{- end}}
)
var (
{{- range .Templates}}
{{- if $.HasIndex .}}{{- else}}
{{.Name}} *{{$.GetConfigName .}}
{{- end}}
{{- end}} {{- end}}
) )
var ( var (
{{- range .Templates}} {{- range .Templates}}
_{{.Name}} {{$.GetVariable .}} {{- if $.HasIndex .}}
_{{.Name}} {{$.GetVariable .}}
{{- end}}
{{- end}}
)
var (
{{- range .Templates}}
{{- if $.HasIndex .}}{{- else}}
_{{.Name}} *{{$.GetConfigName .}}
{{- end}}
{{- end}} {{- end}}
) )
@ -121,14 +137,21 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
mutex.Lock() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()
for _, sign := range signs { for _, sign := range signs {
{{- range .Templates}} switch sign {
temp := make({{$.GetVariable .}}) {{- range .Templates}}
if err := handle(sign, &temp, json);err != nil { case {{.Name}}Sign:
log.Error("Config", log.String("Name", "{{.Name}}"), log.Bool("Invalid", true), log.Err(err)) {{- if $.HasIndex .}}
}else { temp := make({{$.GetVariable .}})
_{{.Name}} = temp {{- else}}
} temp := new({{$.GetConfigName .}})
{{- end}} {{- end}}
if err := handle(sign, &temp, json);err != nil {
log.Error("Config", log.String("Name", "{{.Name}}"), log.Bool("Invalid", true), log.Err(err))
}else {
_{{.Name}} = temp
}
{{- end}}
}
} }
} }
@ -137,7 +160,11 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
switch sign { switch sign {
{{- range .Templates}} {{- range .Templates}}
case {{.Name}}Sign: case {{.Name}}Sign:
temp := make({{$.GetVariable .}}) {{- if $.HasIndex .}}
temp := make({{$.GetVariable .}})
{{- else}}
temp := new({{$.GetConfigName .}})
{{- end}}
if err := json.Unmarshal(data, &{{.Name}}); err != nil { if err := json.Unmarshal(data, &{{.Name}}); err != nil {
log.Error("Config", log.String("Name", "{{.Name}}"), log.Bool("Invalid", true), log.Err(err)) log.Error("Config", log.String("Name", "{{.Name}}"), log.Bool("Invalid", true), log.Err(err))
return return
@ -151,10 +178,24 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
func Refresh() { func Refresh() {
mutex.Lock() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()
cs := make(map[Sign]any)
{{- range .Templates}} {{- range .Templates}}
{{.Name}} = _{{.Name}} {{.Name}} = _{{.Name}}
_{{.Name}} = nil cs[{{.Name}}Sign] = {{.Name}}
{{- end}} {{- end}}
configs = cs
}
// GetConfigs 获取所有配置
func GetConfigs() map[Sign]any {
return configs
}
// GetConfigSigns 获取所有配置的标识
func GetConfigSigns() []Sign {
return signs
} }
`, slf) `, slf)
} }
@ -172,3 +213,7 @@ func (slf *Golang) GetVariable(config *pce.TmplStruct) string {
func (slf *Golang) GetConfigName(config *pce.TmplStruct) string { func (slf *Golang) GetConfigName(config *pce.TmplStruct) string {
return strings.ReplaceAll(config.Name, "Config", "Configuration") return strings.ReplaceAll(config.Name, "Config", "Configuration")
} }
func (slf *Golang) HasIndex(config *pce.TmplStruct) bool {
return config.IndexCount > 0
}