Skip to content

Commit

Permalink
sort responses by status code
Browse files Browse the repository at this point in the history
  • Loading branch information
s-mang committed Mar 25, 2016
1 parent 64d4102 commit ab9f18a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
32 changes: 26 additions & 6 deletions doc/action.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package doc

import (
"net/http"
"strings"
"text/template"

Expand All @@ -10,9 +11,9 @@ import (
var (
actionTmpl *template.Template
actionFmt = `### {{.Title}} [{{.Method}}]
{{.Description}}{{range $req, $resp := .Requests}}
{{with $req}}{{.Render}}{{end}}
{{with $resp}}{{.Render}}{{end}}{{end}}`
{{.Description}}{{range $req := .Requests}}
{{with $req}}{{.Render}}
{{.Response.Render}}{{end}}{{end}}`
)

func init() {
Expand All @@ -23,10 +24,28 @@ type Action struct {
Title string
Description string
Method HTTPMethod
Requests map[*Request]*Response
Requests []*Request
}

func (a *Action) Render() string {
reqsMap := map[int][]*Request{}
for i, req := range a.Requests {
if reqsMap[req.Response.StatusCode] == nil {
reqsMap[req.Response.StatusCode] = []*Request{}
}

reqsMap[req.Response.StatusCode] = append(reqsMap[req.Response.StatusCode], a.Requests[i])
}

sortedReqs := reqsMap[http.StatusOK]
delete(reqsMap, http.StatusOK)

for _, reqs := range reqsMap {
sortedReqs = append(sortedReqs, reqs...)
}

a.Requests = sortedReqs

return render(actionTmpl, a)
}

Expand All @@ -42,11 +61,12 @@ func NewAction(method, handlerName string) (*Action, error) {
Title: title,
Description: desc,
Method: HTTPMethod(method),
Requests: map[*Request]*Response{},
Requests: []*Request{},
}, nil

}

func (a *Action) AddRequest(req *Request, resp *Response) {
a.Requests[req] = resp
req.Response = resp
a.Requests = append(a.Requests, req)
}
7 changes: 4 additions & 3 deletions doc/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ func init() {
}

type Request struct {
Header *Header
Body *Body
Method string
Header *Header
Body *Body
Method string
Response *Response

// TODO:
// Attributes
Expand Down
13 changes: 13 additions & 0 deletions example/widgets/widgets.apib
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ retrieves a single Widget



+ Response 400 (text/plain; charset=utf-8)

+ Headers

X-Content-Type-Options: nosniff

+ Body

strconv.ParseInt: parsing "hello": invalid syntax




## /widgets

### Get Widgets [GET]
Expand Down
12 changes: 12 additions & 0 deletions example/widgets/widgets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ func (t *mainSuite) TestGetWidgets() {
t.Equal(ws[1].Role, widgets.AllWidgets[1].Role)
}

func (t *mainSuite) TestGetWidgetBadRequest() {
idStr := "hello"

urlPath, err := router.Get("GetWidget").URL("id", idStr)
t.Must(t.Nil(err))

resp, err := http.Get(server.URL + urlPath.String())
t.Must(t.Nil(err))

t.Must(t.Equal(resp.StatusCode, http.StatusBadRequest))
}

func (t *mainSuite) TestGetWidget() {
var id int64 = 2
idStr := fmt.Sprintf("%d", id)
Expand Down

0 comments on commit ab9f18a

Please sign in to comment.