From 56d4e4bcea02256b92401529b8c39646e716e611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Sat, 13 Mar 2021 12:56:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E5=8D=A0?= =?UTF-8?q?=E4=BD=8D=E7=AC=A6=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json/build.go | 60 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/json/build.go b/json/build.go index 78bbaba..6d69e62 100644 --- a/json/build.go +++ b/json/build.go @@ -121,16 +121,12 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int return tplList, valList } - key := "\"" + root.Key + "\"" + // key := "\"" + root.Key + "\"" if !root.IsIndexNode { if len(root.Child) > 0 { *tplList = append(*tplList, dj.getStartSymbol(root)) } else { - if root.IsHasLastBrother { - *tplList = append(*tplList, key+":%v,") - } else { - *tplList = append(*tplList, key+":%v") - } + *tplList = append(*tplList, dj.getValFormat(root)) switch val := root.Value.(type) { case string: *valList = append(*valList, "\""+val+"\"") @@ -146,19 +142,9 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int switch val := root.Value.(type) { case string: *valList = append(*valList, val) - if root.IsHasLastBrother { - *tplList = append(*tplList, "\"%v\",") - } else { - *tplList = append(*tplList, "\"%v\"") - - } + *tplList = append(*tplList, dj.getValFormat(root)) default: - if root.IsHasLastBrother { - *tplList = append(*tplList, "%v,") - } else { - *tplList = append(*tplList, "%v") - - } + *tplList = append(*tplList, dj.getValFormat(root)) *valList = append(*valList, root.Value) } } else { @@ -174,6 +160,44 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int 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 计算起始的符号 // // Author : go_developer@163.com<张德满>