other: 配置导表优化
This commit is contained in:
parent
91b2b52fc8
commit
130869af4e
|
@ -8,16 +8,25 @@ import (
|
|||
"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{
|
||||
sheet: sheet,
|
||||
sheet: sheet,
|
||||
exportType: exportType,
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
// Xlsx 内置的 Xlsx 配置
|
||||
type Xlsx struct {
|
||||
sheet *xlsx.Sheet
|
||||
sheet *xlsx.Sheet
|
||||
exportType XlsxExportType
|
||||
}
|
||||
|
||||
func (slf *Xlsx) GetConfigName() string {
|
||||
|
@ -41,12 +50,13 @@ func (slf *Xlsx) GetIndexCount() int {
|
|||
}
|
||||
|
||||
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
|
||||
if desc == nil || name == nil || fieldType == nil || exportType == nil {
|
||||
return field, false
|
||||
}
|
||||
field = pce.DataField{
|
||||
Index: index,
|
||||
Name: strings.ReplaceAll(strings.ReplaceAll(str.FirstUpper(name.String()), "\r", ""), "\n", ""),
|
||||
Type: fieldType.String(),
|
||||
ExportType: exportType.String(),
|
||||
|
@ -65,13 +75,13 @@ func (slf *Xlsx) GetFields() []pce.DataField {
|
|||
var fields []pce.DataField
|
||||
if slf.GetIndexCount() > 0 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -86,32 +96,31 @@ func (slf *Xlsx) GetData() [][]pce.DataInfo {
|
|||
for y := 7; y < slf.getHeight(); y++ {
|
||||
var line []pce.DataInfo
|
||||
var stop bool
|
||||
for x := 0; x < slf.getWidth(); x++ {
|
||||
if prefixCell := slf.get(x, y); prefixCell != nil {
|
||||
prefix := prefixCell.String()
|
||||
if strings.HasPrefix(prefix, "#") {
|
||||
break
|
||||
}
|
||||
}
|
||||
if x == 0 {
|
||||
|
||||
if prefixCell := slf.get(0, y); prefixCell != nil {
|
||||
prefix := prefixCell.String()
|
||||
if strings.HasPrefix(prefix, "#") {
|
||||
continue
|
||||
}
|
||||
var isIndex = x-1 < slf.GetIndexCount()
|
||||
}
|
||||
|
||||
for i, field := range slf.GetFields() {
|
||||
var isIndex = i-1 < slf.GetIndexCount()
|
||||
|
||||
var value string
|
||||
if valueCell := slf.get(x, y); valueCell != nil {
|
||||
if valueCell := slf.get(field.Index, y); valueCell != nil {
|
||||
value = valueCell.String()
|
||||
} else if isIndex {
|
||||
stop = true
|
||||
break
|
||||
}
|
||||
valueCell := slf.get(x, y)
|
||||
valueCell := slf.get(field.Index, y)
|
||||
if valueCell == nil {
|
||||
break
|
||||
}
|
||||
if len(fields) > x-1 {
|
||||
if len(fields) > i-1 {
|
||||
line = append(line, pce.DataInfo{
|
||||
DataField: fields[x-1],
|
||||
DataField: field,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
|
@ -164,7 +173,15 @@ func (slf *Xlsx) get(x, y int) *xlsx.Cell {
|
|||
|
||||
func (slf *Xlsx) checkFieldInvalid(field pce.DataField) bool {
|
||||
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:
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ type Exporter struct{}
|
|||
|
||||
// ExportStruct 导出结构
|
||||
func (slf *Exporter) ExportStruct(tmpl Tmpl, tmplStruct ...*TmplStruct) ([]byte, error) {
|
||||
raw, err := tmpl.Render(tmplStruct)
|
||||
raw, err := tmpl.Render(tmplStruct...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package pce
|
||||
|
||||
import (
|
||||
"github.com/kercylan98/minotaur/utils/super"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
@ -20,6 +19,5 @@ type Field interface {
|
|||
func GetFieldGolangType(field Field) string {
|
||||
typeOf := reflect.TypeOf(field).Elem()
|
||||
kind := strings.ToLower(typeOf.Kind().String())
|
||||
name := strings.ToLower(typeOf.Name())
|
||||
return super.If(kind == name, kind, name)
|
||||
return kind
|
||||
}
|
||||
|
|
|
@ -26,13 +26,13 @@ type Loader struct {
|
|||
// LoadStruct 加载结构
|
||||
func (slf *Loader) LoadStruct(config Config) *TmplStruct {
|
||||
var tmpl = &TmplStruct{
|
||||
Name: str.FirstUpper(config.GetConfigName()),
|
||||
Desc: config.GetDescription(),
|
||||
Name: str.FirstUpper(config.GetConfigName()),
|
||||
Desc: config.GetDescription(),
|
||||
IndexCount: config.GetIndexCount(),
|
||||
}
|
||||
indexCount := config.GetIndexCount()
|
||||
for i, field := range config.GetFields() {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -167,6 +167,7 @@ type DataInfo struct {
|
|||
|
||||
// DataField 配置数据字段
|
||||
type DataField struct {
|
||||
Index int // 字段索引
|
||||
Name string // 字段名称
|
||||
Desc string // 字段描述
|
||||
Type string // 字段类型
|
||||
|
|
|
@ -3,5 +3,5 @@ package pce
|
|||
// Tmpl 配置结构模板接口
|
||||
type Tmpl interface {
|
||||
// Render 渲染模板
|
||||
Render(templates []*TmplStruct) (string, error)
|
||||
Render(templates ...*TmplStruct) (string, error)
|
||||
}
|
||||
|
|
|
@ -6,9 +6,10 @@ import (
|
|||
|
||||
// TmplStruct 模板结构
|
||||
type TmplStruct struct {
|
||||
Name string // 结构名称
|
||||
Desc string // 结构描述
|
||||
Fields []*TmplField // 字段列表
|
||||
Name string // 结构名称
|
||||
Desc string // 结构描述
|
||||
Fields []*TmplField // 字段列表
|
||||
IndexCount int // 索引数量
|
||||
}
|
||||
|
||||
// addField 添加字段
|
||||
|
@ -20,6 +21,8 @@ func (slf *TmplStruct) addField(parent, name, desc, fieldType string, fields map
|
|||
}
|
||||
if !hash.Exist(fields, fieldType) {
|
||||
field.setStruct(parent, name, desc, fieldType, fields)
|
||||
} else {
|
||||
field.Type = GetFieldGolangType(fields[fieldType])
|
||||
}
|
||||
slf.Fields = append(slf.Fields, field)
|
||||
return field
|
||||
|
|
|
@ -40,11 +40,7 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
|
|||
|
||||
var (
|
||||
json = jsonIter.ConfigCompatibleWithStandardLibrary
|
||||
configs map[Sign]any = map[Sign]any{
|
||||
{{- range .Templates}}
|
||||
{{.Name}}Sign: {{.Name}},
|
||||
{{- end}}
|
||||
}
|
||||
configs map[Sign]any
|
||||
signs = []Sign{
|
||||
{{- range .Templates}}
|
||||
{{.Name}}Sign,
|
||||
|
@ -55,13 +51,33 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
|
|||
|
||||
var (
|
||||
{{- range .Templates}}
|
||||
{{.Name}} {{$.GetVariable .}}
|
||||
{{- if $.HasIndex .}}
|
||||
{{.Name}} {{$.GetVariable .}}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
)
|
||||
|
||||
var (
|
||||
{{- range .Templates}}
|
||||
{{- if $.HasIndex .}}{{- else}}
|
||||
{{.Name}} *{{$.GetConfigName .}}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
)
|
||||
|
||||
var (
|
||||
{{- range .Templates}}
|
||||
_{{.Name}} {{$.GetVariable .}}
|
||||
{{- if $.HasIndex .}}
|
||||
_{{.Name}} {{$.GetVariable .}}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
)
|
||||
|
||||
var (
|
||||
{{- range .Templates}}
|
||||
{{- if $.HasIndex .}}{{- else}}
|
||||
_{{.Name}} *{{$.GetConfigName .}}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
)
|
||||
|
||||
|
@ -121,14 +137,21 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
|
|||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
for _, sign := range signs {
|
||||
{{- range .Templates}}
|
||||
temp := make({{$.GetVariable .}})
|
||||
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}}
|
||||
switch sign {
|
||||
{{- range .Templates}}
|
||||
case {{.Name}}Sign:
|
||||
{{- if $.HasIndex .}}
|
||||
temp := make({{$.GetVariable .}})
|
||||
{{- else}}
|
||||
temp := new({{$.GetConfigName .}})
|
||||
{{- 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 {
|
||||
{{- range .Templates}}
|
||||
case {{.Name}}Sign:
|
||||
temp := make({{$.GetVariable .}})
|
||||
{{- if $.HasIndex .}}
|
||||
temp := make({{$.GetVariable .}})
|
||||
{{- else}}
|
||||
temp := new({{$.GetConfigName .}})
|
||||
{{- end}}
|
||||
if err := json.Unmarshal(data, &{{.Name}}); err != nil {
|
||||
log.Error("Config", log.String("Name", "{{.Name}}"), log.Bool("Invalid", true), log.Err(err))
|
||||
return
|
||||
|
@ -151,10 +178,24 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
|
|||
func Refresh() {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
cs := make(map[Sign]any)
|
||||
|
||||
{{- range .Templates}}
|
||||
{{.Name}} = _{{.Name}}
|
||||
_{{.Name}} = nil
|
||||
cs[{{.Name}}Sign] = {{.Name}}
|
||||
{{- end}}
|
||||
|
||||
configs = cs
|
||||
}
|
||||
|
||||
// GetConfigs 获取所有配置
|
||||
func GetConfigs() map[Sign]any {
|
||||
return configs
|
||||
}
|
||||
|
||||
// GetConfigSigns 获取所有配置的标识
|
||||
func GetConfigSigns() []Sign {
|
||||
return signs
|
||||
}
|
||||
`, slf)
|
||||
}
|
||||
|
@ -172,3 +213,7 @@ func (slf *Golang) GetVariable(config *pce.TmplStruct) string {
|
|||
func (slf *Golang) GetConfigName(config *pce.TmplStruct) string {
|
||||
return strings.ReplaceAll(config.Name, "Config", "Configuration")
|
||||
}
|
||||
|
||||
func (slf *Golang) HasIndex(config *pce.TmplStruct) bool {
|
||||
return config.IndexCount > 0
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue