Skip to content

Commit

Permalink
adding examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelarte committed Nov 14, 2024
1 parent 7ffa1a3 commit a220d2c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 18 deletions.
6 changes: 4 additions & 2 deletions milogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (w *customResponseWriter) Write(data []byte) (int, error) {
return w.body.Write(data)
}

func Milogo() gin.HandlerFunc {
config := pkg.DefaultConfig()
func Milogo(configOptions ...pkg.ConfigOption) gin.HandlerFunc {
config := pkg.DefaultConfig(configOptions...)

return func(c *gin.Context) {
// Create a custom response writer
Expand All @@ -35,6 +35,8 @@ func Milogo() gin.HandlerFunc {
var jsonData interface{}
if err := json.Unmarshal(writer.body.Bytes(), &jsonData); err == nil {
fields := c.Query(config.QueryParamField)

// TODO wrapper
if partialResponseFields, errParsing := config.Parser.Parse(fields); errParsing == nil &&
pkg.Filter(jsonData, partialResponseFields) == nil {
modifiedBody, err := json.Marshal(jsonData)
Expand Down
83 changes: 69 additions & 14 deletions milogo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/manuelarte/milogo/pkg"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -39,13 +40,23 @@ func TestEchoRoute(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
router := setupRouter()
url := "/echo"
router.POST(url, func(c *gin.Context) {
var body map[string]interface{}
err := c.BindJSON(&body)
if err != nil {
c.Status(400)

return
}
c.JSON(200, body)
})

w := httptest.NewRecorder()
out, err := json.Marshal(test.body)
if err != nil {
t.Fatal(err)
}
url := "/echo"
if test.fields != "" {
url += "?fields=" + test.fields
}
Expand Down Expand Up @@ -172,20 +183,64 @@ func TestArrayRoute(t *testing.T) {
}
}

func setupRouter() *gin.Engine {
func TestEchoWrapRoute(t *testing.T) {
t.Parallel()

tests := map[string]struct {
body interface{}
fields string
expected string
}{
"no query param fields": {
body: map[string]interface{}{"name": "Manuel", "age": 99},
fields: "",
expected: `{"data":{"name":"Manuel","age":99}}`,
},
"query param fields, 1/2": {
body: map[string]interface{}{"name": "Manuel", "age": 99},
fields: "name",
expected: `{"data":{"name":"Manuel"}}`,
},
"query param fields, 2/2": {
body: map[string]interface{}{"name": "Manuel", "age": 99},
fields: "name,age",
expected: `{"data":{"name":"Manuel","age":99}}`,
},
}
for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()
milogoOption, _ := pkg.WithWrapField("data")
url := "/echo-wrap"
router := setupRouter(milogoOption)
router.POST(url, func(c *gin.Context) {
type Response struct {
Data interface{} `json:"data"`
}
c.JSON(200, Response{Data: test.body})
})

w := httptest.NewRecorder()
out, err := json.Marshal(test.body)
if err != nil {
t.Fatal(err)
}
if test.fields != "" {
url += "?fields=" + test.fields
}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(out))
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
assert.JSONEq(t, test.expected, w.Body.String())
})
}
}

func setupRouter(configOptions ...pkg.ConfigOption) *gin.Engine {
r := gin.Default()
r.Use(Milogo())

r.POST("/echo", func(c *gin.Context) {
var body map[string]interface{}
err := c.BindJSON(&body)
if err != nil {
c.Status(400)

return
}
c.JSON(200, body)
})
r.Use(Milogo(configOptions...))

return r
}
19 changes: 17 additions & 2 deletions pkg/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@ package pkg

type Config struct {
QueryParamField string
WrapperField string
Parser Parser
}

func DefaultConfig() Config {
return Config{
func DefaultConfig(configOptions ...ConfigOption) Config {
c := Config{
QueryParamField: "fields",
Parser: NewParser(),
}
for _, co := range configOptions {
co(c)
}

return c
}

type ConfigOption func(c Config)

func WithWrapField(wrapperField string) (ConfigOption, error) {
// validate wrapper field is not several words, but only one and alphanumeric
return func(c Config) {
c.WrapperField = wrapperField
}, nil
}

0 comments on commit a220d2c

Please sign in to comment.