修复 in 判断的BUG

This commit is contained in:
2023-03-30 16:28:10 +08:00
parent 4629e31171
commit a1a1b1d2e0
3 changed files with 20 additions and 8 deletions

View File

@ -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
}
}