From 2b465b064691a22cf82b979bf435240d496849c9 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 14 Jul 2023 08:36:01 +0300 Subject: [PATCH] load a default fetchFunc for dao.ExpandRecord(s) --- CHANGELOG.md | 2 ++ daos/record_expand.go | 14 +++++++++--- daos/record_expand_test.go | 44 ++++++++++++++++++++++---------------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35a18737..d64c7601 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/daos/record_expand.go b/daos/record_expand.go index f20d8328..9eec8a4d 100644 --- a/daos/record_expand.go +++ b/daos/record_expand.go @@ -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 { diff --git a/daos/record_expand_test.go b/daos/record_expand_test.go index d37294d5..5439754f 100644 --- a/daos/record_expand_test.go +++ b/daos/record_expand_test.go @@ -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",