forked from qri-io/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traversal.go
35 lines (30 loc) · 1.01 KB
/
traversal.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
package jsonschema
// JSONPather makes validators traversible by JSON-pointers,
// which is required to support references in JSON schemas.
type JSONPather interface {
// JSONProp take a string references for a given JSON property
// implementations must return any matching property of that name
// or nil if no such subproperty exists.
// Note this also applies to array values, which are expected to interpret
// valid numbers as an array index
JSONProp(name string) interface{}
}
// JSONContainer is an interface that enables tree traversal by listing
// the immideate children of an object
type JSONContainer interface {
// JSONChildren should return all immidiate children of this element
JSONChildren() map[string]JSONPather
}
func walkJSON(elem JSONPather, fn func(elem JSONPather) error) error {
if err := fn(elem); err != nil {
return err
}
if con, ok := elem.(JSONContainer); ok {
for _, ch := range con.JSONChildren() {
if err := walkJSON(ch, fn); err != nil {
return err
}
}
}
return nil
}