Skip to content

Commit 04aeef2

Browse files
authored
Merge pull request #10 from securityfirst/difficulty
Difficulty
2 parents 0ead6c1 + 82acfdf commit 04aeef2

17 files changed

+470
-262
lines changed

component/cmp_all_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var _ = Suite(&CmpSuite{})
1818
type CmpSuite struct{}
1919

2020
func (*CmpSuite) TestParse(c *C) {
21-
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: repoAddress("klaidliadon", "tent-content")})
21+
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: repoAddress("securityfirst", "tent-example")})
2222
c.Assert(err, IsNil)
2323
err = r.Fetch(&git.FetchOptions{})
2424
c.Assert(err, Equals, git.NoErrAlreadyUpToDate)
@@ -35,15 +35,17 @@ func (*CmpSuite) TestParse(c *C) {
3535
for _, cat := range t.Categories()["en"] {
3636
c.Log(cat.Name)
3737
for _, sub := range cat.subcategories {
38-
c.Logf("\t%q, items:%v checks:%v", sub.Name, len(sub.items), len(sub.checklist.Checks))
38+
for _, dif := range sub.difficulties {
39+
c.Logf("\t%q %q, items:%v checks:%v", sub.Name, dif.Name, len(dif.items), len(dif.checklist.Checks))
40+
}
3941
}
4042
}
4143
}
4244

