| Sketch🕴️ | 305f817 | 2026-02-27 13:58:43 +0400 | [diff] [blame^] | 1 | package loop |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "sync" |
| 7 | |
| 8 | "github.com/invopop/jsonschema" |
| 9 | ) |
| 10 | |
| 11 | type Comment struct { |
| 12 | Author string `json:"author"` |
| 13 | Comment string `json:"comment"` |
| 14 | } |
| 15 | |
| 16 | func (c Comment) String() string { |
| 17 | return fmt.Sprintf("%s: %s", c.Author, c.Comment) |
| 18 | } |
| 19 | |
| 20 | type 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 | |
| 31 | const tmpl = `%s: %s |
| 32 | %s |
| 33 | %s |
| 34 | %s` |
| 35 | |
| 36 | func (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 | |
| 48 | func 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 | } |