| 1 | package fixture |
| 2 | |
| 3 | // Not compiled. Every sink here takes a sentence the gate can read. |
| 4 | |
| 5 | const noKey = "this box has no secrets key" |
| 6 | |
| 7 | func literal(w int) { |
| 8 | jsonError(w, 400, "invalid region") |
| 9 | } |
| 10 | |
| 11 | func constant(w int) { |
| 12 | jsonError(w, 400, noKey) |
| 13 | } |
| 14 | |
| 15 | func concat(w int, to string) { |
| 16 | jsonError(w, 400, "region "+to+" is not armed") |
| 17 | } |
| 18 | |
| 19 | func sprintf(w int, n int, region string) { |
| 20 | jsonError(w, 400, fmt.Sprintf("%d boxes publish %s", n, region)) |
| 21 | } |
| 22 | |
| 23 | func viaHelper(w int, n int) { |
| 24 | jsonError(w, 400, tooMany(n)) |
| 25 | } |
| 26 | |
| 27 | func tooMany(n int) string { |
| 28 | if n > 1 { |
| 29 | return fmt.Sprintf("%d boxes is too many", n) |
| 30 | } |
| 31 | return "one box is too few" |
| 32 | } |
| 33 | |
| 34 | // A local whose every assignment is a sentence. |
| 35 | func localLiteral(w int, bad bool) { |
| 36 | msg := "that region is not armed" |
| 37 | if bad { |
| 38 | msg = "that region is not hosting" |
| 39 | } |
| 40 | jsonError(w, 400, msg) |
| 41 | } |
| 42 | |
| 43 | // A multi-value helper whose string results are all sentences. |
| 44 | func multi(w int) { |
| 45 | text, _ := explain(1) |
| 46 | jsonError(w, 400, text) |
| 47 | } |
| 48 | |
| 49 | func explain(n int) (string, bool) { |
| 50 | if n > 0 { |
| 51 | return "the app is not enabled", true |
| 52 | } |
| 53 | return fmt.Sprintf("%d apps are not enabled", n), false |
| 54 | } |
| 55 | |
| 56 | // A parameter whose every call site passes a sentence. |
| 57 | func viaParam(w int) { |
| 58 | jsonError(w, 400, wrapOK("first")) |
| 59 | } |
| 60 | |
| 61 | func wrapOK(m string) string { |
| 62 | return m |
| 63 | } |
| 64 | |
| 65 | func callsAreLiterals() string { |
| 66 | return wrapOK("second") |
| 67 | } |
| 68 | |
| 69 | // A field whose every write, in the struct that is read, is a sentence. |
| 70 | type Check struct{ Reason string } |
| 71 | |
| 72 | func makeCheck() Check { |
| 73 | return Check{Reason: "this box has no secrets key"} |
| 74 | } |
| 75 | |
| 76 | func viaField(w int, c Check) { |
| 77 | jsonError(w, 400, c.Reason) |
| 78 | } |
| 79 | |
| 80 | // An error that only selects among sentences, and is never spoken. |
| 81 | func selects(w int, err error) { |
| 82 | jsonError(w, 400, pick(err)) |
| 83 | } |
| 84 | |
| 85 | func pick(err error) string { |
| 86 | if err == nil { |
| 87 | return "the copy is fine" |
| 88 | } |
| 89 | return "the copy is not fine" |
| 90 | } |
| 91 | |
| 92 | // A placeholder that is data, not an error. |
| 93 | func dataPlaceholder(w int, host string) { |
| 94 | jsonError(w, 400, fmt.Sprintf("host %s is not armed", host)) |
| 95 | } |
| 96 | |
| 97 | // A method on a receiver declared once. |
| 98 | type local struct{} |
| 99 | |
| 100 | func (local) Message() string { return "the app is not enabled" } |
| 101 | |
| 102 | func viaMethod(w int, m local) { |
| 103 | jsonError(w, 400, m.Message()) |
| 104 | } |
| 105 | |
| 106 | // A helper that dresses data, and a string field written from data: both |
| 107 | // stand for a row's <placeholder>. |
| 108 | func label(host string) string { |
| 109 | return "host " + host |
| 110 | } |
| 111 | |
| 112 | func viaLabel(w int, h string) { |
| 113 | jsonError(w, 400, fmt.Sprintf("%s has no seat", label(h))) |
| 114 | } |
| 115 | |
| 116 | type target struct{ Name string } |
| 117 | |
| 118 | func makeTarget(n string) target { |
| 119 | return target{Name: n} |
| 120 | } |
| 121 | |
| 122 | func viaDataField(w int, t target) { |
| 123 | jsonError(w, 400, fmt.Sprintf("region %s is gone", t.Name)) |
| 124 | } |
| 125 | |
| 126 | // A helper that joins data (stagingCodes' shape): the gate cannot derive it, |
| 127 | // and it carries no error's text, so it stands for a <placeholder>. |
| 128 | func joined(codes []string) string { |
| 129 | return strings.Join(codes, ", ") |
| 130 | } |
| 131 | |
| 132 | func viaJoined(w int, codes []string) { |
| 133 | jsonError(w, 400, fmt.Sprintf("the staging codes are %s", joined(codes))) |
| 134 | } |
| 135 | |