so long, mongo, and thanks for all the fish 🫡

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2025-06-30 16:58:18 -04:00
parent 4f6d75f9d8
commit 13f3abe75d
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
4 changed files with 61 additions and 1 deletions

View File

@ -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) {

View File

@ -394,7 +394,6 @@ func innerWatch(coll *mongo.Collection) {
}}, options.UpdateOne().SetUpsert(true)) }}, options.UpdateOne().SetUpsert(true))
} }
fmt.Printf("%v\n", data)
} }
} }

View File

@ -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
View File

@ -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++ {