-
Notifications
You must be signed in to change notification settings - Fork 6
/
cmp_subcategory.go
126 lines (107 loc) · 2.85 KB
/
cmp_subcategory.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
package component
import (
"encoding/json"
"fmt"
"regexp"
)
type Subcategory struct {
parent *Category
ID string `json:"id"`
Name string `json:"name"`
Hash string `json:"hash"`
Order float64 `json:"-"`
difficulties []*Difficulty
}
func (s *Subcategory) Resource() Resource {
return Resource{
Slug: s.parent.Resource().Slug + "_" + s.ID,
Content: []map[string]string{
map[string]string{"name": s.Name},
},
}
}
func (s *Subcategory) HasChildren() bool {
return len(s.difficulties) != 0
}
func (s *Subcategory) Tree(html bool) interface{} {
var difficulties = make([]interface{}, len(s.difficulties))
for i, v := range s.difficulties {
difficulties[i] = v.Tree(html)
}
return map[string]interface{}{
"id": s.ID,
"name": s.Name,
"difficulties": difficulties,
}
}
func (s *Subcategory) SHA() string {
return s.Hash
}
func (s *Subcategory) SetParent(c *Category) {
s.parent = c
}
func (s *Subcategory) MarshalJSON() ([]byte, error) {
var m = map[string]interface{}{
"name": s.Name,
"difficulties": s.DifficultyNames(),
}
if s.Hash != "" {
m["hash"] = s.Hash
}
return json.Marshal(m)
}
func (s *Subcategory) Difficulties() []Difficulty {
var dst = make([]Difficulty, len(s.difficulties))
for i, v := range s.difficulties {
dst[i] = *v
}
return dst
}
func (s *Subcategory) DifficultyNames() []string {
var r = make([]string, 0, len(s.difficulties))
for i := range s.difficulties {
r = append(r, s.difficulties[i].ID)
}
return r
}
func (s *Subcategory) AddDifficulty(difficulties ...*Difficulty) error {
for _, v := range difficulties {
if s.Difficulty(v.ID) != nil {
return fmt.Errorf("Difficulty %s exists in %s/%s", v.ID, s.parent.ID, s.ID)
}
v.parent = s
}
s.difficulties = append(s.difficulties, difficulties...)
return nil
}
func (s *Subcategory) Difficulty(ID string) *Difficulty {
for _, v := range s.difficulties {
if v.ID == ID {
return v
}
}
return nil
}
func (s *Subcategory) basePath() string {
return fmt.Sprintf("%s/%s", s.parent.basePath(), s.ID)
}
func (s *Subcategory) Path() string {
return fmt.Sprintf("%s/.metadata%s", s.basePath(), fileExt)
}
var subPath = regexp.MustCompile("contents(?:_[a-z]{2})?/[^/]+/([^/]+)/.metadata.md")
func (s *Subcategory) SetPath(filepath string) error {
p := subPath.FindStringSubmatch(filepath)
if len(p) == 0 {
return ErrContent
}
s.ID = p[1]
return nil
}
func (*Subcategory) order() []string { return []string{"Name", "Order"} }
func (*Subcategory) optionals() []string { return nil }
func (s *Subcategory) pointers() args { return args{&s.Name, &s.Order} }
func (s *Subcategory) values() args { return args{s.Name, s.Order} }
func (s *Subcategory) Contents() string { return getMeta(s) }
func (s *Subcategory) SetContents(contents string) error {
return setMeta(contents, s)
}