carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package fixture
2
3// Not compiled. Every sink here takes a sentence the gate can read.
4
5const noKey = "this box has no secrets key"
6
7func literal(w int) {
8 jsonError(w, 400, "invalid region")
9}
10
11func constant(w int) {
12 jsonError(w, 400, noKey)
13}
14
15func concat(w int, to string) {
16 jsonError(w, 400, "region "+to+" is not armed")
17}
18
19func sprintf(w int, n int, region string) {
20 jsonError(w, 400, fmt.Sprintf("%d boxes publish %s", n, region))
21}
22
23func viaHelper(w int, n int) {
24 jsonError(w, 400, tooMany(n))
25}
26
27func 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.
35func 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.
44func multi(w int) {
45 text, _ := explain(1)
46 jsonError(w, 400, text)
47}
48
49func 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.
57func viaParam(w int) {
58 jsonError(w, 400, wrapOK("first"))
59}
60
61func wrapOK(m string) string {
62 return m
63}
64
65func callsAreLiterals() string {
66 return wrapOK("second")
67}
68
69// A field whose every write, in the struct that is read, is a sentence.
70type Check struct{ Reason string }
71
72func makeCheck() Check {
73 return Check{Reason: "this box has no secrets key"}
74}
75
76func 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.
81func selects(w int, err error) {
82 jsonError(w, 400, pick(err))
83}
84
85func 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.
93func 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.
98type local struct{}
99
100func (local) Message() string { return "the app is not enabled" }
101
102func 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>.
108func label(host string) string {
109 return "host " + host
110}
111
112func viaLabel(w int, h string) {
113 jsonError(w, 400, fmt.Sprintf("%s has no seat", label(h)))
114}
115
116type target struct{ Name string }
117
118func makeTarget(n string) target {
119 return target{Name: n}
120}
121
122func 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>.
128func joined(codes []string) string {
129 return strings.Join(codes, ", ")
130}
131
132func viaJoined(w int, codes []string) {
133 jsonError(w, 400, fmt.Sprintf("the staging codes are %s", joined(codes)))
134}
135