Skip to content

Commit

Permalink
Merge pull request #34 from uber/improved-status-code
Browse files Browse the repository at this point in the history
Cleanup usage of status code thrift annotation
  • Loading branch information
Raynos committed Apr 3, 2017
2 parents 3db4e6d + 58f9a38 commit 1bbe2ba
Show file tree
Hide file tree
Showing 33 changed files with 101 additions and 111 deletions.
21 changes: 12 additions & 9 deletions codegen/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type MethodSpec struct {
Headers []string
RequestType string
ResponseType string
OKStatusCode []StatusCode
OKStatusCode StatusCode
ExceptionStatusCode []StatusCode
// Additional struct generated from the bundle of request args.
RequestBoxed bool
Expand Down Expand Up @@ -155,15 +155,18 @@ func (ms *MethodSpec) setOKStatusCode(statusCode string) error {
if statusCode == "" {
return errors.Errorf("no http OK status code set by annotation '%s' ", antHTTPStatus)
}
scode := strings.Split(statusCode, ",")
ms.OKStatusCode = make([]StatusCode, len(scode))
var err error
for i, c := range scode {
ms.OKStatusCode[i].Code, err = strconv.Atoi(c)
if err != nil {
return errors.Wrapf(err, "failed to parse the annotation %s for ok response status")
}

code, err := strconv.Atoi(statusCode)
if err != nil {
return errors.Wrapf(err,
"Could not parse status code annotation (%s) for ok response",
statusCode,
)
}
ms.OKStatusCode = StatusCode{
Code: code,
}

return nil
}

Expand Down
18 changes: 0 additions & 18 deletions codegen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
tmpl "text/template"

Expand Down Expand Up @@ -100,7 +99,6 @@ var funcMap = tmpl.FuncMap{
"title": strings.Title,
"Title": strings.Title,
"fullTypeName": fullTypeName,
"statusCodes": statusCodes,
"camel": camelCase,
"split": strings.Split,
"dec": decrement,
Expand All @@ -116,22 +114,6 @@ func fullTypeName(typeName, packageName string) string {
return packageName + "." + typeName
}

func statusCodes(codes []StatusCode) string {
if len(codes) == 0 {
return "[]int{}"
}
buf := bytes.NewBufferString("[]int{")
for i := 0; i < len(codes)-1; i++ {
if _, err := buf.WriteString(strconv.Itoa(codes[i].Code) + ","); err != nil {
return err.Error()
}
}
if _, err := buf.WriteString(strconv.Itoa(codes[len(codes)-1].Code) + "}"); err != nil {
return err.Error()
}
return string(buf.Bytes())
}

func camelCase(src string) string {
byteSrc := []byte(src)
chunks := camelingRegex.FindAll(byteSrc, -1)
Expand Down
7 changes: 4 additions & 3 deletions codegen/templates/endpoint.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ func Handle{{title .Name}}Request(
}()

// Handle client respnse.
if !res.IsOKResponse(clientResp.StatusCode, {{statusCodes .OKStatusCode}}) {
expectedStatusCode := []int{ {{.DownstreamMethod.OKStatusCode.Code}} }
if !res.IsOKResponse(clientResp.StatusCode, expectedStatusCode) {
req.Logger.Warn("Unknown response status code",
zap.Int("status code", clientResp.StatusCode),
)
}
{{if or (eq $clientMethodResponseType "") (eq .ResponseType "") -}}
res.WriteJSONBytes(clientResp.StatusCode, nil)
res.WriteJSONBytes({{.OKStatusCode.Code}}, nil)
{{- else -}}
b, err := ioutil.ReadAll(clientResp.Body)
if err != nil {
Expand All @@ -90,7 +91,7 @@ func Handle{{title .Name}}Request(
return
}
response := convert{{title .Name}}ClientResponse(&clientRespBody)
res.WriteJSON(clientResp.StatusCode, response)
res.WriteJSON({{.OKStatusCode.Code}}, response)
{{- end -}}
}

Expand Down
4 changes: 2 additions & 2 deletions codegen/templates/endpoint_test.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Test{{.HandlerID | Title}}{{.TestName | Title}}OKResponse(t *testing.T) {

{{range .ClientStubs}}
fake{{.ClientMethod | Title}} := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader({{(index $.Method.OKStatusCode 0).Code}})
w.WriteHeader({{$.Method.OKStatusCode.Code}})
// TODO(zw): generate client response.
if _, err := w.Write([]byte( `{{.ClientResponseString}}`)); err != nil {
t.Fatal("can't write fake response")
Expand Down Expand Up @@ -75,7 +75,7 @@ func Test{{.HandlerID | Title}}{{.TestName | Title}}OKResponse(t *testing.T) {
return
}

assert.Equal(t, "{{(index $.Method.OKStatusCode 0).Code}} OK", res.Status)
assert.Equal(t, {{$.Method.OKStatusCode.Code}}, res.StatusCode)
assert.Equal(t, 1, counter)
}

Expand Down
50 changes: 20 additions & 30 deletions codegen/test_data/bar.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@
"Headers": null,
"RequestType": "ArgNotStructHTTPRequest",
"ResponseType": "",
"OKStatusCode": [
{
"Code": 200,
"Message": ""
}
],
"OKStatusCode": {
"Code": 200,
"Message": ""
},
"ExceptionStatusCode": [
{
"Code": 403,
Expand Down Expand Up @@ -79,12 +77,10 @@
"Headers": null,
"RequestType": "",
"ResponseType": "bar.BarResponse",
"OKStatusCode": [
{
"Code": 200,
"Message": ""
}
],
"OKStatusCode": {
"Code": 200,
"Message": ""
},
"ExceptionStatusCode": [
{
"Code": 403,
Expand Down Expand Up @@ -120,12 +116,10 @@
"Headers": null,
"RequestType": "",
"ResponseType": "bar.BarResponse",
"OKStatusCode": [
{
"Code": 200,
"Message": ""
}
],
"OKStatusCode": {
"Code": 200,
"Message": ""
},
"ExceptionStatusCode": [
{
"Code": 403,
Expand Down Expand Up @@ -161,12 +155,10 @@
"Headers": null,
"RequestType": "NormalHTTPRequest",
"ResponseType": "bar.BarResponse",
"OKStatusCode": [
{
"Code": 200,
"Message": ""
}
],
"OKStatusCode": {
"Code": 200,
"Message": ""
},
"ExceptionStatusCode": [
{
"Code": 403,
Expand Down Expand Up @@ -211,12 +203,10 @@
],
"RequestType": "TooManyArgsHTTPRequest",
"ResponseType": "bar.BarResponse",
"OKStatusCode": [
{
"Code": 200,
"Message": ""
}
],
"OKStatusCode": {
"Code": 200,
"Message": ""
},
"ExceptionStatusCode": [
{
"Code": 403,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ func TestArgNotStructSuccessfulRequestOKResponse(t *testing.T) {
return
}

assert.Equal(t, "200 OK", res.Status)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, 1, counter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ func TestMissingArgSuccessfulRequestOKResponse(t *testing.T) {
return
}

assert.Equal(t, "200 OK", res.Status)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, 1, counter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ func TestNoRequestSuccessfulRequestOKResponse(t *testing.T) {
return
}

assert.Equal(t, "200 OK", res.Status)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, 1, counter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ func TestNormalSuccessfulRequestOKResponse(t *testing.T) {
return
}

assert.Equal(t, "200 OK", res.Status)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, 1, counter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func TestTooManyArgsSuccessfulRequestOKResponse(t *testing.T) {
return
}

assert.Equal(t, "200 OK", res.Status)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, 1, counter)
}
5 changes: 3 additions & 2 deletions codegen/test_data/endpoints/bar_bar_method_argnotstruct.gogen
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ func HandleArgNotStructRequest(
}()

// Handle client respnse.
if !res.IsOKResponse(clientResp.StatusCode, []int{200}) {
expectedStatusCode := []int{200}
if !res.IsOKResponse(clientResp.StatusCode, expectedStatusCode) {
req.Logger.Warn("Unknown response status code",
zap.Int("status code", clientResp.StatusCode),
)
}
res.WriteJSONBytes(clientResp.StatusCode, nil)
res.WriteJSONBytes(200, nil)
}

func convertToArgNotStructClientRequest(body *ArgNotStructHTTPRequest) *barClient.ArgNotStructHTTPRequest {
Expand Down
5 changes: 3 additions & 2 deletions codegen/test_data/endpoints/bar_bar_method_missingarg.gogen
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func HandleMissingArgRequest(
}()

// Handle client respnse.
if !res.IsOKResponse(clientResp.StatusCode, []int{200}) {
expectedStatusCode := []int{200}
if !res.IsOKResponse(clientResp.StatusCode, expectedStatusCode) {
req.Logger.Warn("Unknown response status code",
zap.Int("status code", clientResp.StatusCode),
)
Expand All @@ -56,7 +57,7 @@ func HandleMissingArgRequest(
return
}
response := convertMissingArgClientResponse(&clientRespBody)
res.WriteJSON(clientResp.StatusCode, response)
res.WriteJSON(200, response)
}

func convertMissingArgClientResponse(body *bar.BarResponse) *bar.BarResponse {
Expand Down
5 changes: 3 additions & 2 deletions codegen/test_data/endpoints/bar_bar_method_norequest.gogen
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func HandleNoRequestRequest(
}()

// Handle client respnse.
if !res.IsOKResponse(clientResp.StatusCode, []int{200}) {
expectedStatusCode := []int{200}
if !res.IsOKResponse(clientResp.StatusCode, expectedStatusCode) {
req.Logger.Warn("Unknown response status code",
zap.Int("status code", clientResp.StatusCode),
)
Expand All @@ -56,7 +57,7 @@ func HandleNoRequestRequest(
return
}
response := convertNoRequestClientResponse(&clientRespBody)
res.WriteJSON(clientResp.StatusCode, response)
res.WriteJSON(200, response)
}

func convertNoRequestClientResponse(body *bar.BarResponse) *bar.BarResponse {
Expand Down
5 changes: 3 additions & 2 deletions codegen/test_data/endpoints/bar_bar_method_normal.gogen
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func HandleNormalRequest(
}()

// Handle client respnse.
if !res.IsOKResponse(clientResp.StatusCode, []int{200}) {
expectedStatusCode := []int{200}
if !res.IsOKResponse(clientResp.StatusCode, expectedStatusCode) {
req.Logger.Warn("Unknown response status code",
zap.Int("status code", clientResp.StatusCode),
)
Expand All @@ -64,7 +65,7 @@ func HandleNormalRequest(
return
}
response := convertNormalClientResponse(&clientRespBody)
res.WriteJSON(clientResp.StatusCode, response)
res.WriteJSON(200, response)
}

func convertToNormalClientRequest(body *NormalHTTPRequest) *barClient.NormalHTTPRequest {
Expand Down
5 changes: 3 additions & 2 deletions codegen/test_data/endpoints/bar_bar_method_toomanyargs.gogen
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func HandleTooManyArgsRequest(
}()

// Handle client respnse.
if !res.IsOKResponse(clientResp.StatusCode, []int{200}) {
expectedStatusCode := []int{200}
if !res.IsOKResponse(clientResp.StatusCode, expectedStatusCode) {
req.Logger.Warn("Unknown response status code",
zap.Int("status code", clientResp.StatusCode),
)
Expand All @@ -68,7 +69,7 @@ func HandleTooManyArgsRequest(
return
}
response := convertTooManyArgsClientResponse(&clientRespBody)
res.WriteJSON(clientResp.StatusCode, response)
res.WriteJSON(200, response)
}

func convertToTooManyArgsClientRequest(body *TooManyArgsHTTPRequest) *barClient.TooManyArgsHTTPRequest {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1bbe2ba

Please sign in to comment.