| 1 | // Package updatewords holds every member-facing string the instance update |
| 2 | // settings plans add (plans 2 to 5), as drafts with an ID and a context. The |
| 3 | // copy review (plan 5, Task 1) exports All(), Paul rewrites them, and the |
| 4 | // approved text replaces each Text; a test then pins them to the approved |
| 5 | // record. Code reads a string only through Text, so a reviewed string has one |
| 6 | // home. Each plan adds its own words_*.go file, so parallel branches never |
| 7 | // edit the same lines. |
| 8 | package updatewords |
| 9 | |
| 10 | import ( |
| 11 | "fmt" |
| 12 | "sort" |
| 13 | ) |
| 14 | |
| 15 | // Draft is one string: Context says where it appears and what it does, for |
| 16 | // the reviewer who cannot see the screen. |
| 17 | type Draft struct{ ID, Text, Context string } |
| 18 | |
| 19 | var drafts = map[string]Draft{} |
| 20 | |
| 21 | // add registers drafts from a file's init. A duplicate ID is a programming |
| 22 | // error and panics at start-up, never at the moment a member reads it. |
| 23 | func add(ds ...Draft) { |
| 24 | for _, d := range ds { |
| 25 | if _, dup := drafts[d.ID]; dup { |
| 26 | panic(fmt.Sprintf("updatewords: duplicate id %s", d.ID)) |
| 27 | } |
| 28 | drafts[d.ID] = d |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Text is the string for id. An unknown id panics: it can only be a typo in |
| 33 | // code, which the package's tests catch. |
| 34 | func Text(id string) string { |
| 35 | d, ok := drafts[id] |
| 36 | if !ok { |
| 37 | panic(fmt.Sprintf("updatewords: unknown id %s", id)) |
| 38 | } |
| 39 | return d.Text |
| 40 | } |
| 41 | |
| 42 | // All is every draft, sorted by ID. |
| 43 | func All() []Draft { |
| 44 | out := make([]Draft, 0, len(drafts)) |
| 45 | for _, d := range drafts { |
| 46 | out = append(out, d) |
| 47 | } |
| 48 | sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID }) |
| 49 | return out |
| 50 | } |
| 51 | |