-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsoncrop.go
More file actions
212 lines (196 loc) · 5.46 KB
/
Copy pathjsoncrop.go
File metadata and controls
212 lines (196 loc) · 5.46 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package json2image
import (
"encoding/json"
"strconv"
"strings"
)
func JsonCrop(input interface{}, rules []string) ([]byte, error) {
output := make(map[string]interface{})
for _, rule := range rules {
steps := parseRule(rule)
processStep(input, steps, output, nil)
}
return json.Marshal(output)
}
type PathStep struct {
Key string
Indices []int // 将单个 Index 改为 Indices 数组
}
func parseRule(rule string) []PathStep {
parts := strings.Split(rule, ".")
var steps []PathStep
for _, part := range parts {
if idxStart := strings.Index(part, "["); idxStart != -1 && strings.HasSuffix(part, "]") {
key := part[:idxStart]
indexStr := part[idxStart+1 : len(part)-1]
if indexStr == "*" {
steps = append(steps, PathStep{Key: key, Indices: nil})
} else {
// 处理多个索引值,用逗号分隔
indices := []int{}
for _, idx := range strings.Split(indexStr, ",") {
if index, err := strconv.Atoi(strings.TrimSpace(idx)); err == nil {
indices = append(indices, index)
}
}
steps = append(steps, PathStep{Key: key, Indices: indices})
}
} else {
steps = append(steps, PathStep{Key: part})
}
}
return steps
}
func processStep(input interface{}, steps []PathStep, output map[string]interface{}, pathSoFar []PathStep) {
if len(steps) == 0 {
return
}
currentStep := steps[0]
remainingSteps := steps[1:]
switch currentStep.Key {
case "*":
switch input := input.(type) {
case map[string]interface{}:
for key := range input {
newStep := PathStep{Key: key}
newPath := append(pathSoFar, newStep)
processStep(input[key], remainingSteps, output, newPath)
}
case []interface{}:
for i := range input {
newStep := PathStep{Key: currentStep.Key, Indices: []int{i}} // 修复:使用切片包含单个索引
newPath := append(pathSoFar, newStep)
processStep(input[i], remainingSteps, output, newPath)
}
}
default:
switch input := input.(type) {
case map[string]interface{}:
if child, exists := input[currentStep.Key]; exists {
newPath := append(pathSoFar, currentStep)
if len(currentStep.Indices) > 0 {
if slice, ok := child.([]interface{}); ok {
// 处理多个索引
for _, index := range currentStep.Indices {
if index < len(slice) {
newStep := PathStep{Key: currentStep.Key, Indices: []int{index}}
newPath := append(pathSoFar, newStep)
processStep(slice[index], remainingSteps, output, newPath)
}
}
}
} else {
if slice, ok := child.([]interface{}); ok {
// 处理 [*] 的情况
for i := range slice {
newStep := PathStep{Key: currentStep.Key, Indices: []int{i}}
newPath := append(pathSoFar, newStep)
processStep(slice[i], remainingSteps, output, newPath)
}
} else {
processStep(child, remainingSteps, output, newPath)
}
}
}
case []interface{}:
if len(currentStep.Indices) > 0 {
for _, index := range currentStep.Indices {
if index < len(input) {
newStep := PathStep{Key: currentStep.Key, Indices: []int{index}}
newPath := append(pathSoFar, newStep)
processStep(input[index], remainingSteps, output, newPath)
}
}
}
}
}
if len(remainingSteps) == 0 {
var value interface{}
switch input := input.(type) {
case map[string]interface{}:
if len(currentStep.Indices) > 0 {
if slice, ok := input[currentStep.Key].([]interface{}); ok {
for _, index := range currentStep.Indices {
if index < len(slice) {
value = slice[index]
fullPath := append(pathSoFar, PathStep{Key: currentStep.Key, Indices: []int{index}})
setValue(output, fullPath, value)
}
}
return
}
} else {
value = input[currentStep.Key]
}
case []interface{}:
if len(currentStep.Indices) > 0 {
for _, index := range currentStep.Indices {
if index < len(input) {
value = input[index]
fullPath := append(pathSoFar, PathStep{Key: currentStep.Key, Indices: []int{index}})
setValue(output, fullPath, value)
}
}
return
}
}
if value != nil {
fullPath := append(pathSoFar, currentStep)
setValue(output, fullPath, value)
}
}
}
func setValue(output map[string]interface{}, path []PathStep, value interface{}) {
current := output
for i, step := range path {
isLast := i == len(path)-1
if len(step.Indices) > 0 {
key := step.Key
index := step.Indices[0] // 使用第一个索引
var slice []interface{}
if existing, ok := current[key].([]interface{}); ok {
slice = existing
if index >= len(slice) {
newSlice := make([]interface{}, index+1)
copy(newSlice, slice)
for j := len(slice); j <= index; j++ {
newSlice[j] = make(map[string]interface{})
}
slice = newSlice
current[key] = slice
}
} else {
slice = make([]interface{}, index+1)
for j := 0; j <= index; j++ {
slice[j] = make(map[string]interface{})
}
current[key] = slice
}
if isLast {
slice[index] = value
return
} else {
if elem, ok := slice[index].(map[string]interface{}); ok {
current = elem
} else {
elem := make(map[string]interface{})
slice[index] = elem
current = elem
}
}
} else {
key := step.Key
if isLast {
current[key] = value
} else {
if existing, ok := current[key].(map[string]interface{}); ok {
current = existing
} else {
newMap := make(map[string]interface{})
current[key] = newMap
current = newMap
}
}
}
}
}