-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcats.go
114 lines (98 loc) · 2.77 KB
/
cats.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
package cats
import (
"bytes"
"encoding/json"
"encoding/xml"
"sort"
)
// Category model.
type Category struct {
ID int
Name string
Icon string
Parent int
Path string
Children []*Category
}
func (c *Category) generatePath(cm *CategoryMap) {
path := new(bytes.Buffer)
if c.Parent != 0 {
path.WriteString(cm.Get(c.Parent).Path)
path.WriteString("/")
} else {
path.WriteString("/")
}
path.WriteString(c.Name)
c.Path = path.String()
for _, ch := range c.Children {
ch.generatePath(cm)
}
}
// CategoryMap a map of Category pointers and associates an index with it, for ordered output.
// The order of the index is based on the order items where added.
type CategoryMap struct {
cats map[int]*Category
index []int
}
// NewCm initializes and returs a pointer to a new CategoryMap.
func NewCm() *CategoryMap {
cats := make(map[int]*Category)
return &CategoryMap{
cats: cats,
}
}
// Set a Category point to the map.
// It will also be appended to the index, keeping the order of calling this function.
func (cm *CategoryMap) Set(c *Category) {
cm.cats[c.ID] = c
cm.index = append(cm.index, c.ID)
}
// Get a Category pointer by its id.
func (cm *CategoryMap) Get(id int) (c *Category) {
return cm.cats[id]
}
// Sort re-indexes the CategoryMap, increasing order on category id.
// It is advised to add the categories in a sorted way instead of using this method.
func (cm *CategoryMap) Sort() {
sort.Ints(cm.index)
}
// Index returns a struct of category id. It allows for a sorted range loop.
func (cm *CategoryMap) Index() []int {
return cm.index
}
// Tree creates decendant tree by population the Category's children.
// It returns a slice of Category pointers, representing the root of the tree.
func (cm *CategoryMap) Tree(offset int) (root []*Category) {
for _, i := range cm.Index() {
c := cm.Get(i)
// Are we at the root of the tree?
if c.Parent == offset {
root = append(root, c)
continue
}
p := cm.Get(c.Parent)
// Append Category to its parent's children
p.Children = append(p.Children, c)
}
return
}
// JSONTree returns a JSON document, representing the parent /
// child relationship of the categories in a tree.
// It returns an error if json.Marshall does.
func (cm *CategoryMap) JSONTree(offset int) ([]byte, error) {
//return json.MarshalIndent(cm.Tree(offset), "", " ")
return json.Marshal(cm.Tree(offset))
}
// XMLTree returns a XML document, representing the parent /
// child relationship of the categories in a tree.
// It returns an error if XML.Marshall does.
func (cm *CategoryMap) XMLTree(offset int) ([]byte, error) {
return xml.Marshal(cm.Tree(offset))
}
// GeneratePaths generate paths for each category
func (cm *CategoryMap) GeneratePaths() {
root := cm.Tree(0)
for _, c := range root {
c.generatePath(cm)
}
}