支持非整齐的list

This commit is contained in:
白茶清欢 2021-03-12 23:28:28 +08:00
parent 5939795603
commit df00e13602
2 changed files with 42 additions and 8 deletions

View File

@ -148,12 +148,36 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int
return tplList, valList return tplList, valList
} }
} else {
if len(root.Child) == 0 {
switch val := root.Value.(type) {
case string:
*valList = append(*valList, val)
if root.IsHasLastBrother {
*tplList = append(*tplList, "\"%v\",")
} else {
*tplList = append(*tplList, "\"%v\"")
}
default:
if root.IsHasLastBrother {
*tplList = append(*tplList, "%v,")
} else {
*tplList = append(*tplList, "%v")
}
*valList = append(*valList, root.Value)
}
} else { } else {
*tplList = append(*tplList, "{") *tplList = append(*tplList, "{")
} }
}
for _, node := range root.Child { for _, node := range root.Child {
dj.buildTpl(node, tplList, valList) dj.buildTpl(node, tplList, valList)
} }
if !root.IsIndexNode {
if root.IsHasLastBrother { if root.IsHasLastBrother {
*tplList = append(*tplList, "},") *tplList = append(*tplList, "},")
} else { } else {
@ -161,9 +185,18 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int
*tplList = append(*tplList, "]") *tplList = append(*tplList, "]")
} else { } else {
*tplList = append(*tplList, "}") *tplList = append(*tplList, "}")
}
}
} else {
if len(root.Child) > 0 {
if root.IsHasLastBrother {
*tplList = append(*tplList, "},")
} else {
*tplList = append(*tplList, "}")
}
}
}
}
}
return tplList, valList return tplList, valList
} }

View File

@ -26,5 +26,6 @@ func TestJSON(t *testing.T) {
tree.SetValue("slice.[0].name", "zhang") tree.SetValue("slice.[0].name", "zhang")
tree.SetValue("slice.[1].name", "de") tree.SetValue("slice.[1].name", "de")
tree.SetValue("slice.[2].name", "man") tree.SetValue("slice.[2].name", "man")
tree.SetValue("slice.[3]", "zhangdeman")
fmt.Println(tree.String()) fmt.Println(tree.String())
} }