scaffold builds a success response for a REST request from GitHub's description. It starts from GitHub's example. About one in five examples lag behind the description, so it fills their gaps with placeholders. It returns the response and says where the body came from.
(method, path string)
| 20 | // lag behind the description, so it fills their gaps with placeholders. It |
| 21 | // returns the response and says where the body came from. |
| 22 | func (idx *restIndex) scaffold(method, path string) (response, string, error) { |
| 23 | route := idx.match(method, path) |
| 24 | if route == nil { |
| 25 | return response{}, "", fmt.Errorf("GitHub's REST description has no path matching %s", path) |
| 26 | } |
| 27 | op := route.item.GetOperation(method) |
| 28 | if op == nil { |
| 29 | return response{}, "", fmt.Errorf("GitHub documents no %s for %s", method, route.template) |
| 30 | } |
| 31 | code, ref := successStatus(op.Responses) |
| 32 | if ref == nil || ref.Value == nil { |
| 33 | return response{}, "", fmt.Errorf("GitHub documents no success status for %s %s", method, route.template) |
| 34 | } |
| 35 | status := 200 |
| 36 | if n, err := strconv.Atoi(code); err == nil { |
| 37 | status = n |
| 38 | } |
| 39 | var resp response |
| 40 | statusText := "" |
| 41 | if status != 200 { |
| 42 | resp.Status = status |
| 43 | statusText = fmt.Sprintf(", status %d", status) |
| 44 | } |
| 45 | |
| 46 | media := jsonMedia(ref.Value.Content) |
| 47 | if media == nil || media.Schema == nil || media.Schema.Value == nil { |
| 48 | if len(ref.Value.Content) > 0 { |
| 49 | return response{}, "", fmt.Errorf("GitHub's response for status %s is %s; provide it with ghmock set --raw-file or --zip-dir", |
| 50 | code, strings.Join(slices.Sorted(maps.Keys(ref.Value.Content)), ", ")) |
| 51 | } |
| 52 | return resp, fmt.Sprintf(" with no body%s", statusText), nil |
| 53 | } |
| 54 | |
| 55 | if example, ok := pickExample(media); ok { |
| 56 | f := &restFiller{} |
| 57 | filled := f.fill(media.Schema, example, "", 0) |
| 58 | f.filled += encodeContent(filled) |
| 59 | body, err := json.Marshal(filled) |
| 60 | if err == nil { |
| 61 | if problems, _ := idx.check(method, path, &response{Status: status, Body: body}); len(problems) == 0 { |
| 62 | resp.Body = body |
| 63 | detail := " from GitHub's example" |
| 64 | if f.filled > 0 { |
| 65 | detail += fmt.Sprintf(", with %s added or fixed", plural(f.filled, "value")) |
| 66 | } |
| 67 | return resp, detail + statusText, nil |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | f := &restFiller{} |
| 73 | body, err := json.Marshal(f.generate(media.Schema, "", 0)) |
| 74 | if err != nil { |
| 75 | return response{}, "", err |
| 76 | } |
| 77 | if problems, _ := idx.check(method, path, &response{Status: status, Body: body}); len(problems) > 0 { |
| 78 | return response{}, "", fmt.Errorf("couldn't build a body that matches GitHub's description:\n%sSet one with ghmock set --off-schema", |
| 79 | indentProblems(problems, " ", maxShownProblems)) |
no test coverage detected