| 1 | package suite |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | ) |
| 7 | |
| 8 | // Authorization is what S2 answers for one item, for one named asker. |
| 9 | // |
| 10 | // Doctrine §4.3: four values, none collapsible into another. Only |
| 11 | // NOT_VISIBLE may be silent; everything else that is not VISIBLE withholds |
| 12 | // loudly and names the app. |
| 13 | type Authorization string |
| 14 | |
| 15 | const ( |
| 16 | // VISIBLE: this person may read this revision. |
| 17 | VISIBLE Authorization = "VISIBLE" |
| 18 | // NOT_VISIBLE: this person may not read it. The only value a consumer |
| 19 | // may act on silently. |
| 20 | NOT_VISIBLE Authorization = "NOT_VISIBLE" |
| 21 | // STALE: the item is readable, but the app of record has moved past the |
| 22 | // revision asked about. A synchronisation condition, never a denial. |
| 23 | STALE Authorization = "STALE" |
| 24 | // UNKNOWN: the app of record did not answer usefully, or cannot vouch |
| 25 | // for the revision asked about. Withheld loudly. |
| 26 | UNKNOWN Authorization = "UNKNOWN" |
| 27 | ) |
| 28 | |
| 29 | // ItemRevision is one pair in an S2 question: which item, at which revision. |
| 30 | type ItemRevision struct { |
| 31 | ItemID string `json:"item_id"` |
| 32 | Revision int64 `json:"revision"` |
| 33 | } |
| 34 | |
| 35 | type s2Result struct { |
| 36 | ItemID *string `json:"item_id"` |
| 37 | Revision *int64 `json:"revision"` |
| 38 | Result *Authorization `json:"result"` |
| 39 | } |
| 40 | |
| 41 | type s2Body struct { |
| 42 | Results []s2Result `json:"results"` |
| 43 | } |
| 44 | |
| 45 | // DecodeS2 turns an S2 answer into exactly one Authorization per pair asked, |
| 46 | // in the order they were asked. |
| 47 | func DecodeS2(asked []ItemRevision, status int, body []byte) []Authorization { |
| 48 | // Whole-call failures. A body that arrives with a non-2xx status is not |
| 49 | // an answer however well it parses, and a body that does not parse is |
| 50 | // not an answer however it arrived. |
| 51 | if status < 200 || status > 299 { |
| 52 | return allUnknown(len(asked)) |
| 53 | } |
| 54 | // Before decoding, not after: a duplicate key is resolved by encoding/json |
| 55 | // and gone by the time the duplicate-row rule below could see it. |
| 56 | if err := rejectAmbiguousJSON(body); err != nil { |
| 57 | return allUnknown(len(asked)) |
| 58 | } |
| 59 | // Strict, as S1 is: a result row is (item_id, revision, result) and |
| 60 | // nothing else. A row carrying a title or a passage is a sibling leaking |
| 61 | // into a decoder that would otherwise drop the field unseen; it is not an |
| 62 | // answer (Fable, review of c38e3ad, M5). |
| 63 | dec := json.NewDecoder(bytes.NewReader(body)) |
| 64 | dec.DisallowUnknownFields() |
| 65 | var parsed s2Body |
| 66 | if err := dec.Decode(&parsed); err != nil { |
| 67 | return allUnknown(len(asked)) |
| 68 | } |
| 69 | |
| 70 | byPair := make(map[ItemRevision]Authorization, len(parsed.Results)) |
| 71 | for _, r := range parsed.Results { |
| 72 | if r.ItemID == nil || r.Revision == nil || r.Result == nil { |
| 73 | return allUnknown(len(asked)) |
| 74 | } |
| 75 | pair := ItemRevision{ItemID: *r.ItemID, Revision: *r.Revision} |
| 76 | if _, already := byPair[pair]; already { |
| 77 | // Answered twice. Two answers are not an answer, and choosing |
| 78 | // between them would be this client making a decision the app of |
| 79 | // record did not make. |
| 80 | byPair[pair] = UNKNOWN |
| 81 | continue |
| 82 | } |
| 83 | byPair[pair] = known(*r.Result) |
| 84 | } |
| 85 | |
| 86 | out := make([]Authorization, len(asked)) |
| 87 | for i, pair := range asked { |
| 88 | answer, answered := byPair[pair] |
| 89 | if !answered { |
| 90 | answer = UNKNOWN |
| 91 | } |
| 92 | out[i] = answer |
| 93 | } |
| 94 | return out |
| 95 | } |
| 96 | |
| 97 | // known maps anything outside the closed four-value vocabulary to UNKNOWN. |
| 98 | // A fifth word is a sibling this client does not understand; passing it |
| 99 | // through would hand a caller a value it cannot switch on. |
| 100 | func known(a Authorization) Authorization { |
| 101 | switch a { |
| 102 | case VISIBLE, NOT_VISIBLE, STALE, UNKNOWN: |
| 103 | return a |
| 104 | } |
| 105 | return UNKNOWN |
| 106 | } |
| 107 | |
| 108 | func allUnknown(n int) []Authorization { |
| 109 | out := make([]Authorization, n) |
| 110 | for i := range out { |
| 111 | out[i] = UNKNOWN |
| 112 | } |
| 113 | return out |
| 114 | } |
| 115 | |