From 13f3abe75dbf2eae988d50e133ba40b3afaac08e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Mon, 30 Jun 2025 16:58:18 -0400 Subject: [PATCH] =?UTF-8?q?so=20long,=20mongo,=20and=20thanks=20for=20all?= =?UTF-8?q?=20the=20fish=20=F0=9F=AB=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model_test.go | 3 +++ registry.go | 1 - testing.go | 6 ++++++ util.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/model_test.go b/model_test.go index dd9ff5c..71040c9 100644 --- a/model_test.go +++ b/model_test.go @@ -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) { diff --git a/registry.go b/registry.go index 18fdddd..b6edd1a 100644 --- a/registry.go +++ b/registry.go @@ -394,7 +394,6 @@ func innerWatch(coll *mongo.Collection) { }}, options.UpdateOne().SetUpsert(true)) } - fmt.Printf("%v\n", data) } } diff --git a/testing.go b/testing.go index d47343d..dc7daca 100644 --- a/testing.go +++ b/testing.go @@ -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 { diff --git a/util.go b/util.go index 5b5cf57..279870f 100644 --- a/util.go +++ b/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++ {