This repository has been archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
transform.go
97 lines (86 loc) · 2.93 KB
/
transform.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
package ipld
import (
"errors"
"strconv"
"path"
)
// TransformFunc is the type of the function called for each node visited by
// Transform. The root argument is the node from which the Transform began. The
// curr argument is the currently visited node. The path argument is the
// traversal path, from root to curr.
//
// If there was a problem walking to curr, the err argument will describe the
// problem and the function can decide how to handle the error (and Transform
// _will not_ descend into any of the children of curr).
//
// TransformFunc may return a node, in which case the returned node will be used
// for further traversal instead of the curr node.
//
// TransformFunc may return an error. If the error is the special SkipNode
// error, the children of curr are skipped. All other errors halt processing
// early.
type TransformFunc func(root, curr Node, path []string, err error) (Node, error)
// Transform traverses the given root node and all its children, calling
// TransformFunc with every Node visited, including root. All errors that arise
// while visiting nodes are passed to given TransformFunc. The traversing
// algorithm is the same as the Walk function.
//
// Transform returns a node constructed from the different nodes returned by
// TransformFunc.
func Transform(root Node, transformFn TransformFunc) (Node, error) {
n, err := transform(root, root, nil, transformFn)
if node, ok := n.(Node); ok {
return node, err
} else {
return nil, err
}
}
// TransformFrom is just like Transform, but starts the Walk at given startFrom
// sub-node.
func TransformFrom(root Node, startFrom []string, transformFn TransformFunc) (interface{}, error) {
start := GetPathCmp(root, startFrom)
if start == nil {
return nil, errors.New("no descendant at " + path.Join(startFrom...))
}
return transform(root, start, startFrom, transformFn)
}
// transform is used to implement Transform
func transform(root Node, curr interface{}, npath []string, transformFunc TransformFunc) (interface{}, error) {
if nc, ok := curr.(Node); ok { // it's a node!
// first, call user's WalkFunc.
newnode, err := transformFunc(root, nc, npath, nil)
res := Node{}
if err == SkipNode {
return newnode, nil // ok, let's skip this one.
} else if err != nil {
return nil, err // something bad happened, return early.
} else if newnode != nil {
nc = newnode
}
// then recurse.
for k, v := range nc {
n, err := transform(root, v, append(npath, k), transformFunc)
if err != nil {
return nil, err
} else if n != nil {
res[k] = n
}
}
return res, nil
} else if sc, ok := curr.([]interface{}); ok { // it's a slice!
res := []interface{}{}
for i, v := range sc {
k := strconv.Itoa(i)
n, err := transform(root, v, append(npath, k), transformFunc)
if err != nil {
return nil, err
} else if n != nil {
res = append(res, n)
}
}
return res, nil
} else { // it's just data.
// ignore it.
}
return curr, nil
}