From a1a1b1d2e0f783f077388125dddf35bfff4f7e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 30 Mar 2023 16:28:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20in=20=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- go.sum | 4 ++++ tree/generate.go | 22 +++++++++++++++------- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 3d58812..eeb28e4 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20220627070212-c590a0a1c216 - git.zhangdeman.cn/zhangdeman/util v0.0.0-20230211164227-256094968151 + git.zhangdeman.cn/zhangdeman/util v0.0.0-20230330082619-662152cb682d github.com/Jeffail/gabs v1.4.0 github.com/pkg/errors v0.9.1 github.com/smartystreets/goconvey v1.7.2 diff --git a/go.sum b/go.sum index b28f983..fc1eeab 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,10 @@ git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20220627070212-c590a0a1c216/go.mod git.zhangdeman.cn/zhangdeman/util v0.0.0-20220626081130-cbac0b676fb8/go.mod h1:G2/OKMbEn89d+YUXQtv9Nlh0LGg14pOqDnbOgBTTRXY= git.zhangdeman.cn/zhangdeman/util v0.0.0-20230211164227-256094968151 h1:j537bRLQL1FlkdXTIaT9Ecjx5eogkPsGiTOWIEFQlc8= git.zhangdeman.cn/zhangdeman/util v0.0.0-20230211164227-256094968151/go.mod h1:SyRTkOz6gxUVn3S/Qtkf+rhKV0I1ym8lwsT8YjggYFs= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20230330065032-faba0a5a9ea1 h1:u2FdNfcGvRmlKpuPBk2qvYenkZjinHY2PLu5Wmhka8A= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20230330065032-faba0a5a9ea1/go.mod h1:SyRTkOz6gxUVn3S/Qtkf+rhKV0I1ym8lwsT8YjggYFs= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20230330082619-662152cb682d h1:kMQZmkYBceHM3O7wiCelSADjTyOF3EBxXTX8fgZA+6c= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20230330082619-662152cb682d/go.mod h1:qeVsrMae8ljqzcsmI+lWPU/4Rdjb9cOt4oaDUNEf1Ck= github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo= github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= diff --git a/tree/generate.go b/tree/generate.go index aa8b844..0aa7d02 100644 --- a/tree/generate.go +++ b/tree/generate.go @@ -9,6 +9,7 @@ package tree import ( "fmt" + "git.zhangdeman.cn/zhangdeman/util" "strings" "github.com/pkg/errors" @@ -27,6 +28,7 @@ func New(jsonData string) (*Generate, error) { } g.root = NewVirtualNode() g.currentNode = g.root + g.currentParentNode = g.root err := g.init() return g, err } @@ -86,7 +88,7 @@ func (g *Generate) init() error { fmt.Println(jsonKey, valueType, jsonValue, startIndex) // 创建节点, 并挂在到树上 var newNode *Node - if g.currentNode.ValueType == ValueTypeArray || g.currentNode.ValueType == ValueTypeMap { + if util.Array.In(g.currentNode.ValueType, []string{ValueTypeArray, ValueTypeMap}) >= 0 { newNode = NewNode(jsonKey, jsonValue, valueType, g.currentNode) g.currentParentNode = g.currentNode g.currentParentNode.SonNodeList = append(g.currentParentNode.SonNodeList, newNode) @@ -104,11 +106,11 @@ func (g *Generate) init() error { // // Date : 11:36 2023/3/29 func (g *Generate) getKey(startIndex int) (string, int, error) { - keyCharList := []string{} + keyCharList := make([]string, 0) hasStart := false for startIndex < len(g.jsonDataByte) { charStr := string(g.jsonDataByte[startIndex]) - if charStr == KeywordSpace { + if charStr == KeywordSpace && !hasStart { // 跳过空格 startIndex++ continue @@ -127,7 +129,12 @@ func (g *Generate) getKey(startIndex int) (string, int, error) { } if !hasStart { - if charStr == KeywordDoubleQuote || charStr == KeywordObjectStart || charStr == KeywordArrayStart || charStr == KeywordComma { + if util.Array.In(charStr, []string{ + KeywordDoubleQuote, + KeywordObjectStart, + KeywordArrayStart, + KeywordComma, + }) >= 0 { startIndex++ continue } @@ -252,18 +259,18 @@ func (g *Generate) getValue(startIndex int) (string, int, error) { isStart := false for startIndex < len(g.jsonDataByte) { str := string(g.jsonDataByte[startIndex]) - if str == KeywordSpace { + if str == KeywordSpace && !isStart { startIndex++ continue } if !isStart { - if str == KeywordArrayEnd || str == KeywordObjectEnd || str == KeywordComma { + if util.Array.In(str, []string{KeywordArrayEnd, KeywordObjectEnd, KeywordComma}) >= 0 { startIndex++ continue } } if isStart { - if str == KeywordDoubleQuote || str == KeywordArrayEnd || str == KeywordObjectEnd || str == KeywordComma { + if util.Array.In(str, []string{KeywordDoubleQuote, KeywordArrayEnd, KeywordObjectEnd, KeywordComma}) >= 0 { // 值的拼接已结束 startIndex++ break @@ -275,6 +282,7 @@ func (g *Generate) getValue(startIndex int) (string, int, error) { break } else { startIndex++ + isStart = true continue } }