Skip to content

Commit

Permalink
rm cb params for default dump()
Browse files Browse the repository at this point in the history
  • Loading branch information
tpkeeper committed Nov 28, 2019
1 parent 866efe7 commit d104c67
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
85 changes: 51 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,75 @@
* [ ] text/plain

## Usage
### Start using it

Download and install it:
All:

```sh
$ go get github.com/tpkeeper/gin-dump
```go
func main() {
router := gin.Default()

//use Dump() default will print on stdout
router.Use(gindump.Dump())

//or use DumpWithOptions() with more options
router.Use(gindump.DumpWithOptions(true, true, false, true, false, func(dumpStr string) {
fmt.Println(dumpStr)
}))

router.Post("/",myHandler)

...

router.Run()
}
```

Import it in your code:
Group:

```go
import "github.com/tpkeeper/gin-dump"
func main() {
router := gin.Default()

dumpGroup := router.Group("/group")

//use Dump() default will print on stdout
dumpGroup.Use(gindump.Dump())

//or use DumpWithOptions() with more options
dumpGroup.Use(gindump.DumpWithOptions(true, true, false, true, false, func(dumpStr string) {
fmt.Println(dumpStr)
}))

dumpGroup.Post("/",myHandler)

...

router.Run()
}

```

### Canonical example:
EndPoint:

```go
package main

import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/tpkeeper/gin-dump"
)

func main() {
router := gin.Default()

showReq := true
showResp := true
showBody := true
showHeaders := false
showCookies := false
//use Dump() default will print on stdout
router.Post("/",gindump.Dump(),myHandler)

router.Use(gindump.Dump(nil)) // prints on stdout
// or
router.Use(gindump.Dump(func(dumpStr string) {
fmt.Println(dumpStr)
}))
// or
router.Use(gindump.DumpWithOptions(showReq, showResp, showBody, showHeaders, showCookies, nil) // prints on stdout
// or
router.Use(gindump.DumpWithOptions(showReq, showResp, showBody, showHeaders, showCookies, func(dumpStr string) {
//or use DumpWithOptions() with more options
router.Post("/",gindump.DumpWithOptions(true, true, false, true, false, func(dumpStr string) {
fmt.Println(dumpStr)
}))

//...
router.Run()
}),myHandler)

...

router.Run()
}
```


### Output is as follows

```sh
Expand Down
16 changes: 8 additions & 8 deletions gindump.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"github.com/gin-gonic/gin"
)

func Dump(cb func(dumpStr string)) gin.HandlerFunc {
return DumpWithOptions(true, true, true, true, true, cb)
func Dump() gin.HandlerFunc {
return DumpWithOptions(true, true, true, true, true, nil)
}

func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders bool, showCookies bool, cb func(dumpStr string)) gin.HandlerFunc {
headerHiddenFields := make([]string,0)
bodyHiddenFields := make([]string,0)
headerHiddenFields := make([]string, 0)
bodyHiddenFields := make([]string, 0)

if !showCookies {
headerHiddenFields = append(headerHiddenFields, "cookie")
Expand All @@ -29,7 +29,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo

if showReq && showHeaders {
//dump req header
s, err := FormatToJson(ctx.Request.Header, headerHiddenFields)
s, err := FormatToBeautifulJson(ctx.Request.Header, headerHiddenFields)

if err != nil {
strB.WriteString(fmt.Sprintf("\nparse req header err \n" + err.Error()))
Expand Down Expand Up @@ -80,7 +80,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo
}
val, err := url.ParseQuery(string(bts))

s, err := FormatToJson(val, bodyHiddenFields)
s, err := FormatToBeautifulJson(val, bodyHiddenFields)
if err != nil {
strB.WriteString(fmt.Sprintf("\nparse req body err \n" + err.Error()))
goto DumpRes
Expand All @@ -100,7 +100,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo

if showResp && showHeaders {
//dump res header
sHeader, err := FormatToJson(ctx.Writer.Header(), headerHiddenFields)
sHeader, err := FormatToBeautifulJson(ctx.Writer.Header(), headerHiddenFields)
if err != nil {
strB.WriteString(fmt.Sprintf("\nparse res header err \n" + err.Error()))
} else {
Expand Down Expand Up @@ -145,7 +145,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo
if cb != nil {
cb(strB.String())
} else {
fmt.Print(strB.String())
fmt.Println(strB.String())
}
}
}
Expand Down
17 changes: 4 additions & 13 deletions gindump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"strings"
"testing"
Expand All @@ -29,9 +28,7 @@ func performRequest(r http.Handler, method, contentType string, path string, bod

func TestMIMEJSON(t *testing.T) {
router := gin.New()
router.Use(Dump(func(dumpStr string) {
fmt.Println(dumpStr)
}))
router.Use(Dump())

router.POST("/dump", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
Expand All @@ -56,12 +53,13 @@ func TestMIMEJSON(t *testing.T) {
}

body := bytes.NewBuffer(b)

performRequest(router, "POST", gin.MIMEJSON, "/dump", body)

}
func TestMIMEJSONWithOption(t *testing.T) {
router := gin.New()
router.Use(DumpWithOptions(true,false,true,true,false,func(dumpStr string) {
router.Use(DumpWithOptions(true, false, true, true, false, func(dumpStr string) {
fmt.Println(dumpStr)
}))

Expand Down Expand Up @@ -92,18 +90,11 @@ func TestMIMEJSONWithOption(t *testing.T) {

}



func TestMIMEPOSTFORM(t *testing.T) {
router := gin.New()
router.Use(Dump(func(dumpStr string) {
fmt.Println(dumpStr)
}))
router.Use(Dump())

router.POST("/dump", func(c *gin.Context) {
bts, err := httputil.DumpRequest(c.Request, true)
fmt.Println(string(bts), err)

c.JSON(http.StatusOK, gin.H{
"ok": true,
"data": map[string]interface{}{
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ require (
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)

go 1.13
9 changes: 4 additions & 5 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ var StringMaxLength = 0
var Newline = "\n"
var Indent = 4



func BeautifyJsonBytes(data []byte, hiddenFields []string) ([]byte, error) {
var v interface{}
if err := json.Unmarshal(data, &v); err != nil {
Expand All @@ -26,8 +24,9 @@ func BeautifyJsonBytes(data []byte, hiddenFields []string) ([]byte, error) {
return []byte(format(v, 1)), nil
}

//transfer v to json bytes
func FormatToJson(v interface{}, hiddenFields []string) ([]byte, error) {
//transfer v to beautified json bytes
func FormatToBeautifulJson(v interface{}, hiddenFields []string) ([]byte, error) {

data, err := json.Marshal(v)
if err != nil {
return nil, err
Expand Down Expand Up @@ -132,7 +131,7 @@ func removeHiddenFields(v interface{}, hiddenFields []string) interface{} {

// case insensitive key deletion
for _, hiddenField := range hiddenFields {
for k, _ := range m {
for k := range m {
if strings.ToLower(k) == strings.ToLower(hiddenField) {
delete(m, k)
}
Expand Down

0 comments on commit d104c67

Please sign in to comment.