diff --git a/builder.go b/builder.go index 95a44ff..3662954 100644 --- a/builder.go +++ b/builder.go @@ -1,6 +1,10 @@ package dynamicstruct -import "reflect" +import ( + "fmt" + "reflect" + "strings" +) type ( // Builder 运行时动态生成结构体的接口约束 @@ -86,8 +90,28 @@ func MergeStructs(values ...any) Builder { return builder } +// IsNestedField 判断是否是嵌套结构体 +func (b *builderImpl) parseNestedField(fieldName string) ([]string, bool) { + fieldNameArr := strings.Split(strings.Trim(fieldName, "."), ".") + return fieldNameArr, len(fieldNameArr) > 1 +} + // AddField 添加结构体字段 func (b *builderImpl) AddField(name string, pkg string, typ any, tag string, anonymous bool) Builder { + // 判断是否嵌套结构体 + fieldNameArr, isNestedField := b.parseNestedField(name) + if !isNestedField { + // 普通字段 + b.addNormalField(name, pkg, typ, tag, anonymous) + return b + } + // TODO : 添加嵌套的结构体 + fmt.Println(fieldNameArr) + return b +} + +// addNormalField 添加普通无嵌套的字段 +func (b *builderImpl) addNormalField(name string, pkg string, typ any, tag string, anonymous bool) Builder { if existFieldCfg := b.GetField(name); nil != existFieldCfg { // 说明已存在指定名称字段 // 重复添加, 则会议后面的标签以及类型, 覆盖前面的值 @@ -102,7 +126,6 @@ func (b *builderImpl) AddField(name string, pkg string, typ any, tag string, ano anonymous: anonymous, pkg: pkg, }) - return b } // RemoveField 根据名称移除结构体字段