blob: e6398577987729a78ddeb450f527c1159c566467 [file] [log] [blame]
Sketch🕴️305f8172026-02-27 13:58:43 +04001package loop
2
3import (
4 "fmt"
5 "strings"
6 "sync"
7
8 "github.com/invopop/jsonschema"
9)
10
11type Comment struct {
12 Author string `json:"author"`
13 Comment string `json:"comment"`
14}
15
16func (c Comment) String() string {
17 return fmt.Sprintf("%s: %s", c.Author, c.Comment)
18}
19
20type ToDo struct {
21 ID string `json:"id"`
22 Title string `json:"title"`
23 Description string `json:"description"`
24 Items []*ToDo `json:"items"`
25 Done bool `json:"done"`
26 AssignedTo string `json:"assignedTo"`
27 Discussion []Comment `json:"discussion"`
28 lock sync.Locker
29}
30
31const tmpl = `%s: %s
32%s
33%s
34%s`
35
36func (t ToDo) String() string {
37 var comments []string
38 for _, c := range t.Discussion {
39 comments = append(comments, fmt.Sprintf("\t - %s", c.String()))
40 }
41 var items []string
42 for _, i := range t.Items {
43 items = append(items, fmt.Sprintf("\t%s", i.String()))
44 }
45 return fmt.Sprintf(tmpl, t.ID, t.Title, t.Description, strings.Join(comments, "\n"), strings.Join(items, "\n"))
46}
47
48func ToDoJSONSchema() string {
49 reflector := jsonschema.Reflector{
50 AllowAdditionalProperties: false,
51 RequiredFromJSONSchemaTags: true,
52 DoNotReference: false,
53 }
54 var v ToDo
55 s := reflector.Reflect(v)
56 b, err := s.MarshalJSON()
57 if err != nil {
58 panic(err)
59 }
60 return string(b)
61}