Skip to content

Commit

Permalink
feat(gctx): add gctx for Google JSON style response (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
cage1016 authored and cutedogspark committed Apr 11, 2018
1 parent ab83acc commit b9ca0e0
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 50 deletions.
47 changes: 0 additions & 47 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package ctx

import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/labstack/echo"
Expand Down Expand Up @@ -133,48 +131,3 @@ func (r *errorCall) Do() (err error) {
}
return r.c.JSONBlob(r.httpStatus, b)
}

// Google JSON Style error call
type gerrorCall struct {
c echo.Context
httpStatus int
responseParams GErrorResponse
}

type gerrorMessage struct {
Code int `json:"code"`
Message string `json:"message"`
Errors []GError `json:"errors,omitempty"`
}

type GErrorResponse struct {
ApiVersion string `json:"apiVersion"`
Error gerrorMessage `json:"error"`
}

func (c CustomCtx) GError(errs ...GError) *gerrorCall {
rs := &gerrorCall{
c: echo.Context(c),
responseParams: GErrorResponse{
ApiVersion: apiVersion,
Error: gerrorMessage{},
},
}

if len(errs) > 0 {
s, _ := strconv.Atoi(fmt.Sprintf("%d", errs[0].Code)[:3])
rs.httpStatus = s
rs.responseParams.Error.Code = errs[0].Code
rs.responseParams.Error.Message = errs[0].Message
rs.responseParams.Error.Errors = errs
}
return rs
}

func (r *gerrorCall) Do() (err error) {
b, err := json.Marshal(r.responseParams)
if err != nil {
return err
}
return r.c.JSONBlob(r.httpStatus, b)
}
11 changes: 9 additions & 2 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ func TestCustomCtx(t *testing.T) {
},
wantJSON: `{"apiVersion": "1.0", "data": "hello world"}`,
},
{
name: "200 with google json style",
givenHandler: func(c echo.Context) error {
return c.(ctx.CustomCtx).GResp(http.StatusOK).Data("hello world").Do()
},
wantJSON: `{"apiVersion": "1.0", "data": "hello world"}`,
},
{
name: "400 with google json style",
givenHandler: func(c echo.Context) error {

gerr := ctx.NewGErrors().Append(ctx.GError{
gerrs := ctx.NewGErrors().Append(ctx.GError{
Code: 40000001,
Domain: "Calendar",
Reason: "ResourceNotFoundException",
Expand All @@ -47,7 +54,7 @@ func TestCustomCtx(t *testing.T) {
Location: "part",
})

return c.(ctx.CustomCtx).GError(gerr...).Do()
return c.(ctx.CustomCtx).GResp().Errors(gerrs...).Do()
},
wantJSON: `{"apiVersion":"1.0","error":{"code":40000001,"message":"Resources is not exist","errors":[{"extendedHelp":"http://help-link", "sendReport":"http://report.dajui.com/", "domain":"Calendar", "reason":"ResourceNotFoundException", "message":"Resources is not exist", "location":"query", "locationType":"database query"},{"message":"Required parameter: part", "location":"part", "locationType":"parameter", "domain":"global", "reason":"required"}]}}`,
},
Expand Down
121 changes: 121 additions & 0 deletions gctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package ctx

import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/labstack/echo"
)

type grespCall struct {
ver string
c echo.Context
httpStatus []int
}

func (c CustomCtx) GResp(httpStatus ...int) *grespCall {
rs := &grespCall{
ver: apiVersion,
c: echo.Context(c),
httpStatus: httpStatus,
}
return rs
}

type gdataCall struct {
c echo.Context
httpStatus int
responseParams SuccessResp
}

func (r *grespCall) Ver(ver string) *grespCall {
r.ver = ver
return r
}

func (r *grespCall) Data(data ...interface{}) *gdataCall {
var d interface{}
if len(data) == 0 {
d = []string{}
} else {
d = data[0]
}

rs := &gdataCall{
c: r.c,
httpStatus: r.httpStatus[0],
responseParams: SuccessResp{
ApiVersion: r.ver,
Data: d,
},
}
return rs
}

// Response Json Format
// - replace string when response raw data
// - ex: replace := strings.NewReplacer("{PP_KEY}", encryptionKey)
func (r *gdataCall) Do(replace ...*strings.Replacer) (err error) {
b, err := json.Marshal(r.responseParams)
if err != nil {
return err
}
data := string(b)
for _, value := range replace {
data = value.Replace(data)
}

return r.c.JSONBlob(r.httpStatus, []byte(data))
}

// Google JSON Style error call
type gerrorCall struct {
c echo.Context
httpStatus int
responseParams GErrorResponse
}

type gerrorMessage struct {
Code uint `json:"code"`
Message string `json:"message"`
Errors []GError `json:"errors,omitempty"`
}

type GErrorResponse struct {
ApiVersion string `json:"apiVersion"`
Error gerrorMessage `json:"error"`
}

func (r *grespCall) Errors(errs ...GError) *gerrorCall {
rs := &gerrorCall{
c: r.c,
responseParams: GErrorResponse{
ApiVersion: apiVersion,
Error: gerrorMessage{},
},
}

if len(errs) > 0 {
if len(r.httpStatus) > 0 {
rs.httpStatus = r.httpStatus[0]
} else {
s, _ := strconv.Atoi(fmt.Sprintf("%d", errs[0].Code)[:3])
rs.httpStatus = s
}

rs.responseParams.Error.Code = errs[0].Code
rs.responseParams.Error.Message = errs[0].Message
rs.responseParams.Error.Errors = errs
}
return rs
}

func (r *gerrorCall) Do() (err error) {
b, err := json.Marshal(r.responseParams)
if err != nil {
return err
}
return r.c.JSONBlob(r.httpStatus, b)
}
6 changes: 5 additions & 1 deletion gerr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ctx
type GErrors []GError

type GError struct {
Code int `json:"-"`
Code uint `json:"-"`
Domain string `json:"domain,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
Expand All @@ -18,4 +18,8 @@ func (c GErrors) Append(gerr GError) GErrors {
return c
}

func (c GErrors) Empty() bool {
return len(c) == 0
}

func NewGErrors() GErrors { return GErrors{} }

0 comments on commit b9ca0e0

Please sign in to comment.