-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray.go
171 lines (149 loc) · 3 KB
/
array.go
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
162
163
164
165
166
167
168
169
170
171
package jsons
import (
"database/sql/driver"
"encoding/json"
"errors"
"sort"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func (a Array) Value() (driver.Value, error) {
return json.Marshal(a)
}
func (a *Array) Scan(v interface{}) error {
if v == nil {
*a = nil
return nil
}
switch val := v.(type) {
case []byte:
return json.Unmarshal(val, a)
case string:
return json.Unmarshal([]byte(val), a)
}
return errors.New("invalid scan array source")
}
func (Array) GormDBDataType(*gorm.DB, *schema.Field) string {
return "json"
}
func (a Array) Len(keys ...interface{}) int {
switch len(keys) {
case 0:
return len(a)
default:
return a.Get(keys...).Len()
}
}
func (a Array) Cap(keys ...interface{}) int {
switch len(keys) {
case 0:
return cap(a)
default:
return a.Get(keys...).Cap()
}
}
func (a Array) Raw(keys ...interface{}) Raw {
return a.JSONValue(keys...).Raw()
}
func (a Array) Set(keys ...interface{}) {
if length := len(keys); length > 1 {
key := keys[length-2]
val := value(keys[length-1])
switch value := a.Get(keys[:length-2]...).value.(type) {
case Value:
value.Set(key, val)
case Object:
value.Set(key, val)
case Array:
if index, ok := key.(int); ok && index < len(value) {
value[index] = val
}
}
}
}
func (a Array) Get(keys ...interface{}) (val Value) {
if a == nil {
return
}
switch len(keys) {
case 0:
return value(a)
case 1:
if key, ok := keys[0].(int); ok && key < len(a) {
return value(a[key])
}
default:
var v = value(a)
for _, k := range keys {
switch key := k.(type) {
case int:
v = v.Array().Get(key)
case string:
v = v.Object().Get(key)
}
}
return v
}
return
}
func (a Array) Reverse(keys ...interface{}) Array {
switch len(keys) {
case 0:
for b, e := 0, len(a)-1; b < e; b, e = b+1, e-1 {
a[b], a[e] = a[e], a[b]
}
return a
default:
return a.Get(keys...).Reverse()
}
}
func (a Array) Clone(keys ...interface{}) Value {
switch len(keys) {
case 0:
var array = make(Array, len(a), cap(a))
data, _ := json.Marshal(a)
_ = json.Unmarshal(data, &array)
return value(array)
default:
return a.Get(keys...).Clone()
}
}
func (a Array) Range(fn func(index int, value Value) (continued bool)) bool {
for index, val := range a {
if !fn(index, value(val)) {
return false
}
}
return true
}
func (a Array) Sort(less func(i, j int) bool) Array {
sort.Slice(a, less)
return a
}
func (a Array) Index(value interface{}) int {
value = original(value)
for i, v := range a {
if original(v) == value {
return i
}
}
return -1
}
func (a Array) Contains(value interface{}) bool {
return a.Index(value) >= 0
}
func (a Array) Slice(begin, end int) Array {
return a[begin:end]
}
func (a Array) Append(arr Array) Array {
return append(a, arr...)
}
func (a Array) JSON(keys ...interface{}) []byte {
return a.Get(keys...).JSON()
}
func (a Array) JSONValue(keys ...interface{}) Value {
return a.Get(keys...)
}
func (a Array) JSONString(keys ...interface{}) string {
return a.Get(keys...).JSONString()
}