Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2fb9615210
Fetching contributors…

Cannot retrieve contributors at this time

file 67 lines (54 sloc) 1.918 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
package mongo

import (
"github.com/ungerik/go-start/errs"
"github.com/ungerik/go-start/model"
"github.com/ungerik/go-start/mgo/bson"
)

///////////////////////////////////////////////////////////////////////////////
// SubDocumentBase

type SubDocumentBase struct {
rootDocumentID bson.ObjectId `gostart:"-"`
collection *Collection `gostart:"-"`
selector string `gostart:"-"`
embeddingStruct interface{} `gostart:"-"`
}

func (self *SubDocumentBase) Init(collection *Collection, selector string, embeddingStruct interface{}) {
if collection == nil {
panic("mongo.SubDocumentBase.Init() called with nil collection")
}
if selector == "" {
panic("mongo.SubDocumentBase.Init() called with empty selector")
}
if embeddingStruct == nil {
panic("mongo.SubDocumentBase.Init() called with nil embeddingStruct")
}

self.collection = collection
self.selector = selector
self.embeddingStruct = embeddingStruct

InitRefs(embeddingStruct)
}

func (self *SubDocumentBase) Collection() *Collection {
return self.collection
}

// Implements DocumentInterface
func (self *SubDocumentBase) RootDocumentObjectId() bson.ObjectId {
return self.rootDocumentID
}

func (self *SubDocumentBase) RootDocumentSetObjectId(id bson.ObjectId) {
self.rootDocumentID = id
}

func (self *SubDocumentBase) Iterator() model.Iterator {
return model.NewObjectIterator(self.embeddingStruct)
}

func (self *SubDocumentBase) Save() error {
if self.embeddingStruct == nil {
return errs.Format("Can't save uninitialized mongo.SubDocument. embeddingStruct is nil.")
}
if !self.rootDocumentID.Valid() {
return errs.Format("Can't save mongo.SubDocument with invalid RootDocumentObjectId.")
}

return self.collection.Update(self.rootDocumentID, bson.M{self.selector: self.embeddingStruct})
}

func (self *SubDocumentBase) RemoveRootDocument() error {
return self.collection.Remove(self.rootDocumentID)
}
Something went wrong with that request. Please try again.