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()
|
||||
}
|
||||
|
||||
func bucket(gfsRef GridFSReference) *gridfs.Bucket {
|
||||
func bucket(gfsRef gridFSReference) *gridfs.Bucket {
|
||||
b, _ := gridfs.NewBucket(DB, options.GridFSBucket().SetName(gfsRef.BucketName))
|
||||
return b
|
||||
}
|
||||
|
||||
func gridFsLoad(val any, g GridFSReference, field string) any {
|
||||
func gridFsLoad(val any, g gridFSReference, field string) any {
|
||||
doc := reflect.ValueOf(val)
|
||||
rdoc := reflect.ValueOf(val)
|
||||
if doc.Kind() != reflect.Pointer {
|
||||
@ -125,8 +125,8 @@ func gridFsSave(val any, imodel Model) error {
|
||||
}
|
||||
_, err := structtag.Parse(string(ft.Tag))
|
||||
panik(err)
|
||||
var gfsRef *GridFSReference
|
||||
for kk, vv := range imodel.GridFSReferences {
|
||||
var gfsRef *gridFSReference
|
||||
for kk, vv := range imodel.gridFSReferences {
|
||||
if strings.HasPrefix(kk, ft.Name) {
|
||||
gfsRef = &vv
|
||||
break
|
||||
|
14
model.go
14
model.go
@ -12,13 +12,13 @@ import (
|
||||
|
||||
// Model - "base" struct for all queryable models
|
||||
type Model struct {
|
||||
typeName string `bson:"-"`
|
||||
Idx int
|
||||
Type reflect.Type
|
||||
Collection string
|
||||
References map[string]Reference
|
||||
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
|
||||
@ -61,7 +61,7 @@ func (m *Model) getColl() *mongo.Collection {
|
||||
if !ok {
|
||||
panic(fmt.Sprintf(errFmtModelNotRegistered, m.typeName))
|
||||
}
|
||||
return DB.Collection(ri.Collection)
|
||||
return DB.Collection(ri.collection)
|
||||
}
|
||||
|
||||
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)
|
||||
if cm != nil {
|
||||
for _, field := range fields {
|
||||
var r GridFSReference
|
||||
var r gridFSReference
|
||||
hasAnnotated := false
|
||||
for k2, v := range cm.GridFSReferences {
|
||||
for k2, v := range cm.gridFSReferences {
|
||||
if strings.HasPrefix(k2, field) {
|
||||
r = v
|
||||
hasAnnotated = true
|
||||
@ -227,14 +227,14 @@ func (q *Query) Populate(fields ...string) *Query {
|
||||
|
||||
var r Reference
|
||||
|
||||
for k2, v := range cm.References {
|
||||
for k2, v := range cm.references {
|
||||
if strings.HasPrefix(k2, field) {
|
||||
r = v
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if r.Exists {
|
||||
if r.exists {
|
||||
// get self
|
||||
// get ptr
|
||||
// find
|
||||
@ -263,7 +263,7 @@ func (q *Query) Populate(fields ...string) *Query {
|
||||
ref = ref.Elem()
|
||||
}
|
||||
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 {
|
||||
slic.Elem().Set(reflect.Append(slic.Elem(), reflect.ValueOf(inter)))
|
||||
} else {
|
||||
@ -272,7 +272,7 @@ func (q *Query) Populate(fields ...string) *Query {
|
||||
}
|
||||
tmp1 = slic.Interface()
|
||||
} 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
|
||||
}
|
||||
|
38
registry.go
38
registry.go
@ -30,10 +30,10 @@ type Reference struct {
|
||||
// field kind (struct, slice, ...)
|
||||
Kind reflect.Kind
|
||||
|
||||
Exists bool
|
||||
exists bool
|
||||
}
|
||||
|
||||
type GridFSReference struct {
|
||||
type gridFSReference struct {
|
||||
BucketName string
|
||||
FilenameFmt string
|
||||
LoadType reflect.Type
|
||||
@ -79,7 +79,7 @@ func getRawTypeFromTag(tagOpt string, slice bool) reflect.Type {
|
||||
return t
|
||||
}
|
||||
|
||||
func makeGfsRef(tag *structtag.Tag, idx int) GridFSReference {
|
||||
func makeGfsRef(tag *structtag.Tag, idx int) gridFSReference {
|
||||
opts := tag.Options
|
||||
var ffmt string
|
||||
if len(opts) < 1 {
|
||||
@ -101,7 +101,7 @@ func makeGfsRef(tag *structtag.Tag, idx int) GridFSReference {
|
||||
}
|
||||
}
|
||||
|
||||
return GridFSReference{
|
||||
return gridFSReference{
|
||||
FilenameFmt: ffmt,
|
||||
BucketName: tag.Name,
|
||||
LoadType: typ,
|
||||
@ -117,7 +117,7 @@ func makeRef(idx int, modelName string, fieldName string, ht reflect.Type) Refer
|
||||
Model: modelName,
|
||||
HydratedType: ht,
|
||||
Kind: ht.Kind(),
|
||||
Exists: true,
|
||||
exists: true,
|
||||
FieldName: fieldName,
|
||||
}
|
||||
}
|
||||
@ -127,17 +127,17 @@ func makeRef(idx int, modelName string, fieldName string, ht reflect.Type) Refer
|
||||
FieldName: fieldName,
|
||||
HydratedType: ht,
|
||||
Kind: ht.Kind(),
|
||||
Exists: true,
|
||||
exists: true,
|
||||
}
|
||||
}
|
||||
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 := ""
|
||||
refs := make(map[string]Reference)
|
||||
idcs := make(map[string][]InternalIndex)
|
||||
gfsRefs := make(map[string]GridFSReference)
|
||||
gfsRefs := make(map[string]gridFSReference)
|
||||
|
||||
for i := 0; i < v.NumField(); 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
|
||||
func (r TModelRegistry) Index(n string) int {
|
||||
if v, ok := ModelRegistry[n]; ok {
|
||||
return v.Idx
|
||||
return v.idx
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@ -247,7 +247,7 @@ func (r TModelRegistry) Index(n string) int {
|
||||
func (r TModelRegistry) new_(n string) interface{} {
|
||||
if name, m, ok := ModelRegistry.HasByName(n); ok {
|
||||
v := reflect.New(m.Type)
|
||||
df := v.Elem().Field(m.Idx)
|
||||
df := v.Elem().Field(m.idx)
|
||||
do := reflect.New(df.Type())
|
||||
d := do.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))
|
||||
}
|
||||
ModelRegistry[n] = &Model{
|
||||
Idx: idx,
|
||||
idx: idx,
|
||||
Type: t,
|
||||
Collection: coll,
|
||||
collection: coll,
|
||||
Indexes: inds,
|
||||
References: refs,
|
||||
GridFSReferences: gfs,
|
||||
references: refs,
|
||||
gridFSReferences: gfs,
|
||||
}
|
||||
}
|
||||
for k, v := range ModelRegistry {
|
||||
for k2, v2 := range v.References {
|
||||
if !v2.Exists {
|
||||
for k2, v2 := range v.references {
|
||||
if !v2.exists {
|
||||
if _, ok := ModelRegistry[v2.FieldName]; ok {
|
||||
tmp := ModelRegistry[k].References[k2]
|
||||
ModelRegistry[k].References[k2] = Reference{
|
||||
tmp := ModelRegistry[k].references[k2]
|
||||
ModelRegistry[k].references[k2] = Reference{
|
||||
Model: k,
|
||||
Idx: tmp.Idx,
|
||||
FieldName: tmp.FieldName,
|
||||
Kind: tmp.Kind,
|
||||
HydratedType: tmp.HydratedType,
|
||||
Exists: true,
|
||||
exists: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user