Skip to content

Commit

Permalink
added basic fields wildcard support
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Aug 23, 2023
1 parent ff6904f commit cdbe6d7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

- Skip API `fields` json transformations for non 20x responses ([#3176](https://github.com/pocketbase/pocketbase/issues/3176)).

- (@todo docs) Added `fields` wildcard (`*`) support.

- (@todo docs) Added new "Strip urls domain" `editor` field option to allow controlling the default TinyMCE imported urls behavior (_default to `false` for new content_).

- Reduced the default JSVM prewarmed pool size to 25 to reduce the initial memory consumptions (_you can manually adjust the pool size with `--hooksPool=50` if you need to, but the default should suffice for most cases_).
Expand Down
19 changes: 19 additions & 0 deletions tools/rest/json_serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/tools/list"
"github.com/pocketbase/pocketbase/tools/search"
)

Expand Down Expand Up @@ -94,6 +95,24 @@ func pickMapFields(data map[string]any, fields []string) {
return // nothing to pick
}

if list.ExistInSlice("*", fields) {
// append all missing root level data keys
for k := range data {
var exists bool

for _, f := range fields {
if strings.HasPrefix(f+".", k+".") {
exists = true
break
}
}

if !exists {
fields = append(fields, k)
}
}
}

DataLoop:
for k := range data {
matchingFields := make([]string, 0, len(fields))
Expand Down
70 changes: 70 additions & 0 deletions tools/rest/json_serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,76 @@ func TestSerialize(t *testing.T) {
"fields=a,c",
`{"items":[{"a":11,"c":"test1"},{"a":22,"c":"test2"}],"page":1,"perPage":10,"totalItems":20,"totalPages":30}`,
},
{
"root wildcard",
rest.Serializer{},
200,
&search.Result{
Page: 1,
PerPage: 10,
TotalItems: 20,
TotalPages: 30,
Items: []any{
map[string]any{"a": 11, "b": 11, "c": "test1"},
map[string]any{"a": 22, "b": 22, "c": "test2"},
},
},
"fields=*",
`{"items":[{"a":11,"b":11,"c":"test1"},{"a":22,"b":22,"c":"test2"}],"page":1,"perPage":10,"totalItems":20,"totalPages":30}`,
},
{
"root wildcard with nested exception",
rest.Serializer{},
200,
map[string]any{
"id": "123",
"title": "lorem",
"rel": map[string]any{
"id": "456",
"title": "rel_title",
},
},
"fields=*,rel.id",
`{"id":"123","rel":{"id":"456"},"title":"lorem"}`,
},
{
"sub wildcard",
rest.Serializer{},
200,
map[string]any{
"id": "123",
"title": "lorem",
"rel": map[string]any{
"id": "456",
"title": "rel_title",
"sub": map[string]any{
"id": "789",
"title": "sub_title",
},
},
},
"fields=id,rel.*",
`{"id":"123","rel":{"id":"456","sub":{"id":"789","title":"sub_title"},"title":"rel_title"}}`,
},
{
"sub wildcard with nested exception",
rest.Serializer{},
200,
map[string]any{
"id": "123",
"title": "lorem",
"rel": map[string]any{
"id": "456",
"title": "rel_title",
"sub": map[string]any{
"id": "789",
"title": "sub_title",
},
},
},
"fields=id,rel.*,rel.sub.id",
`{"id":"123","rel":{"id":"456","sub":{"id":"789"},"title":"rel_title"}}`,
},
}

for _, s := range scenarios {
Expand Down

0 comments on commit cdbe6d7

Please sign in to comment.