Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions core/model_populate.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,18 @@ func (m Model[T]) Populate(values ...any) Model[T] {
return m.result
}
if len(values) == 1 {
if str, ok := values[0].(string); ok && (strings.Contains(str, ",") || strings.Contains(str, " ")) {
parts := strings.FieldsFunc(str, func(r rune) bool {
return r == ',' || r == ' '
})
var parts []string
switch v := values[0].(type) {
case []string:
parts = v
case string:
if strings.Contains(v, ",") || strings.Contains(v, " ") {
parts = strings.FieldsFunc(v, func(r rune) bool {
return r == ',' || r == ' '
})
}
}
if len(parts) > 0 {
for _, value := range parts {
m = m.populate(value)
}
Expand Down
57 changes: 50 additions & 7 deletions tests/core_read_populate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestCoreReadPopulate(t *testing.T) {
}, ShouldPanicWith, errors.New("no results found matching the given query"))
})
})
Convey("Populate with a single call", func() {
Convey("Populate with a single call (Variadic arguments)", func() {
Convey("With bson field name", func() {
var bestiaries []DetailedBestiary
BestiaryModel.Find().Populate("monster", "kingdom").ExecInto(&bestiaries)
Expand All @@ -117,12 +117,55 @@ func TestCoreReadPopulate(t *testing.T) {
SoDrowner(bestiaries[1])
})
})
Convey("Populate with a single call (Space separated string)", func() {
var bestiaries []DetailedBestiary
BestiaryModel.Find().Populate("monster kingdom").ExecInto(&bestiaries)
So(bestiaries, ShouldHaveLength, 3)
SoKatakan(bestiaries[0])
SoDrowner(bestiaries[1])
Convey("Populate with a single call (Single argument)", func() {
Convey("String slice", func() {
Convey("With bson field name", func() {
var bestiaries []DetailedBestiary
BestiaryModel.Find().Populate([]string{"monster", "kingdom"}).ExecInto(&bestiaries)
So(bestiaries, ShouldHaveLength, 3)
SoKatakan(bestiaries[0])
SoDrowner(bestiaries[1])
})
Convey("With model field name", func() {
var bestiaries []DetailedBestiary
BestiaryModel.Find().Populate([]string{"Monster", "Kingdom"}).ExecInto(&bestiaries)
So(bestiaries, ShouldHaveLength, 3)
SoKatakan(bestiaries[0])
SoDrowner(bestiaries[1])
})
})
Convey("Space separated string", func() {
Convey("With bson field name", func() {
var bestiaries []DetailedBestiary
BestiaryModel.Find().Populate("monster kingdom").ExecInto(&bestiaries)
So(bestiaries, ShouldHaveLength, 3)
SoKatakan(bestiaries[0])
SoDrowner(bestiaries[1])
})
Convey("With model field name", func() {
var bestiaries []DetailedBestiary
BestiaryModel.Find().Populate("Monster Kingdom").ExecInto(&bestiaries)
So(bestiaries, ShouldHaveLength, 3)
SoKatakan(bestiaries[0])
SoDrowner(bestiaries[1])
})
})
Convey("Comma separated string", func() {
Convey("With bson field name", func() {
var bestiaries []DetailedBestiary
BestiaryModel.Find().Populate("monster, kingdom").ExecInto(&bestiaries)
So(bestiaries, ShouldHaveLength, 3)
SoKatakan(bestiaries[0])
SoDrowner(bestiaries[1])
})
Convey("With model field name", func() {
var bestiaries []DetailedBestiary
BestiaryModel.Find().Populate("Monster, Kingdom").ExecInto(&bestiaries)
So(bestiaries, ShouldHaveLength, 3)
SoKatakan(bestiaries[0])
SoDrowner(bestiaries[1])
})
})
})
Convey("Populate with select", func() {
var bestiaries []DetailedBestiary
Expand Down