4345
func (CmpSuite) TestForm(c *C) {
4446
var f Form
4547

46-
path := `/forms_en/formid.md`
48+
path := `forms_en/formid.md`
4749
contents := `[Name]: # (Form Name)
4850
4951
[Type]: # (screen)

component/cmp_asset.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
type Asset struct {
9-
Id string `json:"-"`
9+
ID string `json:"id"`
1010
Hash string `json:"hash,omAssetpty"`
1111
Content string `json:"content"`
1212
}
@@ -24,17 +24,17 @@ func (a *Asset) SHA() string {
2424
}
2525

2626
func (i *Asset) Path() string {
27-
return fmt.Sprintf("/assets/%s", i.Id)
27+
return fmt.Sprintf("assets/%s", i.ID)
2828
}
2929

30-
var assetPath = regexp.MustCompile("/assets/([^/]+)")
30+
var assetPath = regexp.MustCompile("assets/([^/]+)")
3131

3232
func (i *Asset) SetPath(filepath string) error {
3333
p := assetPath.FindStringSubmatch(filepath)
3434
if len(p) == 0 {
3535
return ErrContent
3636
}
37-
i.Id = p[1]
37+
i.ID = p[1]
3838
return nil
3939
}
4040

component/cmp_category.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
type Category struct {
10-
Id string `json:"-"`
10+
ID string `json:"id"`
1111
Name string `json:"name"`
1212
Hash string `json:"hash"`
1313
Locale string `json:"-"`
@@ -17,7 +17,7 @@ type Category struct {
1717

1818
func (c *Category) Resource() Resource {
1919
return Resource{
20-
Slug: c.Id,
20+
Slug: c.ID,
2121
Content: []map[string]string{
2222
map[string]string{"name": c.Name},
2323
},
@@ -38,6 +38,7 @@ func (c *Category) Tree(html bool) interface{} {
3838
subs = append(subs, c.subcategories[i].Tree(html))
3939
}
4040
return map[string]interface{}{
41+
"id": c.ID,
4142
"name": c.Name,
4243
"subcategories": subs,
4344
}
@@ -54,9 +55,9 @@ func (c *Category) MarshalJSON() ([]byte, error) {
5455
return json.Marshal(m)
5556
}
5657

57-
func (c *Category) Sub(id string) *Subcategory {
58+
func (c *Category) Sub(ID string) *Subcategory {
5859
for _, v := range c.subcategories {
59-
if v.Id == id {
60+
if v.ID == ID {
6061
return v
6162
}
6263
}
@@ -66,7 +67,7 @@ func (c *Category) Sub(id string) *Subcategory {
6667
func (c *Category) Subcategories() []string {
6768
var r = make([]string, 0, len(c.subcategories))
6869
for _, v := range c.subcategories {
69-
r = append(r, v.Id)
70+
r = append(r, v.ID)
7071
}
7172
return r
7273
}
@@ -83,22 +84,22 @@ func (c *Category) basePath() string {
8384
if c.Locale != "" {
8485
loc = "_" + c.Locale
8586
}
86-
return fmt.Sprintf("/contents%s/%s", loc, c.Id)
87+
return fmt.Sprintf("contents%s/%s", loc, c.ID)
8788
}
8889

8990
func (c *Category) Path() string {
9091
return fmt.Sprintf("%s/%s%s", c.basePath(), suffixMeta, fileExt)
9192
}
9293

93-
var catPath = regexp.MustCompile("/contents_([a-z]{2})/([^/]+)/.metadata.md")
94+
var catPath = regexp.MustCompile("contents_([a-z]{2})/([^/]+)/.metadata.md")
9495

9596
func (c *Category) SetPath(filepath string) error {
9697
p := catPath.FindStringSubmatch(filepath)
9798
if len(p) == 0 {
9899
return ErrContent
99100
}
100101
c.Locale = p[1]
101-
c.Id = p[2]
102+
c.ID = p[2]
102103
return nil
103104
}
104105

component/cmp_checklist.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
type Checklist struct {
11-
parent *Subcategory
11+
parent *Difficulty
1212
Hash string `json:"hash"`
1313
Checks []Check `json:"checks"`
1414
}
@@ -17,8 +17,7 @@ func (c *Checklist) Resource() Resource {
1717
var content = make([]map[string]string, 0, len(c.Checks))
1818
for _, c := range c.Checks {
1919
content = append(content, map[string]string{
20-
"difficulty": c.Difficulty,
21-
"text": c.Text,
20+
"text": c.Text,
2221
})
2322
}
2423
return Resource{
@@ -28,17 +27,16 @@ func (c *Checklist) Resource() Resource {
2827
}
2928

3029
type Check struct {
31-
Difficulty string `json:"difficulty"`
32-
Text string `json:"text"`
33-
NoCheck bool `json:"no_check"`
30+
Text string `json:"text"`
31+
NoCheck bool `json:"no_check"`
3432
}
3533

36-
func (c *Check) order() []string { return []string{"Text", "Difficulty", "NoCheck"} }
37-
func (c *Check) pointers() args { return args{&c.Text, &c.Difficulty, &c.NoCheck} }
38-
func (c *Check) values() args { return args{c.Text, c.Difficulty, c.NoCheck} }
34+
func (c *Check) order() []string { return []string{"Text", "NoCheck"} }
35+
func (c *Check) pointers() args { return args{&c.Text, &c.NoCheck} }
36+
func (c *Check) values() args { return args{c.Text, c.NoCheck} }
3937

40-
func (c *Checklist) SetParent(s *Subcategory) {
41-
c.parent = s
38+
func (c *Checklist) SetParent(d *Difficulty) {
39+
c.parent = d
4240
}
4341

4442
func (c *Checklist) HasChildren() bool {
@@ -53,7 +51,7 @@ func (c *Checklist) Path() string {
5351
return fmt.Sprintf("%s/.checks%s", c.parent.basePath(), fileExt)
5452
}
5553

56-
var checklistPath = regexp.MustCompile("/contents(?:_[a-z]{2})?/[^/]+/[^/]+/.checks.md")
54+
var checklistPath = regexp.MustCompile("contents(?:_[a-z]{2})?/[^/]+/[^/]+/[^/]+/.checks.md")
5755

5856
func (*Checklist) SetPath(filepath string) error {
5957
p := checklistPath.FindString(filepath)

component/cmp_difficulty.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package component
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"regexp"
7+
)
8+
9+
type Difficulty struct {
10+
parent *Subcategory
11+
ID string `json:"id"`
12+
Descr string `json:"description"`
13+
Hash string `json:"hash"`
14+
items []*Item
15+
checklist *Checklist
16+
}
17+
18+
func (d *Difficulty) Resource() Resource {
19+
return Resource{
20+
Slug: d.parent.Resource().Slug + "_" + d.ID,
21+
Content: []map[string]string{
22+
map[string]string{"description": d.Descr},
23+
},
24+
}
25+
}
26+
27+
func (d *Difficulty) HasChildren() bool {
28+
return len(d.items) != 0
29+
}
30+
31+
func (d *Difficulty) Tree(html bool) interface{} {
32+
var items = make([]Item, len(d.items))
33+
for i, v := range d.items {
34+
items[i] = *v
35+
if html {
36+
items[i].Body = items[i].htmlBody
37+
}
38+
}
39+
return map[string]interface{}{
40+
"id": d.ID,
41+
"description": d.Descr,
42+
"items": items,
43+
"checks": d.checklist.Checks,
44+
}
45+
}
46+
47+
func (d *Difficulty) SHA() string {
48+
return d.Hash
49+
}
50+
51+
func (d *Difficulty) SetParent(s *Subcategory) {
52+
d.parent = s
53+
}
54+
55+
func (d *Difficulty) MarshalJSON() ([]byte, error) {
56+
var m = map[string]interface{}{
57+
"description": d.Descr,
58+
"items": d.ItemNames(),
59+
"checks": d.checklist.Checks,
60+
}
61+
if d.Hash != "" {
62+
m["hash"] = d.Hash
63+
}
64+
return json.Marshal(m)
65+
}
66+
67+
func (d *Difficulty) Items() []Item {
68+
var dst = make([]Item, len(d.items))
69+
for i, v := range d.items {
70+
dst[i] = *v
71+
}
72+
return dst
73+
}
74+
75+
func (d *Difficulty) ItemNames() []string {
76+
var r = make([]string, 0, len(d.items))
77+
for i := range d.items {
78+
r = append(r, d.items[i].ID)
79+
}
80+
return r
81+
}
82+
83+
func (d *Difficulty) Checks() *Checklist {
84+
var dst []Check
85+
if d.checklist == nil {
86+
d.SetChecks(new(Checklist))
87+
}
88+
dst = make([]Check, len(d.checklist.Checks))
89+
for i, v := range d.checklist.Checks {
90+
dst[i] = v
91+
}
92+
return &Checklist{
93+
parent: d,
94+
Hash: d.checklist.Hash,
95+
Checks: dst,
96+
}
97+
}
98+
99+
func (d *Difficulty) AddChecks(c ...Check) {
100+
if d.checklist == nil {
101+
d.SetChecks(new(Checklist))
102+
}
103+
d.checklist.Add(c...)
104+
}
105+
106+
func (d *Difficulty) SetChecks(c *Checklist) {
107+
d.checklist = c
108+
c.parent = d
109+
}
110+
111+
func (d *Difficulty) AddItem(items ...*Item) error {
112+
for _, v := range items {
113+
if d.Item(v.ID) != nil {
114+
return fmt.Errorf("item %s exists in %s/%s", v.ID, d.parent.ID, d.ID)
115+
}
116+
v.parent = d
117+
}
118+
d.items = append(d.items, items...)
119+
return nil
120+
}
121+
122+
func (d *Difficulty) Item(id string) *Item {
123+
for _, v := range d.items {
124+
if v.ID == id {
125+
return v
126+
}
127+
}
128+
return nil
129+
}
130+
131+
func (d *Difficulty) basePath() string {
132+
return fmt.Sprintf("%s/%s", d.parent.basePath(), d.ID)
133+
}
134+
135+
func (d *Difficulty) Path() string {
136+
return fmt.Sprintf("%s/.metadata%s", d.basePath(), fileExt)
137+
}
138+
139+
var diffPath = regexp.MustCompile("contents(?:_[a-z]{2})?/[^/]+/[^/]+/([^/]+)/.metadata.md")
140+
141+
func (d *Difficulty) SetPath(filepath string) error {
142+
p := diffPath.FindStringSubmatch(filepath)
143+
if len(p) == 0 {
144+
return ErrContent
145+
}
146+
d.ID = p[1]
147+
return nil
148+
}
149+
150+
func (d *Difficulty) order() []string { return []string{"Description"} }
151+
func (d *Difficulty) pointers() args { return args{&d.Descr} }
152+
func (d *Difficulty) values() args { return args{d.Descr} }
153+
154+
func (d *Difficulty) Contents() string { return getMeta(d) }
155+
156+
func (d *Difficulty) SetContents(contents string) error {
157+
if err := checkMeta(contents, d); err != nil {
158+
return err
159+
}
160+
if d.checklist == nil {
161+
d.checklist = new(Checklist)
162+
}
163+
return setMeta(contents, d)
164+
}

component/cmp_form.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
type Form struct {
11-
Id string `json:"-"`
11+
ID string `json:"id"`
1212
Name string `json:"name"`
1313
Hash string `json:"hash"`
1414
Locale string `json:"-"`
@@ -17,7 +17,7 @@ type Form struct {
1717

1818
func (f *Form) Resource() Resource {
1919
return Resource{
20-
Slug: f.Id,
20+
Slug: f.ID,
2121
Content: []map[string]string{
2222
map[string]string{"name": f.Name},
2323
},
@@ -33,18 +33,18 @@ func (f *Form) Path() string {
3333
if f.Locale != "" {
3434
loc = "_" + f.Locale
3535
}
36-
return fmt.Sprintf("/forms%s/%s%s", loc, f.Id, fileExt)
36+
return fmt.Sprintf("forms%s/%s%s", loc, f.ID, fileExt)
3737
}
3838

39-
var formPath = regexp.MustCompile("/forms_([a-z]{2})/([^/]+).md")
39+
var formPath = regexp.MustCompile("forms_([a-z]{2})/([^/]+).md")
4040

4141
func (f *Form) SetPath(filepath string) error {
4242
p := formPath.FindStringSubmatch(filepath)
4343
if len(p) == 0 {
4444
return ErrContent
4545
}
4646
f.Locale = p[1]
47-
f.Id = p[2]
47+
f.ID = p[2]
4848
return nil
4949
}
5050

0 commit comments

Comments
 (0)