Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelarte committed Nov 14, 2024
1 parent a220d2c commit 6333269
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
9 changes: 3 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### TODO

- Support json wrapper response
## [0.0.1]

### Added

- Adding partial response gin framework.
- Support for arrays.
- Support for nested objects.
- Support for nested objects.
- Support json wrapper response
7 changes: 5 additions & 2 deletions milogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ func Milogo(configOptions ...pkg.ConfigOption) gin.HandlerFunc {
if err := json.Unmarshal(writer.body.Bytes(), &jsonData); err == nil {
fields := c.Query(config.QueryParamField)

// TODO wrapper
wrappedJsonData := jsonData
if config.WrapperField != "" {
wrappedJsonData = jsonData.(map[string]interface{})[config.WrapperField]
}
if partialResponseFields, errParsing := config.Parser.Parse(fields); errParsing == nil &&
pkg.Filter(jsonData, partialResponseFields) == nil {
pkg.Filter(wrappedJsonData, partialResponseFields) == nil {
modifiedBody, err := json.Marshal(jsonData)
if err == nil {
c.Writer = writer.ResponseWriter // Set back to original writer
Expand Down
7 changes: 6 additions & 1 deletion milogo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,19 @@ func TestEchoWrapRoute(t *testing.T) {
fields: "name,age",
expected: `{"data":{"name":"Manuel","age":99}}`,
},
"query param in array, 1/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)
url := "/echo-wrap"
router.POST(url, func(c *gin.Context) {
type Response struct {
Data interface{} `json:"data"`
Expand Down
6 changes: 3 additions & 3 deletions pkg/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ func DefaultConfig(configOptions ...ConfigOption) Config {
Parser: NewParser(),
}
for _, co := range configOptions {
co(c)
co(&c)
}

return c
}

type ConfigOption func(c Config)
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) {
return func(c *Config) {
c.WrapperField = wrapperField
}, nil
}

0 comments on commit 6333269

Please sign in to comment.