已知问题修复 #7
| @ -1,12 +1,13 @@ | |||||||
| 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 | ||||||
| @ -69,7 +70,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(`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) | 			// 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], ".") | 		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,6 +106,11 @@ 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 | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user