| 1 | // Package review is the review record, the-valley/review/v1: a reviewer's |
| 2 | // judgment of one version of a change, and the history that keeps every |
| 3 | // judgment of the change. |
| 4 | // |
| 5 | // A review says whether its reviewer approves the version it read or |
| 6 | // requests changes to it. It carries a summary, and notes anchored to the |
| 7 | // files the reviewer read. Human and agent reviewers write the same record; |
| 8 | // an agent's adds the provenance of its run. A review may instead withdraw |
| 9 | // an earlier review of the same change, named by its ID. Nothing else takes |
| 10 | // a judgment back: a later approval does not clear an earlier request for |
| 11 | // changes. |
| 12 | // |
| 13 | // A change's reviews live at refs/the-valley/reviews/<target>/<change ID>. |
| 14 | // The ref points at the newest review commit, whose tree holds one file, |
| 15 | // review.json: |
| 16 | // |
| 17 | // { |
| 18 | // "schema": "the-valley/review/v1", |
| 19 | // "review_id": "<32 lowercase hex digits>", |
| 20 | // "change_id": "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz", |
| 21 | // "target": "main", |
| 22 | // "review": "operator", |
| 23 | // "reviewer": "alice", |
| 24 | // "verdict": "request-changes", |
| 25 | // "withdraws": null, |
| 26 | // "binding": { |
| 27 | // "record": "git-sha1:<the change record commit read>", |
| 28 | // "version": 2, |
| 29 | // "head": "git-sha1:<version 2's head>", |
| 30 | // "target_tip": "git-sha1:<the target tip judged against>", |
| 31 | // "base": "git-sha1:<the merge base of the tip and the head>", |
| 32 | // "delta": "<the SHA-256 of the valley-delta-v1 encoding from base to head>" |
| 33 | // }, |
| 34 | // "summary": "Split the second paragraph.", |
| 35 | // "notes": [ |
| 36 | // {"side": "after", "path": "docs/guide.md", "blob": "git-sha1:<the file read>", |
| 37 | // "lines": {"start": 12, "end": 14}, "body": "This sentence carries two ideas."} |
| 38 | // ], |
| 39 | // "provenance": null |
| 40 | // } |
| 41 | // |
| 42 | // The binding says exactly what the reviewer read (binding.go), and anyone |
| 43 | // holding the objects can recompute it. The history keeps every review in |
| 44 | // order (history.go). |
| 45 | // |
| 46 | // Reading is as strict as a change record's: valid UTF-8, no key twice in |
| 47 | // any object, no field the schema does not have, every field of exactly |
| 48 | // its type, every string and list within its bound, and nothing after the |
| 49 | // object. A review is at most MaxRecord bytes. |
| 50 | // |
| 51 | // The package depends on the standard library, the change record |
| 52 | // (../change) and valley-delta-v1 (../delta), and on git for reading and |
| 53 | // writing objects. |
| 54 | package review |
| 55 | |
| 56 | import ( |
| 57 | "bytes" |
| 58 | "crypto/rand" |
| 59 | "encoding/hex" |
| 60 | "encoding/json" |
| 61 | "errors" |
| 62 | "fmt" |
| 63 | "regexp" |
| 64 | "strconv" |
| 65 | "strings" |
| 66 | "unicode" |
| 67 | "unicode/utf8" |
| 68 | |
| 69 | "the-valley/change" |
| 70 | ) |
| 71 | |
| 72 | // Schema names the record format. |
| 73 | const Schema = "the-valley/review/v1" |
| 74 | |
| 75 | // The verdicts a review gives. approve and request-changes judge the |
| 76 | // version the binding names. withdraw judges nothing: it takes back the |
| 77 | // earlier review it names. |
| 78 | const ( |
| 79 | Approve = "approve" |
| 80 | RequestChanges = "request-changes" |
| 81 | Withdraw = "withdraw" |
| 82 | ) |
| 83 | |
| 84 | // The sides of the comparison a note is anchored to: the merge base the |
| 85 | // version was compared from, or the version's head. |
| 86 | const ( |
| 87 | Before = "before" |
| 88 | After = "after" |
| 89 | ) |
| 90 | |
| 91 | // The bounds a review is held to. |
| 92 | const ( |
| 93 | MaxRecord = 64 << 10 // bytes in review.json |
| 94 | MaxName = 64 // bytes in a review's name |
| 95 | MaxSummary = 8 << 10 // bytes in a summary |
| 96 | MaxNotes = 256 // notes in one review |
| 97 | MaxNoteBody = 8 << 10 // bytes in a note's body |
| 98 | MaxPath = 4095 // bytes in a note's path, Linux's PATH_MAX less its NUL |
| 99 | MaxLine = 1 << 24 // the highest line a note may name |
| 100 | MaxRunText = 256 // bytes in a provenance's harness or model |
| 101 | MaxTokens = 1000000000000 // tokens a provenance may report, input or output |
| 102 | ) |
| 103 | |
| 104 | // IDLength is the length of a review ID: 32 lowercase hex digits. |
| 105 | const IDLength = 32 |
| 106 | |
| 107 | // Record is one review. |
| 108 | type Record struct { |
| 109 | Schema string `json:"schema"` |
| 110 | ReviewID string `json:"review_id"` |
| 111 | ChangeID string `json:"change_id"` |
| 112 | Target string `json:"target"` |
| 113 | Review string `json:"review"` |
| 114 | Reviewer string `json:"reviewer"` |
| 115 | Verdict string `json:"verdict"` |
| 116 | Withdraws *string `json:"withdraws"` |
| 117 | Binding *Binding `json:"binding"` |
| 118 | Summary string `json:"summary"` |
| 119 | Notes []Note `json:"notes"` |
| 120 | Provenance *Provenance `json:"provenance"` |
| 121 | } |
| 122 | |
| 123 | // Binding is the version a review judged and what the reviewer read of |
| 124 | // it: the change record commit that names the version, the version's |
| 125 | // number and head, the target tip it was judged against, the merge base |
| 126 | // of that tip and the head, and the digest of the valley-delta-v1 delta |
| 127 | // from the base to the head. The commits are git-sha1:<40 hex>, and the |
| 128 | // digest is 64 lowercase hex digits. |
| 129 | type Binding struct { |
| 130 | Record string `json:"record"` |
| 131 | Version int `json:"version"` |
| 132 | Head string `json:"head"` |
| 133 | TargetTip string `json:"target_tip"` |
| 134 | Base string `json:"base"` |
| 135 | Delta string `json:"delta"` |
| 136 | } |
| 137 | |
| 138 | // Note is one piece of feedback, anchored to a file the reviewer read: its |
| 139 | // side, its path, the blob the path held on that side, and an optional |
| 140 | // range of lines, numbered from 1. A note on deleted lines is anchored to |
| 141 | // the before side. A note on a whole file has no lines. |
| 142 | type Note struct { |
| 143 | Side string `json:"side"` |
| 144 | Path string `json:"path"` |
| 145 | Blob string `json:"blob"` |
| 146 | Lines *Lines `json:"lines"` |
| 147 | Body string `json:"body"` |
| 148 | } |
| 149 | |
| 150 | // Lines is a range of lines, from Start to End inclusive, numbered from 1. |
| 151 | type Lines struct { |
| 152 | Start int `json:"start"` |
| 153 | End int `json:"end"` |
| 154 | } |
| 155 | |
| 156 | // Provenance is how an agent reviewer's judgment was produced: the |
| 157 | // harness that ran it and its version, the model, the SHA-256 of the |
| 158 | // generation settings and of the manifest of inputs, both in lowercase |
| 159 | // hex, and the tokens the run reported using, nil where it reported none. |
| 160 | // A human's review has none. |
| 161 | type Provenance struct { |
| 162 | Harness string `json:"harness"` |
| 163 | Model string `json:"model"` |
| 164 | Settings string `json:"settings"` |
| 165 | Inputs string `json:"inputs"` |
| 166 | InputTokens *int64 `json:"input_tokens"` |
| 167 | OutputTokens *int64 `json:"output_tokens"` |
| 168 | } |
| 169 | |
| 170 | // Judges reports whether the review judges a version, which every review |
| 171 | // but a withdrawal does. |
| 172 | func (r *Record) Judges() bool { |
| 173 | return r.Verdict == Approve || r.Verdict == RequestChanges |
| 174 | } |
| 175 | |
| 176 | // NewID makes a review ID: 16 random bytes in lowercase hex. A client |
| 177 | // makes one when it first writes a review and keeps it through every |
| 178 | // retry of publishing it. |
| 179 | func NewID() (string, error) { |
| 180 | var raw [IDLength / 2]byte |
| 181 | if _, err := rand.Read(raw[:]); err != nil { |
| 182 | return "", err |
| 183 | } |
| 184 | return hex.EncodeToString(raw[:]), nil |
| 185 | } |
| 186 | |
| 187 | // Parse reads review.json, strictly. |
| 188 | func Parse(data []byte) (*Record, error) { |
| 189 | if len(data) > MaxRecord { |
| 190 | return nil, fmt.Errorf("it is %d bytes, more than the %d a review may be", len(data), MaxRecord) |
| 191 | } |
| 192 | v, err := readJSON(data) |
| 193 | if err != nil { |
| 194 | return nil, err |
| 195 | } |
| 196 | o, err := fieldsOf(v, "the review", "schema", "review_id", "change_id", "target", "review", "reviewer", |
| 197 | "verdict", "withdraws", "binding", "summary", "notes", "provenance") |
| 198 | if err != nil { |
| 199 | return nil, err |
| 200 | } |
| 201 | r := &Record{} |
| 202 | for _, f := range []struct { |
| 203 | name string |
| 204 | into *string |
| 205 | }{ |
| 206 | {"schema", &r.Schema}, {"review_id", &r.ReviewID}, {"change_id", &r.ChangeID}, {"target", &r.Target}, |
| 207 | {"review", &r.Review}, {"reviewer", &r.Reviewer}, {"verdict", &r.Verdict}, {"summary", &r.Summary}, |
| 208 | } { |
| 209 | if *f.into, err = stringOf(o[f.name], f.name); err != nil { |
| 210 | return nil, err |
| 211 | } |
| 212 | } |
| 213 | if r.Withdraws, err = optionalString(o["withdraws"], "withdraws"); err != nil { |
| 214 | return nil, err |
| 215 | } |
| 216 | if r.Binding, err = bindingOf(o["binding"]); err != nil { |
| 217 | return nil, err |
| 218 | } |
| 219 | if r.Notes, err = notesOf(o["notes"]); err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | if r.Provenance, err = provenanceOf(o["provenance"]); err != nil { |
| 223 | return nil, err |
| 224 | } |
| 225 | if err := r.Check(); err != nil { |
| 226 | return nil, err |
| 227 | } |
| 228 | return r, nil |
| 229 | } |
| 230 | |
| 231 | func bindingOf(v *value) (*Binding, error) { |
| 232 | if isNull(v) { |
| 233 | return nil, nil |
| 234 | } |
| 235 | o, err := fieldsOf(v, "its binding", "record", "version", "head", "target_tip", "base", "delta") |
| 236 | if err != nil { |
| 237 | return nil, err |
| 238 | } |
| 239 | b := &Binding{} |
| 240 | for _, f := range []struct { |
| 241 | name string |
| 242 | into *string |
| 243 | }{ |
| 244 | {"record", &b.Record}, {"head", &b.Head}, {"target_tip", &b.TargetTip}, {"base", &b.Base}, {"delta", &b.Delta}, |
| 245 | } { |
| 246 | if *f.into, err = stringOf(o[f.name], "binding."+f.name); err != nil { |
| 247 | return nil, err |
| 248 | } |
| 249 | } |
| 250 | n, err := integerOf(o["version"], "binding.version", 1, change.MaxVersions) |
| 251 | if err != nil { |
| 252 | return nil, err |
| 253 | } |
| 254 | b.Version = int(n) |
| 255 | return b, nil |
| 256 | } |
| 257 | |
| 258 | func notesOf(v *value) ([]Note, error) { |
| 259 | if v.kind != kindArray { |
| 260 | return nil, fmt.Errorf("its notes is %s, where an array is expected", v.kind) |
| 261 | } |
| 262 | if len(v.items) > MaxNotes { |
| 263 | return nil, fmt.Errorf("it holds %d notes, more than the %d a review may", len(v.items), MaxNotes) |
| 264 | } |
| 265 | notes := []Note{} |
| 266 | for i, item := range v.items { |
| 267 | where := fmt.Sprintf("notes[%d]", i) |
| 268 | o, err := fieldsOf(item, where, "side", "path", "blob", "lines", "body") |
| 269 | if err != nil { |
| 270 | return nil, err |
| 271 | } |
| 272 | var n Note |
| 273 | for _, f := range []struct { |
| 274 | name string |
| 275 | into *string |
| 276 | }{ |
| 277 | {"side", &n.Side}, {"path", &n.Path}, {"blob", &n.Blob}, {"body", &n.Body}, |
| 278 | } { |
| 279 | if *f.into, err = stringOf(o[f.name], where+"."+f.name); err != nil { |
| 280 | return nil, err |
| 281 | } |
| 282 | } |
| 283 | if lines := o["lines"]; !isNull(lines) { |
| 284 | lo, err := fieldsOf(lines, where+".lines", "start", "end") |
| 285 | if err != nil { |
| 286 | return nil, err |
| 287 | } |
| 288 | start, err := integerOf(lo["start"], where+".lines.start", 1, MaxLine) |
| 289 | if err != nil { |
| 290 | return nil, err |
| 291 | } |
| 292 | end, err := integerOf(lo["end"], where+".lines.end", 1, MaxLine) |
| 293 | if err != nil { |
| 294 | return nil, err |
| 295 | } |
| 296 | n.Lines = &Lines{Start: int(start), End: int(end)} |
| 297 | } |
| 298 | notes = append(notes, n) |
| 299 | } |
| 300 | return notes, nil |
| 301 | } |
| 302 | |
| 303 | func provenanceOf(v *value) (*Provenance, error) { |
| 304 | if isNull(v) { |
| 305 | return nil, nil |
| 306 | } |
| 307 | o, err := fieldsOf(v, "its provenance", "harness", "model", "settings", "inputs", "input_tokens", "output_tokens") |
| 308 | if err != nil { |
| 309 | return nil, err |
| 310 | } |
| 311 | p := &Provenance{} |
| 312 | for _, f := range []struct { |
| 313 | name string |
| 314 | into *string |
| 315 | }{ |
| 316 | {"harness", &p.Harness}, {"model", &p.Model}, {"settings", &p.Settings}, {"inputs", &p.Inputs}, |
| 317 | } { |
| 318 | if *f.into, err = stringOf(o[f.name], "provenance."+f.name); err != nil { |
| 319 | return nil, err |
| 320 | } |
| 321 | } |
| 322 | for _, f := range []struct { |
| 323 | name string |
| 324 | into **int64 |
| 325 | }{ |
| 326 | {"input_tokens", &p.InputTokens}, {"output_tokens", &p.OutputTokens}, |
| 327 | } { |
| 328 | if isNull(o[f.name]) { |
| 329 | continue |
| 330 | } |
| 331 | n, err := integerOf(o[f.name], "provenance."+f.name, 0, MaxTokens) |
| 332 | if err != nil { |
| 333 | return nil, err |
| 334 | } |
| 335 | *f.into = &n |
| 336 | } |
| 337 | return p, nil |
| 338 | } |
| 339 | |
| 340 | // Check holds a review to everything the schema says beyond the types of |
| 341 | // its fields: the formats and bounds of its strings and lists, and what |
| 342 | // its fields say about one another. A review that judges a version has a |
| 343 | // binding and withdraws nothing. A withdrawal names the review it takes |
| 344 | // back, which is not itself, and has no binding and no notes. Every review |
| 345 | // has a summary. |
| 346 | func (r *Record) Check() error { |
| 347 | switch { |
| 348 | case r.Schema != Schema: |
| 349 | return fmt.Errorf("its schema is %q, where %q is expected", r.Schema, Schema) |
| 350 | case !ValidID(r.ReviewID): |
| 351 | return fmt.Errorf("its review_id %q is not a review ID: %d lowercase hex digits", r.ReviewID, IDLength) |
| 352 | case !change.ValidID(r.ChangeID): |
| 353 | return fmt.Errorf("its change_id %q is not a change ID: 32 letters from k to z", r.ChangeID) |
| 354 | case !change.ValidBranch(r.Target) || !utf8.ValidString(r.Target): |
| 355 | return fmt.Errorf("its target %q is not a branch name in UTF-8", r.Target) |
| 356 | case !ValidName(r.Review): |
| 357 | return fmt.Errorf("its review %q is not a review's name: 1 to %d lowercase letters, digits and dashes, opening with a letter", r.Review, MaxName) |
| 358 | case !change.ValidPrincipal(r.Reviewer): |
| 359 | return fmt.Errorf("its reviewer %q is not a principal name", r.Reviewer) |
| 360 | case !validText(r.Summary, MaxSummary): |
| 361 | return fmt.Errorf("its summary is not 1 to %d bytes free of control characters but newline and tab", MaxSummary) |
| 362 | case len(r.Notes) > MaxNotes: |
| 363 | return fmt.Errorf("it holds %d notes, more than the %d a review may", len(r.Notes), MaxNotes) |
| 364 | } |
| 365 | switch r.Verdict { |
| 366 | case Approve, RequestChanges: |
| 367 | switch { |
| 368 | case r.Withdraws != nil: |
| 369 | return fmt.Errorf("its verdict is %s, and only a withdrawal names a review in withdraws", r.Verdict) |
| 370 | case r.Binding == nil: |
| 371 | return fmt.Errorf("its verdict is %s, and a review that judges a version has a binding", r.Verdict) |
| 372 | } |
| 373 | if err := r.Binding.check(); err != nil { |
| 374 | return err |
| 375 | } |
| 376 | case Withdraw: |
| 377 | switch { |
| 378 | case r.Withdraws == nil || !ValidID(*r.Withdraws): |
| 379 | return fmt.Errorf("it is a withdrawal, and its withdraws is not a review ID: %d lowercase hex digits", IDLength) |
| 380 | case *r.Withdraws == r.ReviewID: |
| 381 | return errors.New("it is a withdrawal of itself, and a withdrawal takes back an earlier review") |
| 382 | case r.Binding != nil: |
| 383 | return errors.New("it is a withdrawal, and a withdrawal judges no version, so it has no binding") |
| 384 | case len(r.Notes) > 0: |
| 385 | return errors.New("it is a withdrawal, and a withdrawal has no notes: its reason is its summary") |
| 386 | } |
| 387 | default: |
| 388 | return fmt.Errorf("its verdict is %q, where %q, %q or %q is expected", r.Verdict, Approve, RequestChanges, Withdraw) |
| 389 | } |
| 390 | for i, n := range r.Notes { |
| 391 | if err := n.check(fmt.Sprintf("notes[%d]", i)); err != nil { |
| 392 | return err |
| 393 | } |
| 394 | } |
| 395 | if p := r.Provenance; p != nil { |
| 396 | switch { |
| 397 | case !validLine(p.Harness, MaxRunText): |
| 398 | return fmt.Errorf("its provenance.harness is not 1 to %d bytes free of control characters", MaxRunText) |
| 399 | case !validLine(p.Model, MaxRunText): |
| 400 | return fmt.Errorf("its provenance.model is not 1 to %d bytes free of control characters", MaxRunText) |
| 401 | case !validDigest(p.Settings): |
| 402 | return fmt.Errorf("its provenance.settings %q is not a SHA-256 in 64 lowercase hex digits", p.Settings) |
| 403 | case !validDigest(p.Inputs): |
| 404 | return fmt.Errorf("its provenance.inputs %q is not a SHA-256 in 64 lowercase hex digits", p.Inputs) |
| 405 | case p.InputTokens != nil && (*p.InputTokens < 0 || *p.InputTokens > MaxTokens): |
| 406 | return fmt.Errorf("its provenance.input_tokens is %d, outside 0 to %d", *p.InputTokens, int64(MaxTokens)) |
| 407 | case p.OutputTokens != nil && (*p.OutputTokens < 0 || *p.OutputTokens > MaxTokens): |
| 408 | return fmt.Errorf("its provenance.output_tokens is %d, outside 0 to %d", *p.OutputTokens, int64(MaxTokens)) |
| 409 | } |
| 410 | } |
| 411 | return nil |
| 412 | } |
| 413 | |
| 414 | func (b *Binding) check() error { |
| 415 | for _, f := range []struct{ name, value string }{ |
| 416 | {"record", b.Record}, {"head", b.Head}, {"target_tip", b.TargetTip}, {"base", b.Base}, |
| 417 | } { |
| 418 | if !change.ValidHead(f.value) { |
| 419 | return fmt.Errorf("its binding.%s %q is not %s and 40 lowercase hex digits", f.name, f.value, change.HeadPrefix) |
| 420 | } |
| 421 | } |
| 422 | switch { |
| 423 | case b.Version < 1 || b.Version > change.MaxVersions: |
| 424 | return fmt.Errorf("its binding.version is %d, outside 1 to %d", b.Version, change.MaxVersions) |
| 425 | case !validDigest(b.Delta): |
| 426 | return fmt.Errorf("its binding.delta %q is not a SHA-256 in 64 lowercase hex digits", b.Delta) |
| 427 | } |
| 428 | return nil |
| 429 | } |
| 430 | |
| 431 | func (n Note) check(where string) error { |
| 432 | switch { |
| 433 | case n.Side != Before && n.Side != After: |
| 434 | return fmt.Errorf("its %s.side is %q, where %q or %q is expected", where, n.Side, Before, After) |
| 435 | case !change.ValidHead(n.Blob): |
| 436 | return fmt.Errorf("its %s.blob %q is not %s and 40 lowercase hex digits", where, n.Blob, change.HeadPrefix) |
| 437 | case n.Lines != nil && (n.Lines.Start < 1 || n.Lines.End > MaxLine || n.Lines.Start > n.Lines.End): |
| 438 | return fmt.Errorf("its %s.lines run from %d to %d, and lines run forward from 1 to at most %d", where, n.Lines.Start, n.Lines.End, MaxLine) |
| 439 | case !validText(n.Body, MaxNoteBody): |
| 440 | return fmt.Errorf("its %s.body is not 1 to %d bytes free of control characters but newline and tab", where, MaxNoteBody) |
| 441 | } |
| 442 | if err := checkPath(n.Path); err != nil { |
| 443 | return fmt.Errorf("its %s.path %w", where, err) |
| 444 | } |
| 445 | return nil |
| 446 | } |
| 447 | |
| 448 | // checkPath refuses a note's path that names no file a tree could hold |
| 449 | // and a checkout could make: an empty one, one longer than MaxPath, one |
| 450 | // with an empty component, a component that is . or .., .git in any case, |
| 451 | // or longer than 255 bytes, and one holding a control character, which no |
| 452 | // line of git's batch reading can carry. It refuses a path that is not |
| 453 | // UTF-8 too, which JSON cannot carry: encoding it would put U+FFFD in |
| 454 | // place of each invalid byte, and so name another path. A file whose path |
| 455 | // is not UTF-8 takes no note; the summary can speak of it. |
| 456 | func checkPath(p string) error { |
| 457 | switch { |
| 458 | case p == "" || len(p) > MaxPath: |
| 459 | return fmt.Errorf("is not 1 to %d bytes", MaxPath) |
| 460 | case !utf8.ValidString(p): |
| 461 | return errors.New("is not UTF-8") |
| 462 | case strings.ContainsFunc(p, unicode.IsControl): |
| 463 | return errors.New("holds a control character") |
| 464 | } |
| 465 | for _, name := range strings.Split(p, "/") { |
| 466 | switch { |
| 467 | case name == "": |
| 468 | return fmt.Errorf("%q has an empty component", p) |
| 469 | case name == "." || name == "..": |
| 470 | return fmt.Errorf("%q has the component %s", p, name) |
| 471 | case strings.EqualFold(name, ".git"): |
| 472 | return fmt.Errorf("%q has the component %s, which git keeps for itself", p, name) |
| 473 | case len(name) > 255: |
| 474 | return fmt.Errorf("%q has a component longer than Linux's limit of 255 bytes", p) |
| 475 | } |
| 476 | } |
| 477 | return nil |
| 478 | } |
| 479 | |
| 480 | // Encode writes a review as review.json. It refuses a review Parse would |
| 481 | // refuse, so what it writes always reads back as the same review. |
| 482 | func Encode(r *Record) ([]byte, error) { |
| 483 | if err := r.Check(); err != nil { |
| 484 | return nil, fmt.Errorf("the review cannot be written: %w", err) |
| 485 | } |
| 486 | out := *r |
| 487 | if out.Notes == nil { |
| 488 | out.Notes = []Note{} |
| 489 | } |
| 490 | var buf bytes.Buffer |
| 491 | enc := json.NewEncoder(&buf) |
| 492 | enc.SetEscapeHTML(false) |
| 493 | enc.SetIndent("", " ") |
| 494 | if err := enc.Encode(&out); err != nil { |
| 495 | return nil, err |
| 496 | } |
| 497 | if buf.Len() > MaxRecord { |
| 498 | return nil, fmt.Errorf("the review cannot be written: it takes %d bytes, more than the %d a review may", buf.Len(), MaxRecord) |
| 499 | } |
| 500 | return buf.Bytes(), nil |
| 501 | } |
| 502 | |
| 503 | // ValidID reports whether s is a review ID. |
| 504 | func ValidID(s string) bool { |
| 505 | return len(s) == IDLength && lowerHex(s) |
| 506 | } |
| 507 | |
| 508 | // reviewName is the shape of a review's name, such as docs-style or |
| 509 | // operator. |
| 510 | var reviewName = regexp.MustCompile(`^[a-z][a-z0-9-]*$`) |
| 511 | |
| 512 | // ValidName reports whether s is a review's name. |
| 513 | func ValidName(s string) bool { |
| 514 | return len(s) <= MaxName && reviewName.MatchString(s) |
| 515 | } |
| 516 | |
| 517 | func validDigest(s string) bool { |
| 518 | return len(s) == 64 && lowerHex(s) |
| 519 | } |
| 520 | |
| 521 | func lowerHex(s string) bool { |
| 522 | for _, c := range []byte(s) { |
| 523 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 524 | return false |
| 525 | } |
| 526 | } |
| 527 | return true |
| 528 | } |
| 529 | |
| 530 | // validText reports whether s is 1 to max bytes of UTF-8 holding no |
| 531 | // control character but newline and tab: a summary's or a note's prose. |
| 532 | func validText(s string, max int) bool { |
| 533 | if len(s) < 1 || len(s) > max || !utf8.ValidString(s) { |
| 534 | return false |
| 535 | } |
| 536 | return !strings.ContainsFunc(s, func(r rune) bool { return unicode.IsControl(r) && r != '\n' && r != '\t' }) |
| 537 | } |
| 538 | |
| 539 | // validLine reports whether s is 1 to max bytes of UTF-8 holding no |
| 540 | // control character at all. |
| 541 | func validLine(s string, max int) bool { |
| 542 | if len(s) < 1 || len(s) > max || !utf8.ValidString(s) { |
| 543 | return false |
| 544 | } |
| 545 | return !strings.ContainsFunc(s, unicode.IsControl) |
| 546 | } |
| 547 | |
| 548 | // fieldsOf is v's fields, when v is an object holding exactly the fields |
| 549 | // named, no more and no fewer. |
| 550 | func fieldsOf(v *value, what string, names ...string) (map[string]*value, error) { |
| 551 | if v.kind != kindObject { |
| 552 | return nil, fmt.Errorf("%s is %s, where an object is expected", what, v.kind) |
| 553 | } |
| 554 | for _, key := range v.keys { |
| 555 | known := false |
| 556 | for _, n := range names { |
| 557 | known = known || key == n |
| 558 | } |
| 559 | if !known { |
| 560 | return nil, fmt.Errorf("%s holds the field %q, which the schema does not have", what, key) |
| 561 | } |
| 562 | } |
| 563 | for _, n := range names { |
| 564 | if _, ok := v.fields[n]; !ok { |
| 565 | return nil, fmt.Errorf("%s has no field %q", what, n) |
| 566 | } |
| 567 | } |
| 568 | return v.fields, nil |
| 569 | } |
| 570 | |
| 571 | func stringOf(v *value, name string) (string, error) { |
| 572 | if v.kind != kindString { |
| 573 | return "", fmt.Errorf("its %s is %s, where a string is expected", name, v.kind) |
| 574 | } |
| 575 | return v.text, nil |
| 576 | } |
| 577 | |
| 578 | func isNull(v *value) bool { |
| 579 | return v.kind == kindLiteral && v.text == "null" |
| 580 | } |
| 581 | |
| 582 | // optionalString reads a string or null. |
| 583 | func optionalString(v *value, name string) (*string, error) { |
| 584 | if isNull(v) { |
| 585 | return nil, nil |
| 586 | } |
| 587 | s, err := stringOf(v, name) |
| 588 | if err != nil { |
| 589 | return nil, fmt.Errorf("its %s is %s, where a string or null is expected", name, v.kind) |
| 590 | } |
| 591 | return &s, nil |
| 592 | } |
| 593 | |
| 594 | // integerOf reads an integer from min to max. Only a plain run of digits |
| 595 | // is an integer: 3.0, 3e0 and -1 are numbers of another type. |
| 596 | func integerOf(v *value, name string, min, max int64) (int64, error) { |
| 597 | if v.kind != kindNumber { |
| 598 | return 0, fmt.Errorf("its %s is %s, where an integer is expected", name, v.kind) |
| 599 | } |
| 600 | for _, c := range []byte(v.text) { |
| 601 | if c < '0' || c > '9' { |
| 602 | return 0, fmt.Errorf("its %s is %s, where an integer is expected", name, v.text) |
| 603 | } |
| 604 | } |
| 605 | n, err := strconv.ParseInt(v.text, 10, 64) |
| 606 | if err != nil || n < min || n > max { |
| 607 | return 0, fmt.Errorf("its %s is %s, outside %d to %d", name, v.text, min, max) |
| 608 | } |
| 609 | return n, nil |
| 610 | } |