支持嵌套结构体解析!!!!!!
This commit is contained in:
83
builder.go
83
builder.go
@ -1,6 +1,8 @@
|
||||
package dynamicstruct
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
@ -42,8 +44,16 @@ type (
|
||||
NewMapOfStructs(key any) any
|
||||
}
|
||||
|
||||
nestedStruct struct {
|
||||
Field string
|
||||
Builder Builder
|
||||
JsonTag string
|
||||
}
|
||||
|
||||
builderImpl struct {
|
||||
fields []*fieldConfigImpl
|
||||
fields []*fieldConfigImpl
|
||||
nestedStructTable map[string]nestedStruct // 嵌套结构体的定义, 父级路径 => 父级路径下挂接的子路径
|
||||
maxFieldDeep int // 字段嵌套最大深度
|
||||
}
|
||||
|
||||
fieldConfigImpl struct {
|
||||
@ -55,14 +65,16 @@ type (
|
||||
}
|
||||
|
||||
dynamicStructImpl struct {
|
||||
definition reflect.Type
|
||||
structFields []reflect.StructField
|
||||
definition reflect.Type
|
||||
}
|
||||
)
|
||||
|
||||
// NewStruct 获取builder实例
|
||||
func NewStruct() Builder {
|
||||
return &builderImpl{
|
||||
fields: []*fieldConfigImpl{},
|
||||
fields: []*fieldConfigImpl{},
|
||||
nestedStructTable: make(map[string]nestedStruct),
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,6 +109,8 @@ func (b *builderImpl) parseNestedField(fieldName string) ([]string, bool) {
|
||||
|
||||
// AddField 添加结构体字段
|
||||
func (b *builderImpl) AddField(name string, pkg string, typ any, tag string, anonymous bool) Builder {
|
||||
// 瞎话线转驼峰, 传入的name实际对应序列化时的json tag
|
||||
// name = wrapper.String(name).SnakeCaseToCamel()
|
||||
// 判断是否嵌套结构体
|
||||
fieldNameArr, isNestedField := b.parseNestedField(name)
|
||||
if !isNestedField {
|
||||
@ -104,7 +118,7 @@ func (b *builderImpl) AddField(name string, pkg string, typ any, tag string, ano
|
||||
b.addNormalField(name, pkg, typ, tag, anonymous)
|
||||
return b
|
||||
}
|
||||
// TODO : 添加嵌套的结构体
|
||||
// 添加嵌套的结构体
|
||||
b.addNestedField(fieldNameArr, pkg, typ, tag, anonymous)
|
||||
return b
|
||||
}
|
||||
@ -129,6 +143,36 @@ func (b *builderImpl) addNormalField(name string, pkg string, typ any, tag strin
|
||||
|
||||
// addNestedField 添加嵌套字段
|
||||
func (b *builderImpl) addNestedField(nameArr []string, pkg string, typ any, tag string, anonymous bool) {
|
||||
if len(nameArr) == 1 {
|
||||
// 说明已经是最顶层结构了
|
||||
b.addNormalField(nameArr[0], pkg, typ, tag, anonymous)
|
||||
return
|
||||
}
|
||||
if len(nameArr) > b.maxFieldDeep {
|
||||
// 设置字段嵌套的最大深度, 由于生成结构体确认深度使用
|
||||
b.maxFieldDeep = len(nameArr)
|
||||
}
|
||||
for i := len(nameArr) - 1; i > 0; i-- {
|
||||
jsonTag := nameArr[i]
|
||||
fieldName := wrapper.String(jsonTag).SnakeCaseToCamel()
|
||||
parentName := strings.Join(nameArr[:i], ".")
|
||||
parentJsonTag := nameArr[i-1]
|
||||
parentFieldName := wrapper.String(parentJsonTag).SnakeCaseToCamel()
|
||||
if len(parentName) > 0 {
|
||||
if _, exist := b.nestedStructTable[parentName]; !exist {
|
||||
b.nestedStructTable[parentName] = nestedStruct{
|
||||
Field: parentFieldName,
|
||||
Builder: NewStruct(),
|
||||
JsonTag: parentJsonTag,
|
||||
}
|
||||
}
|
||||
}
|
||||
if i == len(nameArr)-1 {
|
||||
// 最深层此字段, 直接追加到他的父级结构体中即可
|
||||
b.nestedStructTable[parentName].Builder.AddField(fieldName, pkg, typ, tag, anonymous)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveField 根据名称移除结构体字段
|
||||
@ -166,8 +210,33 @@ func (b *builderImpl) GetField(name string) FieldConfig {
|
||||
|
||||
// Build 构建动态结构体
|
||||
func (b *builderImpl) Build() DynamicStruct {
|
||||
// 按照嵌套深度, 进行一次回溯处理
|
||||
for deep := b.maxFieldDeep - 1; deep > 0; deep-- {
|
||||
// 嵌套数据结构
|
||||
for parentIndex, builderCfg := range b.nestedStructTable {
|
||||
parentNameArr := strings.Split(parentIndex, ".")
|
||||
if len(parentNameArr) != deep {
|
||||
// 从深度最深处向上处理
|
||||
continue
|
||||
}
|
||||
/*if deep == b.maxFieldDeep-1 {
|
||||
// 说明最深层嵌套结构
|
||||
parentJsonTag := parentNameArr[deep-1]
|
||||
parentField := wrapper.String(parentJsonTag).SnakeCaseToCamel()
|
||||
b.AddField(parentField, "", builderCfg.Builder.Build().New(), fmt.Sprintf(`json:"%v"`, parentJsonTag), false)
|
||||
continue
|
||||
}*/
|
||||
if deep == 1 {
|
||||
// 说明是顶层了
|
||||
b.AddField(builderCfg.Field, "", builderCfg.Builder.Build().New(), fmt.Sprintf(`json:"%v"`, builderCfg.JsonTag), false)
|
||||
} else {
|
||||
// (非顶层) 父级结构存在, 将其追加到父级结构中即可, 向前看一步即为父级结构
|
||||
b.nestedStructTable[strings.Join(parentNameArr[:len(parentNameArr)-1], ".")].Builder.AddField(builderCfg.Field, "", builderCfg.Builder.Build().New(), fmt.Sprintf(`json:"%v"`, builderCfg.JsonTag), false)
|
||||
}
|
||||
}
|
||||
}
|
||||
// 一级字段属性
|
||||
var structFields []reflect.StructField
|
||||
|
||||
for _, field := range b.fields {
|
||||
structFields = append(structFields, reflect.StructField{
|
||||
Name: field.name,
|
||||
@ -177,9 +246,9 @@ func (b *builderImpl) Build() DynamicStruct {
|
||||
Anonymous: field.anonymous,
|
||||
})
|
||||
}
|
||||
|
||||
return &dynamicStructImpl{
|
||||
definition: reflect.StructOf(structFields),
|
||||
structFields: structFields,
|
||||
definition: reflect.StructOf(structFields),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user