Skip to content

Commit 019bd6f

Browse files
committed
添加解析JSON
1 parent 4031952 commit 019bd6f

File tree

3 files changed

+349
-31
lines changed

3 files changed

+349
-31
lines changed

array/array.go

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package array
22

33
import (
4+
"encoding/json"
45
"reflect"
56
"strconv"
67
"strings"
78
)
89

9-
var defaultArray = New()
10-
1110
/**
1211
* 获取数组数据 / array struct
1312
*
@@ -17,18 +16,25 @@ var defaultArray = New()
1716
type Array struct {
1817
// 分隔符 / key Delim
1918
keyDelim string
19+
20+
// 原始数据 / source data
21+
source any
2022
}
2123

22-
// 构造函数 / NewArray
23-
func NewArray() Array {
24+
// 构造函数 / New
25+
func New(source any) Array {
2426
return Array{
2527
keyDelim: ".",
28+
source: source,
2629
}
2730
}
2831

29-
// 构造函数 / New array
30-
func New() Array {
31-
return NewArray()
32+
// 解析 JSON 数据 / parse json data
33+
func ParseJSON(source []byte) (Array, error) {
34+
var dst any
35+
err := json.Unmarshal(source, &dst)
36+
37+
return New(dst), err
3238
}
3339

3440
// 设置 keyDelim
@@ -41,8 +47,8 @@ func (this Array) WithKeyDelim(data string) Array {
4147

4248
// 判断是否存在
4349
// if key in source return true or false
44-
func (this Array) Exists(source any, key string) bool {
45-
if this.Find(source, key) != nil {
50+
func (this Array) Exists(key string) bool {
51+
if this.Find(key) != nil {
4652
return true
4753
}
4854

@@ -52,13 +58,13 @@ func (this Array) Exists(source any, key string) bool {
5258
// 判断是否存在
5359
// if key in source return true or false
5460
func Exists(source any, key string) bool {
55-
return defaultArray.Exists(source, key)
61+
return New(source).Exists(key)
5662
}
5763

5864
// 获取
5965
// get key data from source with default value
60-
func (this Array) Get(source any, key string, defVal ...any) any {
61-
data := this.Find(source, key)
66+
func (this Array) Get(key string, defVal ...any) any {
67+
data := this.Find(key)
6268
if data != nil {
6369
return data
6470
}
@@ -73,16 +79,39 @@ func (this Array) Get(source any, key string, defVal ...any) any {
7379
// 获取
7480
// get key data from source with default value
7581
func Get(source any, key string, defVal ...any) any {
76-
return defaultArray.Get(source, key, defVal...)
82+
return New(source).Get(key, defVal...)
83+
}
84+
85+
// 查找
86+
// find key data from source
87+
func (this Array) Find(key string) any {
88+
path := strings.Split(key, this.keyDelim)
89+
90+
return this.Search(path...)
7791
}
7892

7993
// 查找
8094
// find key data from source
81-
func (this Array) Find(source any, key string) any {
82-
var (
83-
val any
84-
path = strings.Split(key, this.keyDelim)
85-
)
95+
func Find(source any, key string) any {
96+
return New(source).Find(key)
97+
}
98+
99+
// 搜索
100+
// Search data with key from source
101+
func (this Array) Search(path ...string) any {
102+
return this.search(this.source, path...)
103+
}
104+
105+
// 搜索
106+
// Search data with key from source
107+
func Search(source any, path ...string) any {
108+
return New(source).Search(path...)
109+
}
110+
111+
// 搜索
112+
// Search data with key from source
113+
func (this Array) search(source any, path ...string) any {
114+
var val any
86115

87116
newSource, isMap := this.anyDataMapFormat(source)
88117
if isMap {
@@ -105,12 +134,6 @@ func (this Array) Find(source any, key string) any {
105134
return nil
106135
}
107136

108-
// 查找
109-
// find key data from source
110-
func Find(source any, key string) any {
111-
return defaultArray.Find(source, key)
112-
}
113-
114137
// 数组
115138
// searchMap
116139
func (this Array) searchMap(source map[string]any, path []string) any {
@@ -219,7 +242,7 @@ func (this Array) searchMapWithPathPrefixes(
219242
return nil
220243
}
221244

222-
func (this Array) IsPathShadowedInDeepMap(path []string, m map[string]any) string {
245+
func (this Array) isPathShadowedInDeepMap(path []string, m map[string]any) string {
223246
var parentVal any
224247

225248
for i := 1; i < len(path); i++ {

0 commit comments

Comments
 (0)