feat: 重构 config 和 configexport 包
- 配置加载包 config 更名为 configuration - 配置导出包 configexport 更名为 pce - 重构 config 包加载方式,采用加载器的方式,并且支持多加载器 - 重构 configexport 包,支持通过实现模板的方式导出不同格式的数据文件及结构文件
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
package pce
|
||||
|
||||
import (
|
||||
"github.com/kercylan98/minotaur/utils/str"
|
||||
"github.com/tidwall/gjson"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NewLoader 创建加载器
|
||||
// - 加载器被用于加载配置表的数据和结构信息
|
||||
func NewLoader() *Loader {
|
||||
return &Loader{
|
||||
fields: make(map[string]Field),
|
||||
}
|
||||
}
|
||||
|
||||
// Loader 配置加载器
|
||||
type Loader struct {
|
||||
fields map[string]Field
|
||||
}
|
||||
|
||||
// BindField 绑定字段
|
||||
func (slf *Loader) BindField(fields ...Field) {
|
||||
for _, field := range fields {
|
||||
slf.fields[field.TypeName()] = field
|
||||
}
|
||||
}
|
||||
|
||||
// LoadStruct 加载结构
|
||||
func (slf *Loader) LoadStruct(config Config) *TmplStruct {
|
||||
var tmpl = &TmplStruct{
|
||||
Name: str.FirstUpper(config.GetConfigName()),
|
||||
Desc: config.GetDescription(),
|
||||
}
|
||||
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 {
|
||||
f.isIndex = true
|
||||
}
|
||||
}
|
||||
return tmpl
|
||||
}
|
||||
|
||||
// LoadData 加载配置并得到配置数据
|
||||
func (slf *Loader) LoadData(config Config) map[any]any {
|
||||
var source = make(map[any]any)
|
||||
var action = source
|
||||
var indexCount = config.GetIndexCount() - 1
|
||||
for _, row := range config.GetData() {
|
||||
var item = make(map[any]any)
|
||||
for index, field := range row {
|
||||
bind, exist := slf.fields[field.Type]
|
||||
var value any
|
||||
if exist {
|
||||
if len(field.Value) == 0 {
|
||||
value = bind.Zero()
|
||||
} else {
|
||||
value = bind.Parse(field.Value)
|
||||
}
|
||||
} else {
|
||||
value = slf.structInterpreter(field.Type, field.Value)
|
||||
}
|
||||
|
||||
if value != nil {
|
||||
item[field.Name] = value
|
||||
}
|
||||
|
||||
if index < indexCount {
|
||||
m, exist := action[value]
|
||||
if !exist {
|
||||
m = map[any]any{}
|
||||
action[value] = m
|
||||
action = m.(map[any]any)
|
||||
} else {
|
||||
action = m.(map[any]any)
|
||||
}
|
||||
} else if index == indexCount {
|
||||
action[value] = item
|
||||
}
|
||||
}
|
||||
action = source
|
||||
}
|
||||
|
||||
return source
|
||||
}
|
||||
|
||||
// sliceInterpreter 切片解释器
|
||||
func (slf *Loader) sliceInterpreter(fieldType, fieldValue string) any {
|
||||
if !strings.HasPrefix(fieldType, "[]") {
|
||||
return nil
|
||||
}
|
||||
t := strings.TrimPrefix(fieldType, "[]")
|
||||
var data = map[any]any{}
|
||||
gjson.ForEachLine(fieldValue, func(line gjson.Result) bool {
|
||||
line.ForEach(func(key, value gjson.Result) bool {
|
||||
field, exist := slf.fields[t]
|
||||
if exist {
|
||||
data[len(data)] = field.Parse(value.String())
|
||||
} else {
|
||||
data[len(data)] = slf.structInterpreter(t, value.String())
|
||||
}
|
||||
return true
|
||||
})
|
||||
return true
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
// structInterpreter 结构体解释器
|
||||
// - {id:int,name:string,info:{lv:int,exp:int}}
|
||||
func (slf *Loader) structInterpreter(fieldType, fieldValue string) any {
|
||||
if strings.HasPrefix(fieldType, "[]") {
|
||||
return slf.sliceInterpreter(fieldType, fieldValue)
|
||||
} else if !strings.HasPrefix(fieldType, "{") || !strings.HasSuffix(fieldType, "}") {
|
||||
return nil
|
||||
}
|
||||
var s = strings.TrimSuffix(strings.TrimPrefix(fieldType, "{"), "}")
|
||||
var data = map[any]any{}
|
||||
var fields []string
|
||||
var field string
|
||||
var leftBrackets []int
|
||||
for i, c := range s {
|
||||
switch c {
|
||||
case ',':
|
||||
if len(leftBrackets) == 0 {
|
||||
fields = append(fields, field)
|
||||
field = ""
|
||||
} else {
|
||||
field += string(c)
|
||||
}
|
||||
case '{':
|
||||
leftBrackets = append(leftBrackets, i)
|
||||
field += string(c)
|
||||
case '}':
|
||||
leftBrackets = leftBrackets[:len(leftBrackets)-1]
|
||||
field += string(c)
|
||||
if len(leftBrackets) == 0 {
|
||||
fields = append(fields, field)
|
||||
field = ""
|
||||
}
|
||||
default:
|
||||
field += string(c)
|
||||
}
|
||||
}
|
||||
if len(field) > 0 {
|
||||
fields = append(fields, field)
|
||||
}
|
||||
|
||||
for _, fieldInfo := range fields {
|
||||
fieldName, fieldType := str.KV(strings.TrimSpace(fieldInfo), ":")
|
||||
field, exist := slf.fields[fieldType]
|
||||
if exist {
|
||||
data[fieldName] = field.Parse(gjson.Get(fieldValue, fieldName).String())
|
||||
} else {
|
||||
data[fieldName] = slf.structInterpreter(fieldType, gjson.Get(fieldValue, fieldName).String())
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// dataInfo 配置数据
|
||||
type dataInfo struct {
|
||||
dataField // 字段
|
||||
Value string // 字段值
|
||||
}
|
||||
|
||||
// dataField 配置数据字段
|
||||
type dataField struct {
|
||||
Name string // 字段名称
|
||||
Desc string // 字段描述
|
||||
Type string // 字段类型
|
||||
ExportType string // 导出类型
|
||||
}
|
||||
Reference in New Issue
Block a user