so long, mongo, and thanks for all the fish 🫡
This commit is contained in:
parent
4f6d75f9d8
commit
13f3abe75d
@ -1,6 +1,7 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
@ -108,6 +109,8 @@ func TestModel_PopulateMulti(t *testing.T) {
|
||||
for _, s := range final {
|
||||
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) {
|
||||
|
@ -394,7 +394,6 @@ func innerWatch(coll *mongo.Collection) {
|
||||
}}, options.UpdateOne().SetUpsert(true))
|
||||
}
|
||||
|
||||
fmt.Printf("%v\n", data)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,6 +45,7 @@ type user struct {
|
||||
ID int64 `bson:"_id" json:"_id"`
|
||||
Document `bson:",inline" json:",inline" coll:"users"`
|
||||
Username string `bson:"username" json:"username"`
|
||||
Favs []user `bson:"favs" json:"favs" ref:"user"`
|
||||
}
|
||||
type story struct {
|
||||
ID int64 `bson:"_id" json:"_id"`
|
||||
@ -100,6 +101,11 @@ func (s *user) SetId(id any) {
|
||||
|
||||
var author = user{
|
||||
Username: "tablet.exe",
|
||||
Favs: []user{
|
||||
{
|
||||
Username: "DarQuiel7",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func genChaps(single bool) []chapter {
|
||||
|
52
util.go
52
util.go
@ -164,6 +164,15 @@ func incrementInterface(t interface{}) interface{} {
|
||||
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 {
|
||||
switch t.(type) {
|
||||
case bson.M, bson.D:
|
||||
@ -172,6 +181,49 @@ func isObject(t interface{}) bool {
|
||||
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 {
|
||||
retI := reflect.New(reflect.SliceOf(typ))
|
||||
for i := 0; i < s.Len(); i++ {
|
||||
|
Loading…
x
Reference in New Issue
Block a user