-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.go
More file actions
161 lines (144 loc) · 3.5 KB
/
array.go
File metadata and controls
161 lines (144 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2021 Wang Min Xiang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package json
import (
"bytes"
"fmt"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"github.com/valyala/bytebufferpool"
"io"
)
var (
EmptyArrayBytes = []byte{'[', ']'}
)
func IsEmptyArray(p []byte) (ok bool) {
ok = len(p) == 0 || bytes.Equal(p, EmptyArrayBytes)
return
}
func NewArrayFromBytes(b []byte) *Array {
if b[0] != '[' || b[len(b)-1] != ']' {
panic(fmt.Errorf("new json array from bytes failed, %s is not json array bytes", string(b)))
}
return &Array{
raw: b,
}
}
func NewArray() *Array {
return &Array{
raw: []byte{'[', ']'},
}
}
type Array struct {
raw []byte
}
func (array *Array) Empty() (ok bool) {
if array.raw == nil || len(array.raw) == 0 {
ok = true
return
}
ok = !gjson.ParseBytes(array.raw).Exists()
return
}
func (array *Array) Raw() (raw []byte) {
raw = array.raw
return
}
func (array *Array) WriteTo(out *Array) (err error) {
if out == nil {
err = fmt.Errorf("fns json.Array WriteTo: can not write to nil point")
return
}
err = out.UnmarshalJSON(array.raw)
return
}
func (array *Array) Add(values ...interface{}) (err error) {
if values == nil || len(values) == 0 {
err = fmt.Errorf("json array add failed, values is empty")
return
}
rb := bytes.NewReader(array.raw)
nb := bytebufferpool.Get()
_, _ = io.Copy(nb, rb)
affected := nb.Bytes()
bytebufferpool.Put(nb)
var addErr error
for i, value := range values {
if value == nil {
continue
}
affected, addErr = sjson.SetBytes(affected, "-1", value)
if addErr != nil {
err = fmt.Errorf("json array add %d failed", i)
return
}
}
array.raw = affected
return
}
func (array *Array) Remove(i int) (err error) {
if i < 0 {
err = fmt.Errorf("json array remove failed, index is less than 0")
return
}
affected, remErr := sjson.DeleteBytes(array.raw, fmt.Sprintf("%d", i))
if remErr != nil {
err = fmt.Errorf("json array remove %d failed", i)
return
}
array.raw = affected
return
}
func (array *Array) Len() (size int) {
size = len(gjson.ParseBytes(array.raw).Array())
return
}
func (array *Array) Get(i int, v interface{}) (err error) {
if i < 0 || i >= array.Len() {
err = fmt.Errorf("json array get failed, index is less than 0 or greater than len")
return
}
if v == nil {
err = fmt.Errorf("json array get %d failed, value is nil", i)
return
}
raw := gjson.ParseBytes(array.raw).Array()[i].Raw
decodeErr := Unmarshal([]byte(raw), v)
if decodeErr != nil {
err = fmt.Errorf("json array get %d failed, decode failed", i)
return
}
return
}
func (array *Array) MapTo(v interface{}) (err error) {
err = Unmarshal(array.raw, v)
return
}
func (array *Array) MarshalJSON() (p []byte, err error) {
p = array.raw
return
}
func (array *Array) UnmarshalJSON(p []byte) (err error) {
if p == nil || len(p) == 0 {
return
}
if p[0] != '[' || p[len(p)-1] != ']' {
err = fmt.Errorf("unmarshal json array from bytes failed, %s is not json array bytes", string(p))
return
}
array.raw = p
return
}