added basic fields wildcard support
This commit is contained in:
		
							parent
							
								
									ff6904f1f8
								
							
						
					
					
						commit
						cdbe6d78d3
					
				| 
						 | 
					@ -59,6 +59,8 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- Skip API `fields` json transformations for non 20x responses ([#3176](https://github.com/pocketbase/pocketbase/issues/3176)).
 | 
					- 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_).
 | 
					- (@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_).
 | 
					- 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_).
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,6 +5,7 @@ import (
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/labstack/echo/v5"
 | 
						"github.com/labstack/echo/v5"
 | 
				
			||||||
 | 
						"github.com/pocketbase/pocketbase/tools/list"
 | 
				
			||||||
	"github.com/pocketbase/pocketbase/tools/search"
 | 
						"github.com/pocketbase/pocketbase/tools/search"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -94,6 +95,24 @@ func pickMapFields(data map[string]any, fields []string) {
 | 
				
			||||||
		return // nothing to pick
 | 
							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:
 | 
					DataLoop:
 | 
				
			||||||
	for k := range data {
 | 
						for k := range data {
 | 
				
			||||||
		matchingFields := make([]string, 0, len(fields))
 | 
							matchingFields := make([]string, 0, len(fields))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -204,6 +204,76 @@ func TestSerialize(t *testing.T) {
 | 
				
			||||||
			"fields=a,c",
 | 
								"fields=a,c",
 | 
				
			||||||
			`{"items":[{"a":11,"c":"test1"},{"a":22,"c":"test2"}],"page":1,"perPage":10,"totalItems":20,"totalPages":30}`,
 | 
								`{"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 {
 | 
						for _, s := range scenarios {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue