unexport Model fields
This commit is contained in:
parent
9d791b1e3d
commit
f9aa34ef12
@ -32,12 +32,12 @@ func parseFmt(format string, value any) string {
|
|||||||
return w.String()
|
return w.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func bucket(gfsRef GridFSReference) *gridfs.Bucket {
|
func bucket(gfsRef gridFSReference) *gridfs.Bucket {
|
||||||
b, _ := gridfs.NewBucket(DB, options.GridFSBucket().SetName(gfsRef.BucketName))
|
b, _ := gridfs.NewBucket(DB, options.GridFSBucket().SetName(gfsRef.BucketName))
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func gridFsLoad(val any, g GridFSReference, field string) any {
|
func gridFsLoad(val any, g gridFSReference, field string) any {
|
||||||
doc := reflect.ValueOf(val)
|
doc := reflect.ValueOf(val)
|
||||||
rdoc := reflect.ValueOf(val)
|
rdoc := reflect.ValueOf(val)
|
||||||
if doc.Kind() != reflect.Pointer {
|
if doc.Kind() != reflect.Pointer {
|
||||||
@ -125,8 +125,8 @@ func gridFsSave(val any, imodel Model) error {
|
|||||||
}
|
}
|
||||||
_, err := structtag.Parse(string(ft.Tag))
|
_, err := structtag.Parse(string(ft.Tag))
|
||||||
panik(err)
|
panik(err)
|
||||||
var gfsRef *GridFSReference
|
var gfsRef *gridFSReference
|
||||||
for kk, vv := range imodel.GridFSReferences {
|
for kk, vv := range imodel.gridFSReferences {
|
||||||
if strings.HasPrefix(kk, ft.Name) {
|
if strings.HasPrefix(kk, ft.Name) {
|
||||||
gfsRef = &vv
|
gfsRef = &vv
|
||||||
break
|
break
|
||||||
|
14
model.go
14
model.go
@ -12,13 +12,13 @@ import (
|
|||||||
|
|
||||||
// Model - "base" struct for all queryable models
|
// Model - "base" struct for all queryable models
|
||||||
type Model struct {
|
type Model struct {
|
||||||
typeName string `bson:"-"`
|
|
||||||
Idx int
|
|
||||||
Type reflect.Type
|
|
||||||
Collection string
|
|
||||||
References map[string]Reference
|
|
||||||
Indexes map[string][]InternalIndex
|
Indexes map[string][]InternalIndex
|
||||||
GridFSReferences map[string]GridFSReference
|
Type reflect.Type
|
||||||
|
collection string
|
||||||
|
gridFSReferences map[string]gridFSReference
|
||||||
|
idx int
|
||||||
|
references map[string]Reference
|
||||||
|
typeName string `bson:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasID is a simple interface that you must implement
|
// HasID is a simple interface that you must implement
|
||||||
@ -61,7 +61,7 @@ func (m *Model) getColl() *mongo.Collection {
|
|||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Sprintf(errFmtModelNotRegistered, m.typeName))
|
panic(fmt.Sprintf(errFmtModelNotRegistered, m.typeName))
|
||||||
}
|
}
|
||||||
return DB.Collection(ri.Collection)
|
return DB.Collection(ri.collection)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) getIdxs() []*mongo.IndexModel {
|
func (m *Model) getIdxs() []*mongo.IndexModel {
|
||||||
|
12
query.go
12
query.go
@ -199,9 +199,9 @@ func (q *Query) LoadFile(fields ...string) *Query {
|
|||||||
_, cm, _ := ModelRegistry.HasByName(q.model.typeName)
|
_, cm, _ := ModelRegistry.HasByName(q.model.typeName)
|
||||||
if cm != nil {
|
if cm != nil {
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
var r GridFSReference
|
var r gridFSReference
|
||||||
hasAnnotated := false
|
hasAnnotated := false
|
||||||
for k2, v := range cm.GridFSReferences {
|
for k2, v := range cm.gridFSReferences {
|
||||||
if strings.HasPrefix(k2, field) {
|
if strings.HasPrefix(k2, field) {
|
||||||
r = v
|
r = v
|
||||||
hasAnnotated = true
|
hasAnnotated = true
|
||||||
@ -227,14 +227,14 @@ func (q *Query) Populate(fields ...string) *Query {
|
|||||||
|
|
||||||
var r Reference
|
var r Reference
|
||||||
|
|
||||||
for k2, v := range cm.References {
|
for k2, v := range cm.references {
|
||||||
if strings.HasPrefix(k2, field) {
|
if strings.HasPrefix(k2, field) {
|
||||||
r = v
|
r = v
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Exists {
|
if r.exists {
|
||||||
// get self
|
// get self
|
||||||
// get ptr
|
// get ptr
|
||||||
// find
|
// find
|
||||||
@ -263,7 +263,7 @@ func (q *Query) Populate(fields ...string) *Query {
|
|||||||
ref = ref.Elem()
|
ref = ref.Elem()
|
||||||
}
|
}
|
||||||
src := ref.Index(i).Interface()
|
src := ref.Index(i).Interface()
|
||||||
inter := populate(r, refColl.Collection, val2, field, src)
|
inter := populate(r, refColl.collection, val2, field, src)
|
||||||
if reflect.ValueOf(inter).Kind() == reflect.Pointer {
|
if reflect.ValueOf(inter).Kind() == reflect.Pointer {
|
||||||
slic.Elem().Set(reflect.Append(slic.Elem(), reflect.ValueOf(inter)))
|
slic.Elem().Set(reflect.Append(slic.Elem(), reflect.ValueOf(inter)))
|
||||||
} else {
|
} else {
|
||||||
@ -272,7 +272,7 @@ func (q *Query) Populate(fields ...string) *Query {
|
|||||||
}
|
}
|
||||||
tmp1 = slic.Interface()
|
tmp1 = slic.Interface()
|
||||||
} else {
|
} else {
|
||||||
tmp1 = populate(r, refColl.Collection, rawDoc, field, reflect.ValueOf(q.doc).Interface())
|
tmp1 = populate(r, refColl.collection, rawDoc, field, reflect.ValueOf(q.doc).Interface())
|
||||||
}
|
}
|
||||||
q.doc = tmp1
|
q.doc = tmp1
|
||||||
}
|
}
|
||||||
|
38
registry.go
38
registry.go
@ -30,10 +30,10 @@ type Reference struct {
|
|||||||
// field kind (struct, slice, ...)
|
// field kind (struct, slice, ...)
|
||||||
Kind reflect.Kind
|
Kind reflect.Kind
|
||||||
|
|
||||||
Exists bool
|
exists bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type GridFSReference struct {
|
type gridFSReference struct {
|
||||||
BucketName string
|
BucketName string
|
||||||
FilenameFmt string
|
FilenameFmt string
|
||||||
LoadType reflect.Type
|
LoadType reflect.Type
|
||||||
@ -79,7 +79,7 @@ func getRawTypeFromTag(tagOpt string, slice bool) reflect.Type {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeGfsRef(tag *structtag.Tag, idx int) GridFSReference {
|
func makeGfsRef(tag *structtag.Tag, idx int) gridFSReference {
|
||||||
opts := tag.Options
|
opts := tag.Options
|
||||||
var ffmt string
|
var ffmt string
|
||||||
if len(opts) < 1 {
|
if len(opts) < 1 {
|
||||||
@ -101,7 +101,7 @@ func makeGfsRef(tag *structtag.Tag, idx int) GridFSReference {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return GridFSReference{
|
return gridFSReference{
|
||||||
FilenameFmt: ffmt,
|
FilenameFmt: ffmt,
|
||||||
BucketName: tag.Name,
|
BucketName: tag.Name,
|
||||||
LoadType: typ,
|
LoadType: typ,
|
||||||
@ -117,7 +117,7 @@ func makeRef(idx int, modelName string, fieldName string, ht reflect.Type) Refer
|
|||||||
Model: modelName,
|
Model: modelName,
|
||||||
HydratedType: ht,
|
HydratedType: ht,
|
||||||
Kind: ht.Kind(),
|
Kind: ht.Kind(),
|
||||||
Exists: true,
|
exists: true,
|
||||||
FieldName: fieldName,
|
FieldName: fieldName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -127,17 +127,17 @@ func makeRef(idx int, modelName string, fieldName string, ht reflect.Type) Refer
|
|||||||
FieldName: fieldName,
|
FieldName: fieldName,
|
||||||
HydratedType: ht,
|
HydratedType: ht,
|
||||||
Kind: ht.Kind(),
|
Kind: ht.Kind(),
|
||||||
Exists: true,
|
exists: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic("model name was empty")
|
panic("model name was empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTags(t reflect.Type, v reflect.Value) (map[string][]InternalIndex, map[string]Reference, map[string]GridFSReference, string) {
|
func parseTags(t reflect.Type, v reflect.Value) (map[string][]InternalIndex, map[string]Reference, map[string]gridFSReference, string) {
|
||||||
coll := ""
|
coll := ""
|
||||||
refs := make(map[string]Reference)
|
refs := make(map[string]Reference)
|
||||||
idcs := make(map[string][]InternalIndex)
|
idcs := make(map[string][]InternalIndex)
|
||||||
gfsRefs := make(map[string]GridFSReference)
|
gfsRefs := make(map[string]gridFSReference)
|
||||||
|
|
||||||
for i := 0; i < v.NumField(); i++ {
|
for i := 0; i < v.NumField(); i++ {
|
||||||
sft := t.Field(i)
|
sft := t.Field(i)
|
||||||
@ -239,7 +239,7 @@ func (r TModelRegistry) HasByName(n string) (string, *Model, bool) {
|
|||||||
// Index returns the index at which the Document struct is embedded
|
// Index returns the index at which the Document struct is embedded
|
||||||
func (r TModelRegistry) Index(n string) int {
|
func (r TModelRegistry) Index(n string) int {
|
||||||
if v, ok := ModelRegistry[n]; ok {
|
if v, ok := ModelRegistry[n]; ok {
|
||||||
return v.Idx
|
return v.idx
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
@ -247,7 +247,7 @@ func (r TModelRegistry) Index(n string) int {
|
|||||||
func (r TModelRegistry) new_(n string) interface{} {
|
func (r TModelRegistry) new_(n string) interface{} {
|
||||||
if name, m, ok := ModelRegistry.HasByName(n); ok {
|
if name, m, ok := ModelRegistry.HasByName(n); ok {
|
||||||
v := reflect.New(m.Type)
|
v := reflect.New(m.Type)
|
||||||
df := v.Elem().Field(m.Idx)
|
df := v.Elem().Field(m.idx)
|
||||||
do := reflect.New(df.Type())
|
do := reflect.New(df.Type())
|
||||||
d := do.Interface().(IDocument)
|
d := do.Interface().(IDocument)
|
||||||
//d := df.Interface().(IDocument)
|
//d := df.Interface().(IDocument)
|
||||||
@ -308,26 +308,26 @@ func (r TModelRegistry) Model(mdl ...any) {
|
|||||||
panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n))
|
panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n))
|
||||||
}
|
}
|
||||||
ModelRegistry[n] = &Model{
|
ModelRegistry[n] = &Model{
|
||||||
Idx: idx,
|
idx: idx,
|
||||||
Type: t,
|
Type: t,
|
||||||
Collection: coll,
|
collection: coll,
|
||||||
Indexes: inds,
|
Indexes: inds,
|
||||||
References: refs,
|
references: refs,
|
||||||
GridFSReferences: gfs,
|
gridFSReferences: gfs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for k, v := range ModelRegistry {
|
for k, v := range ModelRegistry {
|
||||||
for k2, v2 := range v.References {
|
for k2, v2 := range v.references {
|
||||||
if !v2.Exists {
|
if !v2.exists {
|
||||||
if _, ok := ModelRegistry[v2.FieldName]; ok {
|
if _, ok := ModelRegistry[v2.FieldName]; ok {
|
||||||
tmp := ModelRegistry[k].References[k2]
|
tmp := ModelRegistry[k].references[k2]
|
||||||
ModelRegistry[k].References[k2] = Reference{
|
ModelRegistry[k].references[k2] = Reference{
|
||||||
Model: k,
|
Model: k,
|
||||||
Idx: tmp.Idx,
|
Idx: tmp.Idx,
|
||||||
FieldName: tmp.FieldName,
|
FieldName: tmp.FieldName,
|
||||||
Kind: tmp.Kind,
|
Kind: tmp.Kind,
|
||||||
HydratedType: tmp.HydratedType,
|
HydratedType: tmp.HydratedType,
|
||||||
Exists: true,
|
exists: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user