Compare commits
No commits in common. "fc340b9417cf2cfc0f48da06001ae5078e6aae3f" and "7e464374bb4ce426d96e77bb2dcdcc9964304f59" have entirely different histories.
fc340b9417
...
7e464374bb
@ -1,13 +1,12 @@
|
|||||||
package dynamicstruct
|
package dynamicstruct
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const tagTpl = `json:"{TAG_NAME}" xml:"{TAG_NAME}" toml:"{TAG_NAME}" yaml:"{TAG_NAME}" ini:"{TAG_NAME}"`
|
|
||||||
|
|
||||||
type nestedStruct struct {
|
type nestedStruct struct {
|
||||||
Field string
|
Field string
|
||||||
Builder IBuilder
|
Builder IBuilder
|
||||||
@ -70,7 +69,7 @@ func (b *builderImpl) AddField(name string, pkg string, typ any, tag string, ano
|
|||||||
if len(tag) == 0 {
|
if len(tag) == 0 {
|
||||||
arr := strings.Split(strings.TrimSuffix(name, ".[]"), ".")
|
arr := strings.Split(strings.TrimSuffix(name, ".[]"), ".")
|
||||||
// 没指定tag, 字段名称作为tag名称
|
// 没指定tag, 字段名称作为tag名称
|
||||||
tag = strings.ReplaceAll(tagTpl, "{TAG_NAME}", arr[len(arr)-1])
|
tag = strings.ReplaceAll(`json:"{TAG_NAME}" xml:"{TAG_NAME}" toml:"{TAG_NAME}" yaml:"{TAG_NAME}" ini:"{TAG_NAME}"`, "{TAG_NAME}", arr[len(arr)-1])
|
||||||
// tag = fmt.Sprintf(`json:"%s"`, name)
|
// tag = fmt.Sprintf(`json:"%s"`, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,8 +121,7 @@ func (b *builderImpl) addNestedField(nameArr []string, pkg string, typ any, tag
|
|||||||
parentName := strings.Join(nameArr[:i], ".")
|
parentName := strings.Join(nameArr[:i], ".")
|
||||||
parentJsonTag := nameArr[i-1]
|
parentJsonTag := nameArr[i-1]
|
||||||
parentFieldName := wrapper.String(parentJsonTag).SnakeCaseToCamel()
|
parentFieldName := wrapper.String(parentJsonTag).SnakeCaseToCamel()
|
||||||
// fieldTag := fmt.Sprintf(`json:"%s"`, parentJsonTag)
|
fieldTag := fmt.Sprintf(`json:"%s"`, parentJsonTag)
|
||||||
fieldTag := strings.ReplaceAll(tagTpl, "{TAG_NAME}", parentJsonTag)
|
|
||||||
if tagCfg, exist := b.structTagTable[parentName]; exist && len(tagCfg) > 0 {
|
if tagCfg, exist := b.structTagTable[parentName]; exist && len(tagCfg) > 0 {
|
||||||
fieldTag = tagCfg
|
fieldTag = tagCfg
|
||||||
}
|
}
|
||||||
|
@ -106,11 +106,6 @@ func (oj *ownJson) generateStructField(rootPath string, currentName string, curr
|
|||||||
if currentResult.IsArray() {
|
if currentResult.IsArray() {
|
||||||
// 数组, 递归处理
|
// 数组, 递归处理
|
||||||
arrList := currentResult.Array()
|
arrList := currentResult.Array()
|
||||||
if len(arrList) == 0 {
|
|
||||||
// 空数组,无法判断类型,使用any
|
|
||||||
oj.structBuilder.AddField(structPath, "", []any{}, "", false)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if arrList[0].Type == gjson.True || arrList[0].Type == gjson.False {
|
if arrList[0].Type == gjson.True || arrList[0].Type == gjson.False {
|
||||||
oj.structBuilder.AddField(structPath, "", []bool{}, "", false)
|
oj.structBuilder.AddField(structPath, "", []bool{}, "", false)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user