本文整理汇总了Golang中gopkg/in/mgo/v2/bson.ObjectId函数的典型用法代码示例。如果您正苦于以下问题:Golang ObjectId函数的具体用法?Golang ObjectId怎么用?Golang ObjectId使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ObjectId函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: FindGroupById
func (application Application) FindGroupById(id bson.ObjectId) (Group, bool) {
result := Group{}
query := bson.M{"_id": bson.ObjectId(id)}
database.FindBy(application.Id.Hex()+"_groups", &result, query)
return result, result.Id == bson.ObjectId("")
}
示例2: To24bytesId
//Game
func (c *Game) To24bytesId() {
if len(c.Id) == 12 {
c.Id = bson.ObjectId(c.Id.Hex())
}
if len(c.OwnerId) == 12 {
c.OwnerId = bson.ObjectId(c.OwnerId.Hex())
}
}
示例3: User
func (s *AuthService) User(token string) (u bson.ObjectId, err error) {
t, err := jwt.Parse(token, s.signFunc)
if err != nil {
return bson.ObjectId(""), err
}
if s.hasExpired(t) {
return bson.ObjectId(""), &exceptions.TokenExpiredException{"Token is expired."}
}
return bson.ObjectIdHex(t.Claims["user"].(string)), nil
}
示例4: FilesGetHandler
func FilesGetHandler(c echo.Context) error {
// Create the User Session
session := session.NewSession(c)
// Validate that the request signature is valid
session.ValidateFileRequest()
redirect := false
if !session.AuthFailed {
// Get the endpoint for that record
endpoint := session.GetEndpoint()
if plugin.CheckPlugin("get_file", endpoint) {
plugin.RunPlugin("get_file", endpoint, session)
} else {
// Get the ID of the record that owns the file
record_id := bson.ObjectIdHex(session.GetParam("record_id").(string))
// Get the record from the endpoint collection
record := endpoint.FindReadRecordById(record_id, session.User)
// Check if the record exists
if record.Id != bson.ObjectId("") {
// Get the ID of the file
file_id := bson.ObjectIdHex(session.GetParam("file_id").(string))
// Get the file from the database
file := record.FindFileById(file_id)
// Validate that the file exists
if file.Id != bson.ObjectId("") {
// Redirect to the file path
c.Redirect(301, file.DownloadURL())
redirect = true
// Log the request
session.LogRequest()
}
}
if !redirect {
session.Write()
}
}
}
return nil
}
示例5: persistRelation
func (self *DocumentBase) persistRelation(value reflect.Value, autoSave bool) (error, bson.ObjectId) {
// Detect the type of the value which is stored within the slice
switch typedValue := value.Interface().(type) {
// Deserialize objects to id
case IDocumentBase:
{
// Save children when flag is enabled
if autoSave {
err := typedValue.Save()
if err != nil {
return err, bson.ObjectId("")
}
}
objectId := typedValue.GetId()
if !objectId.Valid() {
panic("DB: Can not persist the relation object because the child was not saved before (invalid id).")
}
return nil, objectId
}
// Only save the id
case bson.ObjectId:
{
if !typedValue.Valid() {
panic("DB: Can not persist the relation object because the child was not saved before (invalid id).")
}
return nil, typedValue
}
case string:
{
if !bson.IsObjectIdHex(typedValue) {
return &InvalidIdError{&QueryError{fmt.Sprintf("Invalid id`s given")}}, bson.ObjectId("")
} else {
return nil, bson.ObjectIdHex(typedValue)
}
}
default:
{
panic(fmt.Sprintf("DB: Only type 'bson.ObjectId' and 'IDocumentBase' can be stored in slices. You used %v", value.Interface()))
}
}
}
示例6: GetSuperUser
func (application Application) GetSuperUser() User {
result := User{}
query := bson.M{"super_user": true}
database.FindBy(application.Id.Hex()+"_users", &result, query)
if result.Id == bson.ObjectId("") {
result = User{}
result.SuperUser = true
result.ApplicationId = bson.ObjectId(application.Id)
result.Save()
}
return result
}
示例7: GetSessionGMId
func GetSessionGMId(req *http.Request) (bson.ObjectId, error) {
if v, err := GetGMSessionValue(req, GM_ID_SESSION_KEY); err != nil || v == nil {
return bson.ObjectId(""), errors.New("Invalid session id format")
} else {
if str, found := v.(string); found {
if bson.IsObjectIdHex(str) {
return bson.ObjectIdHex(str), nil
} else {
return bson.ObjectId(""), errors.New("Invalid session id format")
}
} else {
return bson.ObjectId(""), errors.New("Invalid session id format")
}
}
}
示例8: IdFromBase64
func IdFromBase64(b64 string) Id {
var dst = make([]byte, len(b64))
if n, err := base64.URLEncoding.Decode(dst, []byte(b64)); err == nil {
return Id{bson.ObjectId(dst[:n])}
}
return Id{}
}
示例9: FindFeedItemById
func FindFeedItemById(id bson.ObjectId) (FeedItem, error) {
var err error
Col = Session.DB("test").C("FeedItem")
feedItem := FeedItem{}
err = Col.Find(bson.M{"_id": bson.ObjectId(id)}).One(&feedItem)
return feedItem, err
}
示例10: convertToObjectId
func convertToObjectId(id string) bson.ObjectId {
objectIdFormat, err := hex.DecodeString(id)
if err != nil || len(objectIdFormat) != 12 {
panic(fmt.Sprintf("Invalid input to ObjectIdHex: %q", id))
}
return bson.ObjectId(objectIdFormat)
}
示例11: FindPaymentById
//Only can be one payment between two people
func FindPaymentById(id bson.ObjectId) (Payment, error) {
var err error
Col = Session.DB("test").C("Payment")
payment := Payment{}
err = Col.Find(bson.M{"_id": bson.ObjectId(id)}).One(&payment)
return payment, err
}
示例12: FindPurchaseById
func FindPurchaseById(id bson.ObjectId) (Purchase, error) {
var err error
Col = Session.DB("test").C("Purchase")
purchase := Purchase{}
err = Col.Find(bson.M{"_id": bson.ObjectId(id)}).One(&purchase)
return purchase, err
}
示例13: ExecuteCronTrigger
func (application Application) ExecuteCronTrigger() {
result := findTriggerForApplicationIdAndEvent(application.Id, "cron")
if result.Id != bson.ObjectId("") {
result.Run(application.Id.Hex(), "")
}
}
示例14: CheckID
// CheckID converts string s to ObjectId if it is possible,
// otherwise it returns error.
func CheckID(s string) (bson.ObjectId, error) {
d, err := hex.DecodeString(s)
if err != nil || len(d) != 12 {
return "", errors.New("invalid database ID")
}
return bson.ObjectId(d), nil
}
示例15: FindContact
func FindContact(id bson.ObjectId) (Contact, error) {
var err error
Col = Session.DB("test").C("Contact")
contact := Contact{}
err = Col.Find(bson.M{"_id": bson.ObjectId(id)}).One(&contact)
return contact, err
}