Skip to content

Commit

Permalink
Merge pull request #10 from securityfirst/difficulty
Browse files Browse the repository at this point in the history
Difficulty
  • Loading branch information
Coccodrillo authored Dec 14, 2017
2 parents 0ead6c1 + 82acfdf commit 04aeef2
Show file tree
Hide file tree
Showing 17 changed files with 470 additions and 262 deletions.
8 changes: 5 additions & 3 deletions component/cmp_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var _ = Suite(&CmpSuite{})
type CmpSuite struct{}

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

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

path := `/forms_en/formid.md`
path := `forms_en/formid.md`
contents := `[Name]: # (Form Name)
[Type]: # (screen)
Expand Down
8 changes: 4 additions & 4 deletions component/cmp_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type Asset struct {
Id string `json:"-"`
ID string `json:"id"`
Hash string `json:"hash,omAssetpty"`
Content string `json:"content"`
}
Expand All @@ -24,17 +24,17 @@ func (a *Asset) SHA() string {
}

func (i *Asset) Path() string {
return fmt.Sprintf("/assets/%s", i.Id)
return fmt.Sprintf("assets/%s", i.ID)
}

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

func (i *Asset) SetPath(filepath string) error {
p := assetPath.FindStringSubmatch(filepath)
if len(p) == 0 {
return ErrContent
}
i.Id = p[1]
i.ID = p[1]
return nil
}

Expand Down
17 changes: 9 additions & 8 deletions component/cmp_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Category struct {
Id string `json:"-"`
ID string `json:"id"`
Name string `json:"name"`
Hash string `json:"hash"`
Locale string `json:"-"`
Expand All @@ -17,7 +17,7 @@ type Category struct {

func (c *Category) Resource() Resource {
return Resource{
Slug: c.Id,
Slug: c.ID,
Content: []map[string]string{
map[string]string{"name": c.Name},
},
Expand All @@ -38,6 +38,7 @@ func (c *Category) Tree(html bool) interface{} {
subs = append(subs, c.subcategories[i].Tree(html))
}
return map[string]interface{}{
"id": c.ID,
"name": c.Name,
"subcategories": subs,
}
Expand All @@ -54,9 +55,9 @@ func (c *Category) MarshalJSON() ([]byte, error) {
return json.Marshal(m)
}

func (c *Category) Sub(id string) *Subcategory {
func (c *Category) Sub(ID string) *Subcategory {
for _, v := range c.subcategories {
if v.Id == id {
if v.ID == ID {
return v
}
}
Expand All @@ -66,7 +67,7 @@ func (c *Category) Sub(id string) *Subcategory {
func (c *Category) Subcategories() []string {
var r = make([]string, 0, len(c.subcategories))
for _, v := range c.subcategories {
r = append(r, v.Id)
r = append(r, v.ID)
}
return r
}
Expand All @@ -83,22 +84,22 @@ func (c *Category) basePath() string {
if c.Locale != "" {
loc = "_" + c.Locale
}
return fmt.Sprintf("/contents%s/%s", loc, c.Id)
return fmt.Sprintf("contents%s/%s", loc, c.ID)
}

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

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

func (c *Category) SetPath(filepath string) error {
p := catPath.FindStringSubmatch(filepath)
if len(p) == 0 {
return ErrContent
}
c.Locale = p[1]
c.Id = p[2]
c.ID = p[2]
return nil
}

Expand Down
22 changes: 10 additions & 12 deletions component/cmp_checklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type Checklist struct {
parent *Subcategory
parent *Difficulty
Hash string `json:"hash"`
Checks []Check `json:"checks"`
}
Expand All @@ -17,8 +17,7 @@ func (c *Checklist) Resource() Resource {
var content = make([]map[string]string, 0, len(c.Checks))
for _, c := range c.Checks {
content = append(content, map[string]string{
"difficulty": c.Difficulty,
"text": c.Text,
"text": c.Text,
})
}
return Resource{
Expand All @@ -28,17 +27,16 @@ func (c *Checklist) Resource() Resource {
}

type Check struct {
Difficulty string `json:"difficulty"`
Text string `json:"text"`
NoCheck bool `json:"no_check"`
Text string `json:"text"`
NoCheck bool `json:"no_check"`
}

func (c *Check) order() []string { return []string{"Text", "Difficulty", "NoCheck"} }
func (c *Check) pointers() args { return args{&c.Text, &c.Difficulty, &c.NoCheck} }
func (c *Check) values() args { return args{c.Text, c.Difficulty, c.NoCheck} }
func (c *Check) order() []string { return []string{"Text", "NoCheck"} }
func (c *Check) pointers() args { return args{&c.Text, &c.NoCheck} }
func (c *Check) values() args { return args{c.Text, c.NoCheck} }

func (c *Checklist) SetParent(s *Subcategory) {
c.parent = s
func (c *Checklist) SetParent(d *Difficulty) {
c.parent = d
}

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

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

func (*Checklist) SetPath(filepath string) error {
p := checklistPath.FindString(filepath)
Expand Down
164 changes: 164 additions & 0 deletions component/cmp_difficulty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package component

import (
"encoding/json"
"fmt"
"regexp"
)

type Difficulty struct {
parent *Subcategory
ID string `json:"id"`
Descr string `json:"description"`
Hash string `json:"hash"`
items []*Item
checklist *Checklist
}

func (d *Difficulty) Resource() Resource {
return Resource{
Slug: d.parent.Resource().Slug + "_" + d.ID,
Content: []map[string]string{
map[string]string{"description": d.Descr},
},
}
}

func (d *Difficulty) HasChildren() bool {
return len(d.items) != 0
}

func (d *Difficulty) Tree(html bool) interface{} {
var items = make([]Item, len(d.items))
for i, v := range d.items {
items[i] = *v
if html {
items[i].Body = items[i].htmlBody
}
}
return map[string]interface{}{
"id": d.ID,
"description": d.Descr,
"items": items,
"checks": d.checklist.Checks,
}
}

func (d *Difficulty) SHA() string {
return d.Hash
}

func (d *Difficulty) SetParent(s *Subcategory) {
d.parent = s
}

func (d *Difficulty) MarshalJSON() ([]byte, error) {
var m = map[string]interface{}{
"description": d.Descr,
"items": d.ItemNames(),
"checks": d.checklist.Checks,
}
if d.Hash != "" {
m["hash"] = d.Hash
}
return json.Marshal(m)
}

func (d *Difficulty) Items() []Item {
var dst = make([]Item, len(d.items))
for i, v := range d.items {
dst[i] = *v
}
return dst
}

func (d *Difficulty) ItemNames() []string {
var r = make([]string, 0, len(d.items))
for i := range d.items {
r = append(r, d.items[i].ID)
}
return r
}

func (d *Difficulty) Checks() *Checklist {
var dst []Check
if d.checklist == nil {
d.SetChecks(new(Checklist))
}
dst = make([]Check, len(d.checklist.Checks))
for i, v := range d.checklist.Checks {
dst[i] = v
}
return &Checklist{
parent: d,
Hash: d.checklist.Hash,
Checks: dst,
}
}

func (d *Difficulty) AddChecks(c ...Check) {
if d.checklist == nil {
d.SetChecks(new(Checklist))
}
d.checklist.Add(c...)
}

func (d *Difficulty) SetChecks(c *Checklist) {
d.checklist = c
c.parent = d
}

func (d *Difficulty) AddItem(items ...*Item) error {
for _, v := range items {
if d.Item(v.ID) != nil {
return fmt.Errorf("item %s exists in %s/%s", v.ID, d.parent.ID, d.ID)
}
v.parent = d
}
d.items = append(d.items, items...)
return nil
}

func (d *Difficulty) Item(id string) *Item {
for _, v := range d.items {
if v.ID == id {
return v
}
}
return nil
}

func (d *Difficulty) basePath() string {
return fmt.Sprintf("%s/%s", d.parent.basePath(), d.ID)
}

func (d *Difficulty) Path() string {
return fmt.Sprintf("%s/.metadata%s", d.basePath(), fileExt)
}

var diffPath = regexp.MustCompile("contents(?:_[a-z]{2})?/[^/]+/[^/]+/([^/]+)/.metadata.md")

func (d *Difficulty) SetPath(filepath string) error {
p := diffPath.FindStringSubmatch(filepath)
if len(p) == 0 {
return ErrContent
}
d.ID = p[1]
return nil
}

func (d *Difficulty) order() []string { return []string{"Description"} }
func (d *Difficulty) pointers() args { return args{&d.Descr} }
func (d *Difficulty) values() args { return args{d.Descr} }

func (d *Difficulty) Contents() string { return getMeta(d) }

func (d *Difficulty) SetContents(contents string) error {
if err := checkMeta(contents, d); err != nil {
return err
}
if d.checklist == nil {
d.checklist = new(Checklist)
}
return setMeta(contents, d)
}
10 changes: 5 additions & 5 deletions component/cmp_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type Form struct {
Id string `json:"-"`
ID string `json:"id"`
Name string `json:"name"`
Hash string `json:"hash"`
Locale string `json:"-"`
Expand All @@ -17,7 +17,7 @@ type Form struct {

func (f *Form) Resource() Resource {
return Resource{
Slug: f.Id,
Slug: f.ID,
Content: []map[string]string{
map[string]string{"name": f.Name},
},
Expand All @@ -33,18 +33,18 @@ func (f *Form) Path() string {
if f.Locale != "" {
loc = "_" + f.Locale
}
return fmt.Sprintf("/forms%s/%s%s", loc, f.Id, fileExt)
return fmt.Sprintf("forms%s/%s%s", loc, f.ID, fileExt)
}

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

func (f *Form) SetPath(filepath string) error {
p := formPath.FindStringSubmatch(filepath)
if len(p) == 0 {
return ErrContent
}
f.Locale = p[1]
f.Id = p[2]
f.ID = p[2]
return nil
}

Expand Down
Loading

0 comments on commit 04aeef2

Please sign in to comment.