From d139b409fd3d67c876644312b1c7b9d2f9ff398f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 29 Apr 2025 11:37:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=9C=AA=E5=88=A4=E6=96=AD=E9=95=BF=E5=BA=A6=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E8=B6=8A=E7=95=8C=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wrapper/json.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wrapper/json.go b/wrapper/json.go index edc0aa3..37b86f9 100644 --- a/wrapper/json.go +++ b/wrapper/json.go @@ -106,6 +106,11 @@ func (oj *ownJson) generateStructField(rootPath string, currentName string, curr if currentResult.IsArray() { // 数组, 递归处理 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 { oj.structBuilder.AddField(structPath, "", []bool{}, "", false) return -- 2.36.6 From 141a376d198ce3e26a8e982e40f133a5901bd00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 29 Apr 2025 14:57:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B5=8C=E5=A5=97?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1json=20tag=E8=AE=BE=E7=BD=AE=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- builder.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/builder.go b/builder.go index ad64070..a820fbb 100644 --- a/builder.go +++ b/builder.go @@ -1,12 +1,13 @@ package dynamicstruct import ( - "fmt" "git.zhangdeman.cn/zhangdeman/wrapper" "reflect" "strings" ) +const tagTpl = `json:"{TAG_NAME}" xml:"{TAG_NAME}" toml:"{TAG_NAME}" yaml:"{TAG_NAME}" ini:"{TAG_NAME}"` + type nestedStruct struct { Field string Builder IBuilder @@ -69,7 +70,7 @@ func (b *builderImpl) AddField(name string, pkg string, typ any, tag string, ano if len(tag) == 0 { arr := strings.Split(strings.TrimSuffix(name, ".[]"), ".") // 没指定tag, 字段名称作为tag名称 - 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 = strings.ReplaceAll(tagTpl, "{TAG_NAME}", arr[len(arr)-1]) // tag = fmt.Sprintf(`json:"%s"`, name) } } @@ -121,7 +122,8 @@ func (b *builderImpl) addNestedField(nameArr []string, pkg string, typ any, tag parentName := strings.Join(nameArr[:i], ".") parentJsonTag := nameArr[i-1] 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 { fieldTag = tagCfg } -- 2.36.6