Compare commits

...

2 Commits

Author SHA1 Message Date
4297071b3b
restructuring
- use pointers where possible when using reflection
- add a `CreateSlice` func, for storing slices of Models created by Find and FindPaged queries
- modify tests to use `CreateSlice`
2024-09-05 17:53:04 -04:00
14ac93b327
rename model methods
- rename FindAll -> Find
- rename Find -> FindRaw
2024-09-05 14:32:34 -04:00
4 changed files with 89 additions and 33 deletions

@ -54,17 +54,17 @@ type HasIDSlice []HasID
type IModel interface {
Append(field string, a ...interface{}) error
Delete() error
Find(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
FindAll(query interface{}, opts ...*options.FindOptions) (*Query, error)
FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
Find(query interface{}, opts ...*options.FindOptions) (*Query, error)
FindByID(id interface{}) (*Query, error)
FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error)
FindPaged(query interface{}, page int64, perPage int64, options ...*options.FindOptions) (*Query, error)
getColl() *mongo.Collection
getIdxs() []*mongo.IndexModel
getParsedIdxs() map[string][]InternalIndex
Pull(field string, a ...any) error
Remove() error
Save() error
getColl() *mongo.Collection
getIdxs() []*mongo.IndexModel
getParsedIdxs() map[string][]InternalIndex
serializeToStore() any
setTypeName(str string)
getExists() bool
@ -120,23 +120,27 @@ func (m *Model) getParsedIdxs() map[string][]InternalIndex {
return ri.Indexes
}
func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) {
// FindRaw - find documents satisfying `query` and return a plain mongo cursor.
func (m *Model) FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) {
coll := m.getColl()
cursor, err := coll.Find(context.TODO(), query, opts...)
return cursor, err
}
func (m *Model) FindAll(query interface{}, opts ...*options.FindOptions) (*Query, error) {
// Find - find all documents satisfying `query`.
// returns a pointer to a Query for further chaining.
func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, error) {
qqn := ModelRegistry.new_(m.typeName)
qqv := reflect.New(reflect.SliceOf(reflect.TypeOf(qqn).Elem()))
qqv.Elem().Set(reflect.Zero(qqv.Elem().Type()))
qqt := reflect.SliceOf(reflect.TypeOf(qqn))
qqv := reflect.New(qqt)
qqv.Elem().Set(reflect.MakeSlice(qqt, 0, 0))
qq := &Query{
Model: *m,
Collection: m.getColl(),
doc: qqv.Interface(),
Op: OP_FIND_ALL,
}
q, err := m.Find(query, opts...)
q, err := m.FindRaw(query, opts...)
if err == nil {
rawRes := bson.A{}
@ -155,6 +159,8 @@ func (m *Model) FindAll(query interface{}, opts ...*options.FindOptions) (*Query
return qq, err
}
// FindPaged - Wrapper around FindAll with the Skip and Limit options populated.
// returns a pointer to a Query for further chaining.
func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...*options.FindOptions) (*Query, error) {
skipAmt := perPage * (page - 1)
if skipAmt < 0 {
@ -165,15 +171,19 @@ func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...
} else {
opts = append(opts, options.Find().SetSkip(skipAmt).SetLimit(perPage))
}
q, err := m.FindAll(query, opts...)
q, err := m.Find(query, opts...)
q.Op = OP_FIND_PAGED
return q, err
}
// FindByID - find a single document by its _id field.
// Wrapper around FindOne with an ID query as its first argument
func (m *Model) FindByID(id interface{}) (*Query, error) {
return m.FindOne(bson.D{{"_id", id}})
}
// FindOne - find a single document satisfying `query`.
// returns a pointer to a Query for further chaining.
func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error) {
coll := m.getColl()
rip := coll.FindOne(context.TODO(), query, options...)
@ -181,10 +191,13 @@ func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (
err := rip.Decode(&raw)
panik(err)
m.exists = true
qqn := ModelRegistry.new_(m.typeName)
v := reflect.New(reflect.TypeOf(qqn))
v.Elem().Set(reflect.ValueOf(qqn))
qq := &Query{
Collection: m.getColl(),
rawDoc: raw,
doc: ModelRegistry.new_(m.typeName),
doc: v.Elem().Interface(),
Op: OP_FIND_ONE,
Model: *m,
}
@ -330,6 +343,8 @@ func (m *Model) Swap(field string, i, j int) error {
return nil
}
// Save - updates this Model in the database,
// or inserts it if it doesn't exist
func (m *Model) Save() error {
val := valueOf(m.self)
if val.Kind() == reflect.Slice {
@ -349,9 +364,7 @@ func (m *Model) serializeToStore() any {
return serializeIDs((m).self)
}
// Create creates a new instance of a given model
// and returns a pointer to it.
func Create(d any) any {
func createBase(d any) (reflect.Value, int, string) {
var n string
var ri *InternalModel
var ok bool
@ -370,13 +383,21 @@ func Create(d any) any {
r.Elem().Set(v)
df := r.Elem().Field(i)
dm := df.Interface().(Model)
if reflect.ValueOf(d).Kind() == reflect.Pointer {
r.Elem().Set(reflect.ValueOf(d).Elem())
} else {
r.Elem().Set(reflect.ValueOf(d))
}
return r, i, n
}
// Create creates a new instance of a given model
// and returns a pointer to it.
func Create(d any) any {
r, i, n := createBase(d)
df := r.Elem().Field(i)
dm := df.Interface().(Model)
dm.typeName = n
what := r.Interface()
dm.self = what
@ -384,6 +405,15 @@ func Create(d any) any {
return what
}
func CreateSlice[T any](d T) []*T {
r, _, _ := createBase(d)
rtype := r.Type()
rslice := reflect.SliceOf(rtype)
newItem := reflect.New(rslice)
newItem.Elem().Set(reflect.MakeSlice(rslice, 0, 0))
return newItem.Elem().Interface().([]*T)
}
func (m *Model) PrintMe() {
fmt.Printf("My name is %s !\n", nameOf(m))
}

@ -81,9 +81,9 @@ func TestModel_FindAll(t *testing.T) {
initTest()
createAndSave(t, &iti_multi)
smodel := Create(story{}).(*story)
query, err := smodel.FindAll(bson.M{}, options.Find())
query, err := smodel.Find(bson.M{}, options.Find())
assert.Equal(t, nil, err)
final := make([]story, 0)
final := CreateSlice(story{})
query.Exec(&final)
assert.Greater(t, len(final), 0)
}
@ -92,11 +92,14 @@ func TestModel_PopulateMulti(t *testing.T) {
initTest()
bandDoc := Create(iti_single.Chapters[0].Bands[0]).(*band)
saveDoc(t, bandDoc)
mauthor := Create(author).(*user)
saveDoc(t, mauthor)
iti_multi.Author = mauthor
createAndSave(t, &iti_multi)
smodel := Create(story{}).(*story)
query, err := smodel.FindAll(bson.M{}, options.Find())
query, err := smodel.Find(bson.M{}, options.Find())
assert.Equal(t, nil, err)
final := make([]story, 0)
final := CreateSlice(story{})
query.Populate("Author", "Chapters.Bands").Exec(&final)
assert.Greater(t, len(final), 0)
for _, s := range final {
@ -151,6 +154,7 @@ func TestModel_Swap(t *testing.T) {
initTest()
iti_single.Author = &author
storyDoc := Create(iti_single).(*story)
saveDoc(t, storyDoc)
storyDoc.Chapters[0].Bands = append(storyDoc.Chapters[0].Bands, metallica)
assert.Equal(t, 2, len(storyDoc.Chapters[0].Bands))
err := storyDoc.Swap("Chapters[0].Bands", 0, 1)

@ -226,7 +226,7 @@ func (q *Query) Populate(fields ...string) *Query {
_, refColl, _ := ModelRegistry.HasByName(tto.Name())
var tmp1 interface{}
if arr, ok := rawDoc.(bson.A); ok {
typ := cm.Type
typ := reflect.PointerTo(cm.Type)
slic := reflect.New(reflect.SliceOf(typ))
for i, val2 := range arr {
ref := reflect.ValueOf(q.doc)
@ -236,7 +236,7 @@ func (q *Query) Populate(fields ...string) *Query {
src := ref.Index(i).Interface()
inter := populate(r, refColl.Collection, val2, field, src)
if reflect.ValueOf(inter).Kind() == reflect.Pointer {
slic.Elem().Set(reflect.Append(slic.Elem(), reflect.ValueOf(inter).Elem()))
slic.Elem().Set(reflect.Append(slic.Elem(), reflect.ValueOf(inter)))
} else {
slic.Elem().Set(reflect.Append(slic, reflect.ValueOf(inter)))
}
@ -255,35 +255,46 @@ func (q *Query) reOrganize() {
var trvo reflect.Value
if arr, ok := q.rawDoc.(bson.A); ok {
typ := ModelRegistry[q.Model.typeName].Type
if typ.Kind() != reflect.Pointer {
typ = reflect.PointerTo(typ)
}
slic := reflect.New(reflect.SliceOf(typ))
for _, v2 := range arr {
inter := reflect.ValueOf(rerere(v2, typ))
if inter.Kind() == reflect.Pointer {
/*if inter.Kind() == reflect.Pointer {
inter = inter.Elem()
}
}*/
slic.Elem().Set(reflect.Append(slic.Elem(), inter))
}
trvo = slic.Elem()
} else {
trvo = reflect.ValueOf(rerere(q.rawDoc, reflect.TypeOf(q.doc)))
for {
/*for {
if trvo.Kind() == reflect.Pointer {
trvo = trvo.Elem()
} else {
break
}
}
}*/
}
resV := reflect.ValueOf(q.doc)
for {
if resV.Kind() == reflect.Pointer {
resV = resV.Elem()
if resV.Elem().Kind() == reflect.Slice {
resV = resV.Elem()
} else {
break
}
} else {
break
}
}
resV.Set(trvo)
if resV.CanSet() {
resV.Set(trvo)
} else {
resV.Elem().Set(trvo.Elem())
}
}
func rerere(input interface{}, resType reflect.Type) interface{} {
@ -303,7 +314,7 @@ func rerere(input interface{}, resType reflect.Type) interface{} {
resType = resType.Elem()
}
resV := reflect.New(resType)
var rve reflect.Value = resV
var rve = resV
if rve.Kind() == reflect.Pointer {
rve = resV.Elem()
}
@ -472,6 +483,17 @@ func (q *Query) Exec(result interface{}) {
if q.done {
panic("Exec() has already been called!")
}
doc := reflect.ValueOf(q.doc)
if doc.Elem().Kind() == reflect.Slice {
for i := 0; i < doc.Elem().Len(); i++ {
cur := doc.Elem().Index(i)
imodel, ok := cur.Interface().(IModel)
if ok {
imodel.setExists(true)
doc.Elem().Index(i).Set(reflect.ValueOf(imodel))
}
}
}
reflect.ValueOf(result).Elem().Set(reflect.ValueOf(q.doc).Elem())
q.Model.self = q.doc
q.done = true

@ -72,7 +72,7 @@ func getRawTypeFromTag(tagOpt string, slice bool) reflect.Type {
var v uint = 0
t = reflect.TypeOf(v)
case "string":
var v string = "0"
var v = "0"
t = reflect.TypeOf(v)
}
@ -207,11 +207,11 @@ func (r TModelRegistry) Index(n string) int {
}
func (t TModelRegistry) new_(n string) interface{} {
if n, m, ok := ModelRegistry.HasByName(n); ok {
if name, m, ok := ModelRegistry.HasByName(n); ok {
v := reflect.New(m.Type)
df := v.Elem().Field(m.Idx)
d := df.Interface().(Model)
d.typeName = n
d.typeName = name
df.Set(reflect.ValueOf(d))
return v.Interface()
}