优化数据占位符的处理

This commit is contained in:
白茶清欢 2021-03-13 12:56:55 +08:00
parent 5ac11da46a
commit 56d4e4bcea

View File

@ -121,16 +121,12 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int
return tplList, valList return tplList, valList
} }
key := "\"" + root.Key + "\"" // key := "\"" + root.Key + "\""
if !root.IsIndexNode { if !root.IsIndexNode {
if len(root.Child) > 0 { if len(root.Child) > 0 {
*tplList = append(*tplList, dj.getStartSymbol(root)) *tplList = append(*tplList, dj.getStartSymbol(root))
} else { } else {
if root.IsHasLastBrother { *tplList = append(*tplList, dj.getValFormat(root))
*tplList = append(*tplList, key+":%v,")
} else {
*tplList = append(*tplList, key+":%v")
}
switch val := root.Value.(type) { switch val := root.Value.(type) {
case string: case string:
*valList = append(*valList, "\""+val+"\"") *valList = append(*valList, "\""+val+"\"")
@ -146,19 +142,9 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int
switch val := root.Value.(type) { switch val := root.Value.(type) {
case string: case string:
*valList = append(*valList, val) *valList = append(*valList, val)
if root.IsHasLastBrother { *tplList = append(*tplList, dj.getValFormat(root))
*tplList = append(*tplList, "\"%v\",")
} else {
*tplList = append(*tplList, "\"%v\"")
}
default: default:
if root.IsHasLastBrother { *tplList = append(*tplList, dj.getValFormat(root))
*tplList = append(*tplList, "%v,")
} else {
*tplList = append(*tplList, "%v")
}
*valList = append(*valList, root.Value) *valList = append(*valList, root.Value)
} }
} else { } else {
@ -174,6 +160,44 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int
return tplList, valList return tplList, valList
} }
// getValFormat 构建值得占位符
//
// Author : go_developer@163.com<张德满>
//
// Date : 12:49 下午 2021/3/13
func (dj *DynamicJSON) getValFormat(root *JSONode) string {
key := fmt.Sprintf("\"%s\"", root.Key)
if !root.IsIndexNode {
if len(root.Child) > 0 {
// 还有自节点的情况下,不需要占位符
return ""
}
if root.IsHasLastBrother {
return key + ":%v,"
}
return key + ":%v"
}
if len(root.Child) > 0 {
// 是list的索引节点,且有子节点
return ""
}
switch root.Value.(type) {
case string:
if root.IsHasLastBrother {
return "\"%v\","
} else {
return "\"%v\""
}
default:
if root.IsHasLastBrother {
return "%v,"
}
return "%v"
}
return ""
}
// getStartSymbol 计算起始的符号 // getStartSymbol 计算起始的符号
// //
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>