-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
268 lines (245 loc) · 6.98 KB
/
node.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package smugmug
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// NodeService is the API for node endpoints
type NodeService service
// NodeIterFunc is called for each node in the results
type NodeIterFunc func(*Node) (bool, error)
func (s *NodeService) node(req *http.Request) (*Node, error) {
res := &nodeResponse{}
err := s.client.do(req, res)
if err != nil {
return nil, err
}
return s.expand(res.Response.Node, res.Expansions)
}
func (s *NodeService) nodes(req *http.Request) ([]*Node, *Pages, error) {
res := &nodesResponse{}
err := s.client.do(req, res)
if err != nil {
return nil, nil, err
}
for i := range res.Response.Node {
if _, err = s.expand(res.Response.Node[i], res.Expansions); err != nil {
return nil, nil, err
}
}
return res.Response.Node, res.Response.Pages, nil
}
func (s *NodeService) expand(node *Node, expansions map[string]*json.RawMessage) (*Node, error) { //nolint:gocognit
if node.URIs.User != nil {
if val, ok := expansions[node.URIs.User.URI]; ok {
res := struct {
User *User `json:"User"`
}{}
if err := json.Unmarshal(*val, &res); err != nil {
return nil, err
}
node.User = res.User
}
}
if node.URIs.HighlightImage != nil {
if val, ok := expansions[node.URIs.HighlightImage.URI]; ok {
res := struct {
Image *Image `json:"Image"`
}{}
if err := json.Unmarshal(*val, &res); err != nil {
return nil, err
}
node.HighlightImage = res.Image
}
}
if node.URIs.Parent != nil {
if val, ok := expansions[node.URIs.Parent.URI]; ok {
res := struct {
Node *Node `json:"Node"`
}{}
if err := json.Unmarshal(*val, &res); err != nil {
return nil, err
}
node.Parent = res.Node
}
}
switch node.Type {
case TypeFolder:
// deprecated
case TypeAlbum:
if node.URIs.Album != nil {
if val, ok := expansions[node.URIs.Album.URI]; ok {
res := struct {
Album *Album `json:"Album"`
}{}
if err := json.Unmarshal(*val, &res); err != nil {
return nil, err
}
node.Album = res.Album
}
}
}
return node, nil
}
// Node returns the node with id `nodeID`
func (s *NodeService) Node(ctx context.Context, nodeID string, options ...APIOption) (*Node, error) {
uri := fmt.Sprintf("node/%s", nodeID)
req, err := s.client.newRequest(ctx, uri, options)
if err != nil {
return nil, err
}
return s.node(req)
}
// Create creates a node for the nodelet under the `parentID`
func (s *NodeService) Create(ctx context.Context, parentID string, nodelet *Nodelet) (*Node, error) {
uri := fmt.Sprintf("node/%s!children", parentID)
body, err := json.Marshal(nodelet)
if err != nil {
return nil, err
}
req, err := s.client.newRequestWithBody(ctx, http.MethodPost, uri, bytes.NewReader(body), nil)
if err != nil {
return nil, err
}
return s.node(req)
}
// Children returns a single page of direct children of the node (does not traverse)
func (s *NodeService) Children(
ctx context.Context, nodeID string, options ...APIOption) ([]*Node, *Pages, error) {
uri := fmt.Sprintf("node/%s!children", nodeID)
req, err := s.client.newRequest(ctx, uri, options)
if err != nil {
return nil, nil, err
}
return s.nodes(req)
}
// ChildrenIter iterates all direct children of the node
func (s *NodeService) ChildrenIter(
ctx context.Context, nodeID string, iter NodeIterFunc, options ...APIOption) error {
return iterate(ctx, func(ctx context.Context, options ...APIOption) ([]*Node, *Pages, error) {
return s.Children(ctx, nodeID, options...)
}, iter, options...)
}
// Search returns a single page of search results (does not traverse)
func (s *NodeService) Search(
ctx context.Context, options ...APIOption) ([]*Node, *Pages, error) {
req, err := s.client.newRequest(ctx, "node!search", options)
if err != nil {
return nil, nil, err
}
return s.nodes(req)
}
// SearchIter iterates all search results
func (s *NodeService) SearchIter(
ctx context.Context, iter NodeIterFunc, options ...APIOption) error {
return iterate(ctx, s.Search, iter, options...)
}
// Parent returns the parent node
func (s *NodeService) Parent(
ctx context.Context, nodeID string, options ...APIOption) (*Node, error) {
uri := fmt.Sprintf("node/%s!parent", nodeID)
req, err := s.client.newRequest(ctx, uri, options)
if err != nil {
return nil, err
}
return s.node(req)
}
// Parents returns a single page of parent nodes (does not traverse)
func (s *NodeService) Parents(
ctx context.Context, nodeID string, options ...APIOption) ([]*Node, *Pages, error) {
uri := fmt.Sprintf("node/%s!parents", nodeID)
req, err := s.client.newRequest(ctx, uri, options)
if err != nil {
return nil, nil, err
}
return s.nodes(req)
}
// ParentsIter iterates all parental ancestors
func (s *NodeService) ParentsIter(
ctx context.Context, nodeID string, iter NodeIterFunc, options ...APIOption) error {
return iterate(ctx, func(ctx context.Context, options ...APIOption) ([]*Node, *Pages, error) {
return s.Parents(ctx, nodeID, options...)
}, iter, options...)
}
// Walk traverses all children of the node rooted at `nodeID`
func (s *NodeService) Walk(
ctx context.Context, nodeID string, fn NodeIterFunc, options ...APIOption) error {
return s.WalkN(ctx, nodeID, fn, -1, options...)
}
// WalkN traverses all children of the node rooted at `nodeID` to the specified depth
func (s *NodeService) WalkN(
ctx context.Context, nodeID string, fn NodeIterFunc, depth int, options ...APIOption) error {
k := &stack{}
k.push(nodeID, nil, 0)
for {
var err error
nid, ok := k.pop()
if !ok {
return nil
}
node := nid.node
if node == nil {
node, err = s.Node(ctx, nid.id, options...)
if err != nil {
return err
}
}
if ok, err = fn(node); err != nil {
return err
} else if !ok {
return nil
}
switch node.Type {
case "Album", "System Album":
// ignore, no children
case "Folder":
if nid.depth != depth {
if err = s.ChildrenIter(ctx, nid.id, func(node *Node) (bool, error) {
k.push(node.NodeID, node, nid.depth+1)
return true, nil
}, options...); err != nil {
return err
}
}
default:
return fmt.Errorf("unhandled type {%s}", node.Type)
}
}
}
type item struct {
id string
node *Node
depth int
}
type stack []*item
func (s *stack) push(id string, node *Node, depth int) {
*s = append(*s, &item{id: id, node: node, depth: depth})
}
func (s *stack) pop() (*item, bool) {
if len(*s) == 0 {
return nil, false
}
index := len(*s) - 1
element := (*s)[index]
*s = (*s)[:index]
return element, true
}
type nodesResponse struct {
Response struct {
Node []*Node `json:"Node"`
Pages *Pages `json:"Pages"`
} `json:"Response"`
Expansions map[string]*json.RawMessage `json:"Expansions,omitempty"`
Code int `json:"Code"`
Message string `json:"Message"`
}
type nodeResponse struct {
Response struct {
Node *Node `json:"Node"`
} `json:"Response"`
Expansions map[string]*json.RawMessage `json:"Expansions,omitempty"`
Code int `json:"Code"`
Message string `json:"Message"`
}