-
Notifications
You must be signed in to change notification settings - Fork 10
/
item.go
55 lines (48 loc) · 1.1 KB
/
item.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
package postman
type ItemList []*Item
func (b ItemList) findItem(name string) *Item {
for _, current := range b {
if current.Name == name {
return current
}
}
return nil
}
type Item struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Request *Request `json:"request,omitempty"`
Item ItemList `json:"item,omitempty"`
}
func newItem(name string) *Item {
return &Item{
Name: name,
Item: ItemList{},
}
}
func createFolder(itemList *ItemList, path string, folderOpts *folderOptions) *Item {
slicedPath := slicePath(path)
if len(slicedPath) == 0 {
return nil
}
root := itemList.findItem(slicedPath[0])
if root == nil {
root = newItem(slicedPath[0])
if folderOpts != nil {
root.Description = folderOpts.Description
}
*itemList = append(*itemList, root)
}
for _, value := range slicedPath[1:] {
child := root.Item.findItem(value)
if child == nil {
child = newItem(value)
if folderOpts != nil {
child.Description = folderOpts.Description
}
root.Item = append(root.Item, child)
}
root = child
}
return root
}