拆分获取左右括号的逻辑

This commit is contained in:
白茶清欢 2021-03-13 12:37:38 +08:00
parent df00e13602
commit 5ac11da46a

View File

@ -124,15 +124,7 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int
key := "\"" + root.Key + "\"" key := "\"" + root.Key + "\""
if !root.IsIndexNode { if !root.IsIndexNode {
if len(root.Child) > 0 { if len(root.Child) > 0 {
if root.IsRoot { *tplList = append(*tplList, dj.getStartSymbol(root))
*tplList = append(*tplList, "{")
} else {
if root.IsSlice {
*tplList = append(*tplList, key+":[")
} else {
*tplList = append(*tplList, key+":{")
}
}
} else { } else {
if root.IsHasLastBrother { if root.IsHasLastBrother {
*tplList = append(*tplList, key+":%v,") *tplList = append(*tplList, key+":%v,")
@ -177,29 +169,66 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int
for _, node := range root.Child { for _, node := range root.Child {
dj.buildTpl(node, tplList, valList) dj.buildTpl(node, tplList, valList)
} }
if !root.IsIndexNode { *tplList = append(*tplList, dj.getEndSymbol(root))
if root.IsHasLastBrother {
*tplList = append(*tplList, "},")
} else {
if root.IsSlice {
*tplList = append(*tplList, "]")
} else {
*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
} }
// getStartSymbol 计算起始的符号
//
// Author : go_developer@163.com<张德满>
//
// Date : 12:21 下午 2021/3/13
func (dj *DynamicJSON) getStartSymbol(root *JSONode) string {
if root.IsRoot {
return "{"
}
key := fmt.Sprintf("\"%s\"", root.Key)
if !root.IsIndexNode {
if len(root.Child) > 0 {
if root.IsSlice {
return key + ":["
} else {
return key + ":{"
}
}
return ""
}
if len(root.Child) > 0 {
return "{"
}
return ""
}
// getEndSymbol 计算结束的符号
//
// Author : go_developer@163.com<张德满>
//
// Date : 12:21 下午 2021/3/13
func (dj *DynamicJSON) getEndSymbol(root *JSONode) string {
if !root.IsIndexNode {
if root.IsHasLastBrother {
return "},"
}
if root.IsSlice {
return "]"
} else {
return "}"
}
}
if len(root.Child) > 0 {
if root.IsHasLastBrother {
return "},"
}
return "}"
}
return ""
}
// Search 搜索一个key TODO : 优化 // Search 搜索一个key TODO : 优化
// //
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>