| 1 | package change |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "unicode" |
| 11 | "unicode/utf8" |
| 12 | ) |
| 13 | |
| 14 | // A restack report is what the host restacker observed when it tried to |
| 15 | // rebase a ready change onto its target: the inputs it read, what came of |
| 16 | // the attempt, and what it published. It lives at |
| 17 | // refs/the-valley/restack/<target>/<change ID>. The ref points at a report |
| 18 | // commit whose tree holds one file, report.json, in the schema |
| 19 | // valley-restack-report/v1: |
| 20 | // |
| 21 | // { |
| 22 | // "schema": "valley-restack-report/v1", |
| 23 | // "change_id": "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz", |
| 24 | // "target": "main", |
| 25 | // "target_tip": "git-sha1:<40 hex>", |
| 26 | // "operation": "20261017T203500Z-1", |
| 27 | // "result": "conflict", |
| 28 | // "layers": [ |
| 29 | // {"change_id": "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz", |
| 30 | // "record": "git-sha1:<40 hex>", "version": 2, "head": "git-sha1:<40 hex>", |
| 31 | // "branch": "topic", "branch_tip": "git-sha1:<40 hex>", "published": null} |
| 32 | // ], |
| 33 | // "failing_layer": "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz", |
| 34 | // "conflict_paths": ["src/main.go"], |
| 35 | // "blocking_requests": [], |
| 36 | // "refusal": null |
| 37 | // } |
| 38 | // |
| 39 | // The layers are the changes the attempt rebased together, bottom first, |
| 40 | // each as it was read: its record commit, its active version, and that |
| 41 | // version's head and branch. The branch is "" for a version that names |
| 42 | // none. branch_tip is where origin's branch was when the attempt read it, |
| 43 | // or null when origin had no such branch, which a layer with no branch |
| 44 | // always is. It is read apart from the version's head, because the two can |
| 45 | // differ. published is null for a layer whose branch the attempt did not |
| 46 | // move. Otherwise it is the head the branch was moved to, and the version |
| 47 | // and record commit that recorded it, both null when no version was added. |
| 48 | // |
| 49 | // blocking_requests are the integration requests that held the attempt |
| 50 | // back, each as its ref and the object it pointed at when the attempt read |
| 51 | // it. |
| 52 | // |
| 53 | // Each report commit has the report it replaces as its one parent, and the |
| 54 | // first has none, so the ref holds the change's whole history of reports. |
| 55 | // |
| 56 | // A report is an observation and grants nothing. It is not evidence that |
| 57 | // a version's content is unchanged or approved. A reader decides whether |
| 58 | // it still applies by comparing every ref it observed with what origin |
| 59 | // holds now (../rebase/report.go). |
| 60 | // |
| 61 | // It is read as strictly as a record: valid UTF-8, no key twice, no field |
| 62 | // the schema does not have, every field of exactly its type, and every |
| 63 | // string and list within its bound. The push hook reads a pushed report |
| 64 | // with ParseRestackReport, and valley status reads it back the same way. |
| 65 | |
| 66 | // RestackSchema names the report format. |
| 67 | const RestackSchema = "valley-restack-report/v1" |
| 68 | |
| 69 | // RestackPrefix opens every restack report's ref. |
| 70 | const RestackPrefix = "refs/the-valley/restack/" |
| 71 | |
| 72 | // RestackRef is the ref a change's restack report lives at. |
| 73 | func RestackRef(target, id string) string { |
| 74 | return RestackPrefix + target + "/" + id |
| 75 | } |
| 76 | |
| 77 | // ParseRestackRef splits a report's ref into its target and change ID. It |
| 78 | // reports false for a ref that is not refs/the-valley/restack/<target>/<ID> |
| 79 | // with a branch name as the target and a change ID as the ID. |
| 80 | func ParseRestackRef(ref string) (target, id string, ok bool) { |
| 81 | rest, found := strings.CutPrefix(ref, RestackPrefix) |
| 82 | if !found { |
| 83 | return "", "", false |
| 84 | } |
| 85 | slash := strings.LastIndexByte(rest, '/') |
| 86 | if slash < 0 { |
| 87 | return "", "", false |
| 88 | } |
| 89 | target, id = rest[:slash], rest[slash+1:] |
| 90 | if !ValidBranch(target) || !ValidID(id) { |
| 91 | return "", "", false |
| 92 | } |
| 93 | return target, id, true |
| 94 | } |
| 95 | |
| 96 | // What came of an attempt. A conflict and a block move no branch and add |
| 97 | // no version. partial is an attempt that moved its branches and did not |
| 98 | // add every version; complete is one that did both. superseded is an |
| 99 | // attempt given up because its inputs moved while it ran. |
| 100 | const ( |
| 101 | RestackConflict = "conflict" |
| 102 | RestackBlocked = "blocked" |
| 103 | RestackPartial = "partial" |
| 104 | RestackComplete = "complete" |
| 105 | RestackSuperseded = "superseded" |
| 106 | ) |
| 107 | |
| 108 | // The bounds a report is held to. |
| 109 | const ( |
| 110 | MaxRestackLayers = 64 // layers in one report |
| 111 | MaxConflictPaths = 100 // conflict paths in one report |
| 112 | MaxConflictPath = 1024 // bytes in one conflict path |
| 113 | MaxBlockingRequest = 64 // blocking requests in one report |
| 114 | MaxRequestRef = 256 // bytes in one blocking request's ref |
| 115 | MaxRefusal = 2048 // bytes in a refusal |
| 116 | MaxOperation = 64 // bytes in an operation identifier |
| 117 | ) |
| 118 | |
| 119 | // The bounds on the objects that hold reports, which every reader holds a |
| 120 | // report to before it reads anything past them. A report commit names a |
| 121 | // tree, at most one parent, an author, a committer and a short message, |
| 122 | // and its tree names one file. |
| 123 | const ( |
| 124 | MaxReportCommit = 4 << 10 // bytes in a report commit |
| 125 | MaxReportTree = 1 << 10 // bytes in its tree |
| 126 | MaxReportBlob = 64 << 10 // bytes in its report.json |
| 127 | MaxReportUpdates = 64 // report updates in one push |
| 128 | ) |
| 129 | |
| 130 | // RestackReport is one restack report. |
| 131 | type RestackReport struct { |
| 132 | Schema string `json:"schema"` |
| 133 | ChangeID string `json:"change_id"` |
| 134 | Target string `json:"target"` |
| 135 | TargetTip string `json:"target_tip"` |
| 136 | Operation string `json:"operation"` |
| 137 | Result string `json:"result"` |
| 138 | Layers []RestackLayer `json:"layers"` |
| 139 | Failing *string `json:"failing_layer"` |
| 140 | Conflicts []string `json:"conflict_paths"` |
| 141 | Requests []RestackRequest `json:"blocking_requests"` |
| 142 | Refusal *string `json:"refusal"` |
| 143 | } |
| 144 | |
| 145 | // RestackLayer is one change an attempt rebased, as the attempt read it, |
| 146 | // and what it published for it. Branch is "" for a version that names no |
| 147 | // branch. BranchTip is where origin's branch was when the attempt read it, |
| 148 | // nil when origin had no such branch. |
| 149 | type RestackLayer struct { |
| 150 | ChangeID string `json:"change_id"` |
| 151 | Record string `json:"record"` |
| 152 | Version int `json:"version"` |
| 153 | Head string `json:"head"` |
| 154 | Branch string `json:"branch"` |
| 155 | BranchTip *string `json:"branch_tip"` |
| 156 | Published *RestackPublished `json:"published"` |
| 157 | } |
| 158 | |
| 159 | // RestackPublished is what an attempt published for a layer: the head its |
| 160 | // branch was moved to, and the version and record commit that recorded |
| 161 | // that head, both nil when no version was added. |
| 162 | type RestackPublished struct { |
| 163 | Head string `json:"head"` |
| 164 | Version *int `json:"version"` |
| 165 | Record *string `json:"record"` |
| 166 | } |
| 167 | |
| 168 | // RestackRequest is an integration request that held an attempt back: its |
| 169 | // ref, and the object it pointed at when the attempt read it. |
| 170 | type RestackRequest struct { |
| 171 | Ref string `json:"ref"` |
| 172 | Tip string `json:"tip"` |
| 173 | } |
| 174 | |
| 175 | // Layer is the layer of the change given, if the report has one. |
| 176 | func (r *RestackReport) Layer(id string) (RestackLayer, bool) { |
| 177 | for _, l := range r.Layers { |
| 178 | if l.ChangeID == id { |
| 179 | return l, true |
| 180 | } |
| 181 | } |
| 182 | return RestackLayer{}, false |
| 183 | } |
| 184 | |
| 185 | // ParseRestackReport reads report.json, strictly. |
| 186 | func ParseRestackReport(data []byte) (*RestackReport, error) { |
| 187 | v, err := readJSON(data) |
| 188 | if err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | o, err := fieldsOf(v, "the report", "schema", "change_id", "target", "target_tip", "operation", "result", |
| 192 | "layers", "failing_layer", "conflict_paths", "blocking_requests", "refusal") |
| 193 | if err != nil { |
| 194 | return nil, err |
| 195 | } |
| 196 | r := &RestackReport{} |
| 197 | for _, f := range []struct { |
| 198 | name string |
| 199 | into *string |
| 200 | }{ |
| 201 | {"schema", &r.Schema}, {"change_id", &r.ChangeID}, {"target", &r.Target}, |
| 202 | {"target_tip", &r.TargetTip}, {"operation", &r.Operation}, {"result", &r.Result}, |
| 203 | } { |
| 204 | if *f.into, err = stringOf(o[f.name], f.name); err != nil { |
| 205 | return nil, err |
| 206 | } |
| 207 | } |
| 208 | if r.Failing, err = optionalString(o["failing_layer"], "failing_layer"); err != nil { |
| 209 | return nil, err |
| 210 | } |
| 211 | if r.Refusal, err = optionalString(o["refusal"], "refusal"); err != nil { |
| 212 | return nil, err |
| 213 | } |
| 214 | if r.Conflicts, err = stringsOf(o["conflict_paths"], "conflict_paths", MaxConflictPaths); err != nil { |
| 215 | return nil, err |
| 216 | } |
| 217 | if r.Requests, err = requestsOf(o["blocking_requests"]); err != nil { |
| 218 | return nil, err |
| 219 | } |
| 220 | layers := o["layers"] |
| 221 | if layers.kind != kindArray { |
| 222 | return nil, fmt.Errorf("its layers is %s, where an array is expected", layers.kind) |
| 223 | } |
| 224 | if len(layers.items) > MaxRestackLayers { |
| 225 | return nil, fmt.Errorf("it holds %d layers, more than the %d a report may", len(layers.items), MaxRestackLayers) |
| 226 | } |
| 227 | r.Layers = []RestackLayer{} |
| 228 | for i, item := range layers.items { |
| 229 | l, err := layerOf(item, fmt.Sprintf("layers[%d]", i)) |
| 230 | if err != nil { |
| 231 | return nil, err |
| 232 | } |
| 233 | r.Layers = append(r.Layers, l) |
| 234 | } |
| 235 | if err := r.Check(); err != nil { |
| 236 | return nil, err |
| 237 | } |
| 238 | return r, nil |
| 239 | } |
| 240 | |
| 241 | func layerOf(v *value, where string) (RestackLayer, error) { |
| 242 | o, err := fieldsOf(v, where, "change_id", "record", "version", "head", "branch", "branch_tip", "published") |
| 243 | if err != nil { |
| 244 | return RestackLayer{}, err |
| 245 | } |
| 246 | var l RestackLayer |
| 247 | for _, f := range []struct { |
| 248 | name string |
| 249 | into *string |
| 250 | }{ |
| 251 | {"change_id", &l.ChangeID}, {"record", &l.Record}, {"head", &l.Head}, {"branch", &l.Branch}, |
| 252 | } { |
| 253 | if *f.into, err = stringOf(o[f.name], where+"."+f.name); err != nil { |
| 254 | return RestackLayer{}, err |
| 255 | } |
| 256 | } |
| 257 | if l.Version, err = integerOf(o["version"], where+".version", MaxVersions); err != nil { |
| 258 | return RestackLayer{}, err |
| 259 | } |
| 260 | if l.BranchTip, err = optionalString(o["branch_tip"], where+".branch_tip"); err != nil { |
| 261 | return RestackLayer{}, err |
| 262 | } |
| 263 | p := o["published"] |
| 264 | if isNull(p) { |
| 265 | return l, nil |
| 266 | } |
| 267 | where += ".published" |
| 268 | po, err := fieldsOf(p, where, "head", "version", "record") |
| 269 | if err != nil { |
| 270 | return RestackLayer{}, err |
| 271 | } |
| 272 | l.Published = &RestackPublished{} |
| 273 | if l.Published.Head, err = stringOf(po["head"], where+".head"); err != nil { |
| 274 | return RestackLayer{}, err |
| 275 | } |
| 276 | if !isNull(po["version"]) { |
| 277 | n, err := integerOf(po["version"], where+".version", MaxVersions) |
| 278 | if err != nil { |
| 279 | return RestackLayer{}, err |
| 280 | } |
| 281 | l.Published.Version = &n |
| 282 | } |
| 283 | if l.Published.Record, err = optionalString(po["record"], where+".record"); err != nil { |
| 284 | return RestackLayer{}, err |
| 285 | } |
| 286 | return l, nil |
| 287 | } |
| 288 | |
| 289 | // Check holds a report to everything the schema says beyond the types of |
| 290 | // its fields: the formats and bounds of its strings and lists, and what |
| 291 | // its fields say about one another. The change it is about is one of its |
| 292 | // layers, no layer appears twice, and a failing layer is one of them. A |
| 293 | // layer with no branch observed no branch tip and published nothing. No |
| 294 | // blocking request appears twice. A conflict names its failing layer. A |
| 295 | // conflict or a block published nothing, and a complete attempt published |
| 296 | // a version for every layer. A published version comes with the record |
| 297 | // commit that holds it. |
| 298 | func (r *RestackReport) Check() error { |
| 299 | switch { |
| 300 | case r.Schema != RestackSchema: |
| 301 | return fmt.Errorf("its schema is %q, where %q is expected", r.Schema, RestackSchema) |
| 302 | case !ValidID(r.ChangeID): |
| 303 | return fmt.Errorf("its change_id %q is not a change ID: 32 letters from k to z", r.ChangeID) |
| 304 | case !ValidBranch(r.Target) || !utf8.ValidString(r.Target): |
| 305 | return fmt.Errorf("its target %q is not a branch name in UTF-8", r.Target) |
| 306 | case !ValidHead(r.TargetTip): |
| 307 | return fmt.Errorf("its target_tip %q is not %s and 40 lowercase hex digits", r.TargetTip, HeadPrefix) |
| 308 | case !operationName.MatchString(r.Operation) || len(r.Operation) > MaxOperation: |
| 309 | return fmt.Errorf("its operation %q is not 1 to %d letters, digits, dots, colons, dashes and underscores", r.Operation, MaxOperation) |
| 310 | case !validResult(r.Result): |
| 311 | return fmt.Errorf("its result is %q, where %q, %q, %q, %q or %q is expected", r.Result, |
| 312 | RestackConflict, RestackBlocked, RestackPartial, RestackComplete, RestackSuperseded) |
| 313 | case len(r.Layers) == 0: |
| 314 | return errors.New("it holds no layer, and a report holds at least the change it is about") |
| 315 | case len(r.Layers) > MaxRestackLayers: |
| 316 | return fmt.Errorf("it holds %d layers, more than the %d a report may", len(r.Layers), MaxRestackLayers) |
| 317 | case len(r.Conflicts) > MaxConflictPaths: |
| 318 | return fmt.Errorf("it holds %d conflict paths, more than the %d a report may", len(r.Conflicts), MaxConflictPaths) |
| 319 | case len(r.Requests) > MaxBlockingRequest: |
| 320 | return fmt.Errorf("it holds %d blocking requests, more than the %d a report may", len(r.Requests), MaxBlockingRequest) |
| 321 | case r.Refusal != nil && !validText(*r.Refusal, MaxRefusal): |
| 322 | return fmt.Errorf("its refusal is not 1 to %d bytes free of control characters", MaxRefusal) |
| 323 | } |
| 324 | seen := map[string]bool{} |
| 325 | published, versioned := false, true |
| 326 | for i, l := range r.Layers { |
| 327 | where := fmt.Sprintf("layers[%d]", i) |
| 328 | switch { |
| 329 | case !ValidID(l.ChangeID): |
| 330 | return fmt.Errorf("its %s.change_id %q is not a change ID: 32 letters from k to z", where, l.ChangeID) |
| 331 | case seen[l.ChangeID]: |
| 332 | return fmt.Errorf("its %s names the change %s, which an earlier layer names", where, l.ChangeID) |
| 333 | case !ValidHead(l.Record): |
| 334 | return fmt.Errorf("its %s.record %q is not %s and 40 lowercase hex digits", where, l.Record, HeadPrefix) |
| 335 | case l.Version < 1 || l.Version > MaxVersions: |
| 336 | return fmt.Errorf("its %s.version is %d, outside 1 to %d", where, l.Version, MaxVersions) |
| 337 | case !ValidHead(l.Head): |
| 338 | return fmt.Errorf("its %s.head %q is not %s and 40 lowercase hex digits", where, l.Head, HeadPrefix) |
| 339 | case l.Branch != "" && (!ValidBranch(l.Branch) || !utf8.ValidString(l.Branch)): |
| 340 | return fmt.Errorf("its %s.branch %q is neither empty nor a branch name in UTF-8", where, l.Branch) |
| 341 | case l.BranchTip != nil && !ValidHead(*l.BranchTip): |
| 342 | return fmt.Errorf("its %s.branch_tip %q is not %s and 40 lowercase hex digits", where, *l.BranchTip, HeadPrefix) |
| 343 | case l.Branch == "" && l.BranchTip != nil: |
| 344 | return fmt.Errorf("its %s names no branch, so it observed no branch_tip, and its branch_tip is %s", where, *l.BranchTip) |
| 345 | case l.Branch == "" && l.Published != nil: |
| 346 | return fmt.Errorf("its %s names no branch, and a layer with no branch publishes nothing", where) |
| 347 | } |
| 348 | seen[l.ChangeID] = true |
| 349 | p := l.Published |
| 350 | if p == nil { |
| 351 | versioned = false |
| 352 | continue |
| 353 | } |
| 354 | published = true |
| 355 | switch { |
| 356 | case !ValidHead(p.Head): |
| 357 | return fmt.Errorf("its %s.published.head %q is not %s and 40 lowercase hex digits", where, p.Head, HeadPrefix) |
| 358 | case (p.Version == nil) != (p.Record == nil): |
| 359 | return fmt.Errorf("its %s.published names a version without its record, or a record without its version, and the two come together", where) |
| 360 | case p.Version != nil && (*p.Version < 1 || *p.Version > MaxVersions): |
| 361 | return fmt.Errorf("its %s.published.version is %d, outside 1 to %d", where, *p.Version, MaxVersions) |
| 362 | case p.Record != nil && !ValidHead(*p.Record): |
| 363 | return fmt.Errorf("its %s.published.record %q is not %s and 40 lowercase hex digits", where, *p.Record, HeadPrefix) |
| 364 | } |
| 365 | versioned = versioned && p.Version != nil |
| 366 | } |
| 367 | for i, path := range r.Conflicts { |
| 368 | if !validText(path, MaxConflictPath) { |
| 369 | return fmt.Errorf("its conflict_paths[%d] is not 1 to %d bytes free of control characters", i, MaxConflictPath) |
| 370 | } |
| 371 | } |
| 372 | requests := map[string]bool{} |
| 373 | for i, q := range r.Requests { |
| 374 | name, ok := strings.CutPrefix(q.Ref, "refs/the-valley/integration-requests/"+r.Target+"/") |
| 375 | switch { |
| 376 | case !ok || !ValidBranch(name) || len(q.Ref) > MaxRequestRef: |
| 377 | return fmt.Errorf("its blocking_requests[%d].ref %q is not an integration request's ref on %s of at most %d bytes", i, q.Ref, r.Target, MaxRequestRef) |
| 378 | case requests[q.Ref]: |
| 379 | return fmt.Errorf("its blocking_requests[%d] names the request %s, which an earlier entry names", i, q.Ref) |
| 380 | case !ValidHead(q.Tip): |
| 381 | return fmt.Errorf("its blocking_requests[%d].tip %q is not %s and 40 lowercase hex digits", i, q.Tip, HeadPrefix) |
| 382 | } |
| 383 | requests[q.Ref] = true |
| 384 | } |
| 385 | switch { |
| 386 | case !seen[r.ChangeID]: |
| 387 | return fmt.Errorf("no layer names the change %s, and a report is about one of its layers", r.ChangeID) |
| 388 | case r.Failing != nil && !seen[*r.Failing]: |
| 389 | return fmt.Errorf("its failing_layer %q is none of its layers", *r.Failing) |
| 390 | case r.Result == RestackConflict && r.Failing == nil: |
| 391 | return errors.New("its result is conflict, and a conflict names its failing_layer") |
| 392 | case (r.Result == RestackConflict || r.Result == RestackBlocked) && published: |
| 393 | return fmt.Errorf("its result is %s, and a %s publishes nothing", r.Result, r.Result) |
| 394 | case r.Result == RestackComplete && !versioned: |
| 395 | return errors.New("its result is complete, and a complete attempt published a version for every layer") |
| 396 | } |
| 397 | return nil |
| 398 | } |
| 399 | |
| 400 | // EncodeRestackReport writes a report as report.json. It refuses a report |
| 401 | // ParseRestackReport would refuse, so what it writes always reads back as |
| 402 | // the same report. |
| 403 | func EncodeRestackReport(r *RestackReport) ([]byte, error) { |
| 404 | if err := r.Check(); err != nil { |
| 405 | return nil, fmt.Errorf("the report cannot be written: %w", err) |
| 406 | } |
| 407 | out := *r |
| 408 | if out.Conflicts == nil { |
| 409 | out.Conflicts = []string{} |
| 410 | } |
| 411 | if out.Requests == nil { |
| 412 | out.Requests = []RestackRequest{} |
| 413 | } |
| 414 | var buf bytes.Buffer |
| 415 | enc := json.NewEncoder(&buf) |
| 416 | enc.SetEscapeHTML(false) |
| 417 | enc.SetIndent("", " ") |
| 418 | if err := enc.Encode(&out); err != nil { |
| 419 | return nil, err |
| 420 | } |
| 421 | return buf.Bytes(), nil |
| 422 | } |
| 423 | |
| 424 | // operationName is the shape of an operation identifier: a local name the |
| 425 | // restacker gives one attempt. |
| 426 | var operationName = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._:-]*$`) |
| 427 | |
| 428 | func validResult(s string) bool { |
| 429 | switch s { |
| 430 | case RestackConflict, RestackBlocked, RestackPartial, RestackComplete, RestackSuperseded: |
| 431 | return true |
| 432 | } |
| 433 | return false |
| 434 | } |
| 435 | |
| 436 | // validText reports whether s is 1 to max bytes of UTF-8 holding no |
| 437 | // control character. |
| 438 | func validText(s string, max int) bool { |
| 439 | if len(s) < 1 || len(s) > max || !utf8.ValidString(s) { |
| 440 | return false |
| 441 | } |
| 442 | return !strings.ContainsFunc(s, unicode.IsControl) |
| 443 | } |
| 444 | |
| 445 | func isNull(v *value) bool { |
| 446 | return v.kind == kindLiteral && v.text == "null" |
| 447 | } |
| 448 | |
| 449 | // optionalString reads a string or null. |
| 450 | func optionalString(v *value, name string) (*string, error) { |
| 451 | if isNull(v) { |
| 452 | return nil, nil |
| 453 | } |
| 454 | s, err := stringOf(v, name) |
| 455 | if err != nil { |
| 456 | return nil, fmt.Errorf("its %s is %s, where a string or null is expected", name, v.kind) |
| 457 | } |
| 458 | return &s, nil |
| 459 | } |
| 460 | |
| 461 | // requestsOf reads blocking_requests: an array of at most |
| 462 | // MaxBlockingRequest objects, each a request's ref and tip. |
| 463 | func requestsOf(v *value) ([]RestackRequest, error) { |
| 464 | if v.kind != kindArray { |
| 465 | return nil, fmt.Errorf("its blocking_requests is %s, where an array is expected", v.kind) |
| 466 | } |
| 467 | if len(v.items) > MaxBlockingRequest { |
| 468 | return nil, fmt.Errorf("its blocking_requests holds %d entries, more than the %d it may", len(v.items), MaxBlockingRequest) |
| 469 | } |
| 470 | list := []RestackRequest{} |
| 471 | for i, item := range v.items { |
| 472 | where := fmt.Sprintf("blocking_requests[%d]", i) |
| 473 | o, err := fieldsOf(item, where, "ref", "tip") |
| 474 | if err != nil { |
| 475 | return nil, err |
| 476 | } |
| 477 | var q RestackRequest |
| 478 | if q.Ref, err = stringOf(o["ref"], where+".ref"); err != nil { |
| 479 | return nil, err |
| 480 | } |
| 481 | if q.Tip, err = stringOf(o["tip"], where+".tip"); err != nil { |
| 482 | return nil, err |
| 483 | } |
| 484 | list = append(list, q) |
| 485 | } |
| 486 | return list, nil |
| 487 | } |
| 488 | |
| 489 | // stringsOf reads an array of at most max strings. |
| 490 | func stringsOf(v *value, name string, max int) ([]string, error) { |
| 491 | if v.kind != kindArray { |
| 492 | return nil, fmt.Errorf("its %s is %s, where an array is expected", name, v.kind) |
| 493 | } |
| 494 | if len(v.items) > max { |
| 495 | return nil, fmt.Errorf("its %s holds %d entries, more than the %d it may", name, len(v.items), max) |
| 496 | } |
| 497 | list := []string{} |
| 498 | for i, item := range v.items { |
| 499 | s, err := stringOf(item, fmt.Sprintf("%s[%d]", name, i)) |
| 500 | if err != nil { |
| 501 | return nil, err |
| 502 | } |
| 503 | list = append(list, s) |
| 504 | } |
| 505 | return list, nil |
| 506 | } |