load a default fetchFunc for dao.ExpandRecord(s)
This commit is contained in:
parent
fdccdcebad
commit
2b465b0646
|
@ -80,6 +80,8 @@
|
|||
- (@todo docs) For consistency and convenience it is now possible to call `Dao.RecordQuery(collectionModelOrIdentifier)` with just the collection id or name.
|
||||
In case an invalid collection id/name string is passed the query will be resolved with cancelled context error.
|
||||
|
||||
- (@todo docs) Use a default fetch function that will return all relations in case the fetchFunc argument of `Dao.ExpandRecord()` and `Dao.ExpandRecords()` is `nil`.
|
||||
|
||||
|
||||
## v0.16.8
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package daos
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
@ -24,6 +23,9 @@ type ExpandFetchFunc func(relCollection *models.Collection, relIds []string) ([]
|
|||
|
||||
// ExpandRecord expands the relations of a single Record model.
|
||||
//
|
||||
// If fetchFunc is not set, then a default function will be used that
|
||||
// returns all relation records.
|
||||
//
|
||||
// Returns a map with the failed expand parameters and their errors.
|
||||
func (dao *Dao) ExpandRecord(record *models.Record, expands []string, fetchFunc ExpandFetchFunc) map[string]error {
|
||||
return dao.ExpandRecords([]*models.Record{record}, expands, fetchFunc)
|
||||
|
@ -31,6 +33,9 @@ func (dao *Dao) ExpandRecord(record *models.Record, expands []string, fetchFunc
|
|||
|
||||
// ExpandRecords expands the relations of the provided Record models list.
|
||||
//
|
||||
// If fetchFunc is not set, then a default function will be used that
|
||||
// returns all relation records.
|
||||
//
|
||||
// Returns a map with the failed expand parameters and their errors.
|
||||
func (dao *Dao) ExpandRecords(records []*models.Record, expands []string, fetchFunc ExpandFetchFunc) map[string]error {
|
||||
normalized := normalizeExpands(expands)
|
||||
|
@ -49,13 +54,16 @@ func (dao *Dao) ExpandRecords(records []*models.Record, expands []string, fetchF
|
|||
var indirectExpandRegex = regexp.MustCompile(`^(\w+)\((\w+)\)$`)
|
||||
|
||||
// notes:
|
||||
// - fetchFunc must be non-nil func
|
||||
// - if fetchFunc is nil, dao.FindRecordsByIds will be used
|
||||
// - all records are expected to be from the same collection
|
||||
// - if MaxExpandDepth is reached, the function returns nil ignoring the remaining expand path
|
||||
// - indirect expands are supported only with single relation fields
|
||||
func (dao *Dao) expandRecords(records []*models.Record, expandPath string, fetchFunc ExpandFetchFunc, recursionLevel int) error {
|
||||
if fetchFunc == nil {
|
||||
return errors.New("Relation records fetchFunc is not set.")
|
||||
// load a default fetchFunc
|
||||
fetchFunc = func(relCollection *models.Collection, relIds []string) ([]*models.Record, error) {
|
||||
return dao.FindRecordsByIds(relCollection.Id, relIds)
|
||||
}
|
||||
}
|
||||
|
||||
if expandPath == "" || recursionLevel > MaxExpandDepth || len(records) == 0 {
|
||||
|
|
|
@ -48,15 +48,6 @@ func TestExpandRecords(t *testing.T) {
|
|||
0,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"empty fetchFunc",
|
||||
"demo4",
|
||||
[]string{"i9naidtvr6qsgb4", "qzaqccwrmva4o1n"},
|
||||
[]string{"self_rel_one", "self_rel_many.self_rel_one"},
|
||||
nil,
|
||||
0,
|
||||
2,
|
||||
},
|
||||
{
|
||||
"fetchFunc with error",
|
||||
"demo4",
|
||||
|
@ -101,6 +92,19 @@ func TestExpandRecords(t *testing.T) {
|
|||
0,
|
||||
1,
|
||||
},
|
||||
{
|
||||
"with nil fetchfunc",
|
||||
"users",
|
||||
[]string{
|
||||
"bgs820n361vj1qd",
|
||||
"4q1xlclmfloku33",
|
||||
"oap640cot4yru2s", // no rels
|
||||
},
|
||||
[]string{"rel"},
|
||||
nil,
|
||||
2,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"expand normalizations",
|
||||
"demo4",
|
||||
|
@ -132,6 +136,19 @@ func TestExpandRecords(t *testing.T) {
|
|||
2,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"with nil fetchfunc",
|
||||
"users",
|
||||
[]string{
|
||||
"bgs820n361vj1qd",
|
||||
"4q1xlclmfloku33",
|
||||
"oap640cot4yru2s", // no rels
|
||||
},
|
||||
[]string{"rel"},
|
||||
nil,
|
||||
2,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"maxExpandDepth reached",
|
||||
"demo4",
|
||||
|
@ -228,15 +245,6 @@ func TestExpandRecord(t *testing.T) {
|
|||
0,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"empty fetchFunc",
|
||||
"demo4",
|
||||
"i9naidtvr6qsgb4",
|
||||
[]string{"self_rel_one", "self_rel_many.self_rel_one"},
|
||||
nil,
|
||||
0,
|
||||
2,
|
||||
},
|
||||
{
|
||||
"fetchFunc with error",
|
||||
"demo4",
|
||||
|
|
Loading…
Reference in New Issue