Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Xiaomi
soar
提交
d5708af2
S
soar
项目概览
Xiaomi
/
soar
大约 1 年 前同步成功
通知
388
Star
8512
Fork
1328
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
soar
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d5708af2
编写于
1月 08, 2019
作者:
martianzhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix JSONFind stack overflow problem
上级
f59e41f0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
348 addition
and
10 deletion
+348
-10
common/testdata/TestJSONFind.golden
common/testdata/TestJSONFind.golden
+8
-1
common/tricks.go
common/tricks.go
+28
-4
common/tricks_test.go
common/tricks_test.go
+312
-5
未找到文件。
common/testdata/TestJSONFind.golden
浏览文件 @
d5708af2
[Janet Elliotte Jason]
[McLaughlin Hunter Harold]
[{
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
}]
[ binary binary utf8mb4_bin ]
common/tricks.go
浏览文件 @
d5708af2
...
...
@@ -107,15 +107,39 @@ func SortedKey(m interface{}) []string {
return
keys
}
//
JSONFind iterate find name in js
on
func
JSONFind
(
json
string
,
name
string
,
result
*
[]
string
)
{
//
jsonFind internal functi
on
func
jsonFind
(
json
string
,
name
string
,
find
*
[]
string
)
(
next
[]
string
)
{
res
:=
gjson
.
Parse
(
json
)
res
.
ForEach
(
func
(
key
,
value
gjson
.
Result
)
bool
{
if
key
.
String
()
==
name
{
*
result
=
append
(
*
result
,
value
.
String
())
*
find
=
append
(
*
find
,
value
.
String
())
}
else
{
JSONFind
(
value
.
String
(),
name
,
result
)
switch
value
.
Type
{
case
gjson
.
Number
,
gjson
.
True
,
gjson
.
False
,
gjson
.
Null
:
default
:
next
=
append
(
next
,
value
.
String
())
}
}
return
true
// keep iterating
})
return
next
}
// JSONFind iterate find name in json
func
JSONFind
(
json
string
,
name
string
)
[]
string
{
var
find
[]
string
next
:=
[]
string
{
json
}
for
{
var
tmpNext
[]
string
for
_
,
subJSON
:=
range
next
{
for
_
,
tmp
:=
range
jsonFind
(
subJSON
,
name
,
&
find
)
{
tmpNext
=
append
(
tmpNext
,
tmp
)
}
}
next
=
tmpNext
if
len
(
next
)
==
0
{
break
}
}
return
find
}
common/tricks_test.go
浏览文件 @
d5708af2
...
...
@@ -59,21 +59,328 @@ func TestJSONFind(t *testing.T) {
"programmers": [
{
"firstName": "Janet",
"
lastNam
e": "McLaughlin",
"
Collat
e": "McLaughlin",
}, {
"firstName": "Elliotte",
"
lastNam
e": "Hunter",
"
Collat
e": "Hunter",
}, {
"firstName": "Jason",
"
lastNam
e": "Harold",
"
Collat
e": "Harold",
}
]
}`
,
`
{
"widget": {
"debug": "on",
"Collate": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
}
`
,
`
[
{
"SQLCache": true,
"CalcFoundRows": false,
"StraightJoin": false,
"Priority": 0,
"Distinct": false,
"From": {
"TableRefs": {
"Left": {
"Source": {
"Schema": {
"O": "",
"L": ""
},
"Name": {
"O": "tb",
"L": "tb"
},
"DBInfo": null,
"TableInfo": null,
"IndexHints": null
},
"AsName": {
"O": "",
"L": ""
}
},
"Right": null,
"Tp": 0,
"On": null,
"Using": null,
"NaturalJoin": false,
"StraightJoin": false
}
},
"Where": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Op": 4,
"L": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Op": 7,
"L": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Name": {
"Schema": {
"O": "",
"L": ""
},
"Table": {
"O": "",
"L": ""
},
"Name": {
"O": "col3",
"L": "col3"
}
},
"Refer": null
},
"R": {
"Type": {
"Tp": 8,
"Flag": 128,
"Flen": 1,
"Decimal": 0,
"Charset": "binary",
"Collate": "binary",
"Elems": null
}
}
},
"R": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Op": 1,
"L": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Op": 7,
"L": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Name": {
"Schema": {
"O": "",
"L": ""
},
"Table": {
"O": "",
"L": ""
},
"Name": {
"O": "col3",
"L": "col3"
}
},
"Refer": null
},
"R": {
"Type": {
"Tp": 8,
"Flag": 128,
"Flen": 1,
"Decimal": 0,
"Charset": "binary",
"Collate": "binary",
"Elems": null
}
}
},
"R": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Op": 7,
"L": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Op": 7,
"L": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Name": {
"Schema": {
"O": "",
"L": ""
},
"Table": {
"O": "",
"L": ""
},
"Name": {
"O": "col1",
"L": "col1"
}
},
"Refer": null
},
"R": {
"Type": {
"Tp": 0,
"Flag": 0,
"Flen": 0,
"Decimal": 0,
"Charset": "",
"Collate": "",
"Elems": null
},
"Name": {
"Schema": {
"O": "",
"L": ""
},
"Table": {
"O": "",
"L": ""
},
"Name": {
"O": "col2",
"L": "col2"
}
},
"Refer": null
}
},
"R": {
"Type": {
"Tp": 253,
"Flag": 0,
"Flen": 3,
"Decimal": -1,
"Charset": "utf8mb4",
"Collate": "utf8mb4_bin",
"Elems": null
}
}
}
}
},
"Fields": {
"Fields": [
{
"Offset": 7,
"WildCard": {
"Table": {
"O": "",
"L": ""
},
"Schema": {
"O": "",
"L": ""
}
},
"Expr": null,
"AsName": {
"O": "",
"L": ""
},
"Auxiliary": false
}
]
},
"GroupBy": null,
"Having": null,
"WindowSpecs": null,
"OrderBy": null,
"Limit": null,
"LockTp": 0,
"TableHints": null,
"IsAfterUnionDistinct": false,
"IsInBraces": false
}
]
`
,
}
err
:=
GoldenDiff
(
func
()
{
for
_
,
json
:=
range
jsons
{
var
result
[]
string
JSONFind
(
json
,
"firstName"
,
&
result
)
result
:=
JSONFind
(
json
,
"Collate"
)
fmt
.
Println
(
result
)
}
},
t
.
Name
(),
update
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录