mirror of
https://github.com/pocketbase/pocketbase.git
synced 2026-10-29 16:30:25 +01:00
commit json status write after checking the fields picker
This commit is contained in:
1 parent
bf12ae0244
commit
97f9d63a1b
5 files changed
+192
-19
No files matched your search
+24
-5
@@ -3,12 +3,16 @@ package picker
|
||||
import (
|
||||
"encoding/json/jsontext"
|
||||
"encoding/json/v2"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/search"
|
||||
"github.com/pocketbase/pocketbase/tools/tokenizer"
|
||||
)
|
||||
|
||||
var ErrInvalidModifierData = errors.New("failed to apply some of the field modifiers for the provided data")
|
||||
|
||||
// Pick converts data into a []any, map[string]any, etc. (using json marshal->unmarshal)
|
||||
// containing only the fields from the parsed rawFields expression.
|
||||
//
|
||||
@@ -16,6 +20,10 @@ import (
|
||||
// Nested fields should be listed with dot-notation.
|
||||
// Fields value modifiers are also supported using the `:modifier(args)` format (see Modifiers).
|
||||
//
|
||||
// In case some data fails to apply against the registered modifiers
|
||||
// a wrapped [ErrInvalidModifierData] is returned that you can inspect and
|
||||
// decide to ignore or propagate further up the execution chain.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// data := map[string]any{"a": 1, "b": 2, "c": map[string]any{"c1": 11, "c2": 22}}
|
||||
@@ -41,7 +49,9 @@ func Pick(data any, rawFields string) (any, error) {
|
||||
}
|
||||
|
||||
var decoded any
|
||||
if err := json.Unmarshal(encoded, &decoded); err != nil {
|
||||
|
||||
err = json.Unmarshal(encoded, &decoded)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// ---
|
||||
@@ -55,10 +65,16 @@ func Pick(data any, rawFields string) (any, error) {
|
||||
|
||||
if isSearchResult {
|
||||
if decodedMap, ok := decoded.(map[string]any); ok {
|
||||
pickParsedFields(decodedMap["items"], parsedFields)
|
||||
err = pickParsedFields(decodedMap["items"], parsedFields)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %w", ErrInvalidModifierData, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pickParsedFields(decoded, parsedFields)
|
||||
err = pickParsedFields(decoded, parsedFields)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %w", ErrInvalidModifierData, err)
|
||||
}
|
||||
}
|
||||
|
||||
return decoded, nil
|
||||
@@ -94,7 +110,10 @@ func parseFields(rawFields string) (map[string]Modifier, error) {
|
||||
func pickParsedFields(data any, fields map[string]Modifier) error {
|
||||
switch v := data.(type) {
|
||||
case map[string]any:
|
||||
pickMapFields(v, fields)
|
||||
err := pickMapFields(v, fields)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case []map[string]any:
|
||||
for _, item := range v {
|
||||
if err := pickMapFields(item, fields); err != nil {
|
||||
@@ -112,7 +131,7 @@ func pickParsedFields(data any, fields map[string]Modifier) error {
|
||||
|
||||
for _, item := range v {
|
||||
if err := pickMapFields(item.(map[string]any), fields); err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,26 @@ package picker_test
|
||||
|
||||
import (
|
||||
"encoding/json/v2"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/picker"
|
||||
"github.com/pocketbase/pocketbase/tools/search"
|
||||
)
|
||||
|
||||
type brokenModifier struct {
|
||||
}
|
||||
|
||||
func (m *brokenModifier) Modify(val any) (any, error) {
|
||||
return nil, errors.New("test_error")
|
||||
}
|
||||
|
||||
func init() {
|
||||
picker.Modifiers["broken"] = func(args ...string) (picker.Modifier, error) {
|
||||
return &brokenModifier{}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickFields(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
name string
|
||||
@@ -223,6 +237,13 @@ func TestPickFields(t *testing.T) {
|
||||
false,
|
||||
`{"id":"123","rel":{"id":"456","sub":{"id":"789"},"title":"rel_title"}}`,
|
||||
},
|
||||
{
|
||||
"with modifer.Modify error",
|
||||
map[string]any{"a": 1},
|
||||
"*:broken",
|
||||
true,
|
||||
`{"a":1}`,
|
||||
},
|
||||
{
|
||||
"invalid excerpt modifier",
|
||||
map[string]any{"a": 1, "b": 2, "c": "test"},
|
||||
|
||||
Reference in new issue
Block a user