-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwalk.go
More file actions
32 lines (26 loc) · 775 Bytes
/
Copy pathwalk.go
File metadata and controls
32 lines (26 loc) · 775 Bytes
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
package chaff
import "fmt"
// WalkSchema recursively visits every JSON object node in a schema tree.
// The visitor is called for each node, parents before children.
// path tracks the JSON pointer path (e.g. "/properties/name").
func walkSchema(node map[string]interface{}, path string, visit func(node map[string]interface{}, path string)) {
visit(node, path)
for key, value := range node {
childPath := path + "/" + key
if obj, ok := value.(map[string]interface{}); ok {
walkSchema(obj, childPath, visit)
continue
}
arr, ok := value.([]interface{})
if !ok {
continue
}
for i, item := range arr {
obj, ok := item.(map[string]interface{})
if !ok {
continue
}
walkSchema(obj, fmt.Sprintf("%s/%d", childPath, i), visit)
}
}
}