Compare commits
2 Commits
f39e1b9c64
...
13f3abe75d
Author | SHA1 | Date | |
---|---|---|---|
13f3abe75d | |||
4f6d75f9d8 |
12
model.go
12
model.go
@ -40,6 +40,8 @@ type IModel interface {
|
|||||||
FindByID(id interface{}) (*Query, error)
|
FindByID(id interface{}) (*Query, error)
|
||||||
FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error)
|
FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error)
|
||||||
FindPaged(query interface{}, page int64, perPage int64, options *options.FindOptionsBuilder) (*Query, error)
|
FindPaged(query interface{}, page int64, perPage int64, options *options.FindOptionsBuilder) (*Query, error)
|
||||||
|
Exists(query interface{}) (bool, error)
|
||||||
|
ExistsID(id interface{}) (bool, error)
|
||||||
Collection() *mongo.Collection
|
Collection() *mongo.Collection
|
||||||
|
|
||||||
getIdxs() []*mongo.IndexModel
|
getIdxs() []*mongo.IndexModel
|
||||||
@ -199,6 +201,16 @@ func (m *Model) Count(query interface{}, options *options.CountOptionsBuilder) (
|
|||||||
return coll.CountDocuments(context.TODO(), query, options)
|
return coll.CountDocuments(context.TODO(), query, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Model) Exists(query interface{}) (bool, error) {
|
||||||
|
cnt, err := m.Count(query, options.Count())
|
||||||
|
return cnt > 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Model) ExistsID(id interface{}) (bool, error) {
|
||||||
|
cnt, err := m.Count(bson.M{"_id": id}, options.Count())
|
||||||
|
return cnt > 0, err
|
||||||
|
}
|
||||||
|
|
||||||
func createBase(d any) (reflect.Value, int, string) {
|
func createBase(d any) (reflect.Value, int, string) {
|
||||||
var n string
|
var n string
|
||||||
var ri *Model
|
var ri *Model
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package orm
|
package orm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go.mongodb.org/mongo-driver/v2/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
@ -108,6 +109,8 @@ func TestModel_PopulateMulti(t *testing.T) {
|
|||||||
for _, s := range final {
|
for _, s := range final {
|
||||||
assert.NotZero(t, s.Chapters[0].Bands[0].Name)
|
assert.NotZero(t, s.Chapters[0].Bands[0].Name)
|
||||||
}
|
}
|
||||||
|
bytes, _ := json.MarshalIndent(final, "", "\t")
|
||||||
|
fmt.Println(string(bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestModel_PopulateChained_Multi(t *testing.T) {
|
func TestModel_PopulateChained_Multi(t *testing.T) {
|
||||||
|
@ -394,7 +394,6 @@ func innerWatch(coll *mongo.Collection) {
|
|||||||
}}, options.UpdateOne().SetUpsert(true))
|
}}, options.UpdateOne().SetUpsert(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%v\n", data)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ type user struct {
|
|||||||
ID int64 `bson:"_id" json:"_id"`
|
ID int64 `bson:"_id" json:"_id"`
|
||||||
Document `bson:",inline" json:",inline" coll:"users"`
|
Document `bson:",inline" json:",inline" coll:"users"`
|
||||||
Username string `bson:"username" json:"username"`
|
Username string `bson:"username" json:"username"`
|
||||||
|
Favs []user `bson:"favs" json:"favs" ref:"user"`
|
||||||
}
|
}
|
||||||
type story struct {
|
type story struct {
|
||||||
ID int64 `bson:"_id" json:"_id"`
|
ID int64 `bson:"_id" json:"_id"`
|
||||||
@ -100,6 +101,11 @@ func (s *user) SetId(id any) {
|
|||||||
|
|
||||||
var author = user{
|
var author = user{
|
||||||
Username: "tablet.exe",
|
Username: "tablet.exe",
|
||||||
|
Favs: []user{
|
||||||
|
{
|
||||||
|
Username: "DarQuiel7",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func genChaps(single bool) []chapter {
|
func genChaps(single bool) []chapter {
|
||||||
|
52
util.go
52
util.go
@ -164,6 +164,15 @@ func incrementInterface(t interface{}) interface{} {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isValidId(t interface{}) bool {
|
||||||
|
switch t.(type) {
|
||||||
|
case uint, uint32, uint64, int, int32, int64, string, bson.ObjectID:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func isObject(t interface{}) bool {
|
func isObject(t interface{}) bool {
|
||||||
switch t.(type) {
|
switch t.(type) {
|
||||||
case bson.M, bson.D:
|
case bson.M, bson.D:
|
||||||
@ -172,6 +181,49 @@ func isObject(t interface{}) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func traverseFields(f string, val interface{}) (ret interface{}, remaining string) {
|
||||||
|
split := strings.Split(f, ".")
|
||||||
|
rv := reflect.ValueOf(val)
|
||||||
|
for {
|
||||||
|
if rv.Kind() == reflect.Pointer {
|
||||||
|
rv = rv.Elem()
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
asAny := make([]any, 0)
|
||||||
|
for _, s := range split {
|
||||||
|
asAny = append(asAny, s)
|
||||||
|
}
|
||||||
|
fmt.Println(asAny...)
|
||||||
|
}
|
||||||
|
if rv.Kind() == reflect.Slice {
|
||||||
|
ret = rv.Interface()
|
||||||
|
remaining = strings.Join(split[1:], ".")
|
||||||
|
fmt.Println("returning?")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
structField := rv.FieldByName(split[0])
|
||||||
|
if structField.IsValid() {
|
||||||
|
fmt.Println(structField.Interface())
|
||||||
|
if len(split) > 1 {
|
||||||
|
if structField.Kind() == reflect.Slice {
|
||||||
|
ret = structField
|
||||||
|
remaining = strings.Join(split[1:], ".")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ret, remaining = traverseFields(strings.Join(split[1:], "."), structField.Interface())
|
||||||
|
fmt.Printf("remaining = %s\n", remaining)
|
||||||
|
} else {
|
||||||
|
ret = structField.Interface()
|
||||||
|
remaining = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func pull(s reflect.Value, idx int, typ reflect.Type) reflect.Value {
|
func pull(s reflect.Value, idx int, typ reflect.Type) reflect.Value {
|
||||||
retI := reflect.New(reflect.SliceOf(typ))
|
retI := reflect.New(reflect.SliceOf(typ))
|
||||||
for i := 0; i < s.Len(); i++ {
|
for i := 0; i < s.Len(); i++ {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user